반응형
1. routes 수정
/movie/#123456과 같이 ID를 입력받을 수 있도록 수정
import { createRouter, createWebHashHistory} from 'vue-router'
import Home from './MyHome.vue'
import Movie from './MyMovie.vue'
import About from './MyAbout.vue'
export default createRouter({
// Hash, History --> Hash mode 사용 예정
history: createWebHashHistory(),
// pages
routes:[
{
path:'/',
component: Home
},
{
path:'/movie/:id',
component: Movie
},
{
path:'/about',
component: About
}
]
})
2. ./store/movie.js :단일 영화 상세정보 가져오기
import axios from 'axios';
import _uniqBy from 'lodash/uniqBy'
export default {
// module화 부분
namespaced:true,
// 실제 데이터
state: () => {
return {
movies:[],
message:'Search for the movie title!',
loading: false,
theMovie: {
}
}
},
// computed와 동일
getters:{
movieIds(state){
return state.movies.map(m => m.imdbID)
}
},
// methods와 동일
// 변이: state 데이터 변경
mutations:{
updateState(state, payload){
// ['movies','message','loading']
Object.keys(payload).forEach(key => {
// state.movies = payload.movies
// state.message = payload.message
// state.loading = payload.loading
state[key] = payload[key]
})
},
resetMovies(state){
state.movies =[]
}
},
// 기본적으로 비동기 처리
actions: {
async searchMovies(context, payload){
// Search 실행중이면 함수 종료
if (context.state.loading) return
// message 초기화
context.commit('updateState', {
message: '',
loading: true
});
try {
const res = await _fetchMovie({
...payload,
page: 1,
});
const { Search, totalResults } = res.data
console.log(totalResults)
context.commit('updateState', {
movies: _uniqBy(Search, 'imdbID')
})
console.log(totalResults)
console.log(typeof totalResults)
const total = parseInt(totalResults, 10) //문자열 totalResults를 10진수 정수로 변경
const pageLength = Math.ceil(total / 10) //한페이지에 10개씩
if(pageLength > 1) {
for(let page = 2; page <= pageLength; page+=1){
if(page > payload.number / 10) break
const res = await _fetchMovie({
...payload,
page
});
const { Search } = res.data
context.commit('updateState',{
movies: [
...context.state.movies,
..._uniqBy(Search, 'imdbID')
]
})
}
}
} catch (message){
context.commit("updateState", {
movie: [],
message,
});
} finally {
context.commit('updateState', {
loading: false,
})
}
},
async searchMovieWithId(context, payload){
if (context.state.loading) return
context.commit('updateState', {
theMovie: {}, //기존의 검색결과를 초기화
loading: true
})
try{
const res = await _fetchMovie(payload)
context.commit('updateState', {
theMovie:res.data
})
} catch (error){
context.commit('updateState', {
theMovie:{}
})
} finally {
context.commit('updateState', {
loading: false
})
}
}
}
}
function _fetchMovie(payload){
const { title, type, year, page, id } = payload;
const OMDB_API_KEY='abd6b67a'
const url = id
? `https://www.omdbapi.com/?apikey=${OMDB_API_KEY}&i=${id}`
:`https://www.omdbapi.com/?apikey=${OMDB_API_KEY}&s=${title}&type=${type}&y=${year}&page=${page}`;
return new Promise((resolve, reject) => {
axios.get(url)
.then((res)=> {
if (res.data.Error){ // 아무 검색어 없이 엔터입력 시 에러처리
reject(res.data.Error);
}
resolve(res);
})
.catch((err)=> {
reject(err.message);
});
});
}
3. ./components/MyLoader.vue
비동기 요청시 로더 별도로 분리
<template>
<div
:class="{ absolute,fixed }"
class="spinner-border"
:style="{
width: `${size}rem`,
height: `${size}rem`,
zIndex
}">
<svg role="status" class="inline w-8 h-8 mr-2 text-gray-200 animate-spin dark:text-gray-600 fill-yellow-400" viewBox="0 0 100 101" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z" fill="currentColor"/>
<path d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z" fill="currentFill"/>
</svg>
</div>
</template>
<script>
export default {
props: {
size: {
type: Number,
default: 2
},
absolute: {
type: Boolean,
default: false
},
fixed: {
type: Boolean,
default: false
},
zIndex: {
type: Number,
default: 0
}
}
}
</script>
<style scoped>
.spinner-border {
margin: auto;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
.absolute {
position: absolute;
}
.fixed {
position: fixed;
}
</style>
3. ./routes/MyMovie.vue
Loading시 보여줄 스켈레톤 UI와 Loading후 보여줄 상세페이지
<template>
<div class="container">
<template v-if="loading">
<div class="skeletons">
<div class="skeleton poster"></div>
<div class="specs">
<div class="skeleton title"></div>
<div class="skeleton spec"></div>
<div class="skeleton plot"></div>
<div class="skeleton etc"></div>
<div class="skeleton etc"></div>
<div class="skeleton etc"></div>
</div>
</div>
<Loader
:size="3"
:z-index="9"
fixed />
</template>
<div v-else class="movie-details">
<div
:style="{ backgroundImage: `url(${theMovie.Poster})` }"
class="poster"></div>
<div class="specs">
<div class="title">
{{ theMovie.Title }}
</div>
<div class="labels">
<span>{{ theMovie.Released }}</span>
<span>{{ theMovie.Runtime }}</span>
<span>{{ theMovie.Country }}</span>
</div>
<div class="plot">
{{ theMovie.Plot }}
</div>
<div class="ratings">
<h3>Ratings</h3>
</div>
<div>
<h3>Actors</h3>
{{ theMovie.Actors}}
</div>
<div>
<h3>Director</h3>
{{ theMovie.Director }}
</div>
<div>
<h3>Production</h3>
{{ theMovie.Production }}
</div>
<div>
<h3>Genre</h3>
{{ theMovie.Genre }}
</div>
</div>
</div>
</div>
</template>
<script>
import Loader from '../components/MyLoader.vue'
export default {
components: {
Loader
},
computed: {
theMovie() {
return this.$store.state.movie.theMovie;
},
loading() {
return this.$store.state.movie.loading
}
},
created() {
this.$store.dispatch('movie/searchMovieWithId',{
id:this.$route.params.id
})
}
}
</script>
<style scoped>
.container {
padding-top: 40px;
margin: 0 auto;
}
.skeletons{
display: flex;
}
.skeletons .poster {
flex-shrink: 0;
width: 500px;
height: 750px;
margin-right: 70px;
}
.skeletons .specs {
flex-grow: 1;
}
.skeletons .skeleton {
border-radius: 10px;
background-color: lightgray;
}
.skeletons .title {
width: 80%;
height: 70px;
}
.skeletons .spec {
width: 60%;
height: 30px;
margin-top: 20px;
}
.skeletons .plot {
width: 100%;
height: 250px;
margin-top: 20px;
}
.skeletons .etc {
width: 50%;
height: 50px;
margin-top: 20px;
}
.movie-details{
display: flex;
color: gray;
}
.movie-details .poster {
flex-shrink: 0;
width: 500px;
height: 750px;
margin-right: 70px;
border-radius: 10px;
background-color: lightgray;
background-size: cover;
background-position: center;
}
.movie-details .specs {
flex-grow: 1;
}
.movie-details .specs .title {
color: black;
font-family: 'Oswald', sans-serif;
font-size: 70px;
line-height: 1;
margin-bottom: 30px;
}
.movie-details .specs .labels {
color: rgb(138, 138, 23);
}
.movie-details .specs .labels span::after{
content:"\00b7";
margin: 0 6px;
}
.movie-details .specs .labels span:last-child::after{
display: none;
}
.movie-details .specs .plot {
margin-top: 20px;
}
.movie-details .specs .ratings {}
.movie-details .specs h3 {
margin: 24px 0 6px;
color: black;
font-family: "Oswald", sans-serif;
font-size: 20px;
}
</style>
반응형
'Programming > Vue' 카테고리의 다른 글
Vue3 + bootstrap5 적용하기 (0) | 2022.05.28 |
---|---|
Vuex 사용 (0) | 2022.05.24 |
Vue 영화검색사이트 기본 설치 파일 (0) | 2022.05.14 |
Vue Tailwind CSS 적용하기 (0) | 2022.05.13 |
Vue apache 배포 오류 (0) | 2022.05.12 |
댓글