프로젝트/영화 검색 (Vue)
Vue 프로젝트 영화 검색 코드 리팩토링
IT Blue
2021. 8. 6. 04:27
Vue 프로젝트 영화 검색 코드 리팩토링
1. /src/store/movie.js 수정
- 비동기 예외 처리 적용
import axios from 'axios'
import _uniqBy from 'lodash/uniqBy'
export default {
... 생략
},
// 비동기
actions: {
async searchMovies({ state, commit }, payload) {
try {
const res = await _fetchMovie({
...payload,
page: 1
})
const { Search, totalResults } = res.data
commit('updateState', {
movies: _uniqBy(Search, 'imdbID')
})
console.log(totalResults) // 266
console.log(typeof totalResults) // string
const total = parseInt(totalResults, 10)
const pageLength = Math.ceil(total / 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
commit('updateState', {
movies: [
...state.movies,
..._uniqBy(Search, 'imdbID')]
})
}
}
} catch (message) {
commit('updateState', {
movies: [],
message
})
}
}
}
}
function _fetchMovie(payload) {
const { title, type, year, page } = payload
const OMDB_API_KEY = '7035c60c'
const url = `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)
})
})
}
2. /src/components/MovieList.vue 수정
<template>
<div class="container">
<div class="inner">
<div class="message">
{{ message }}
</div>
<MovieItem
...생략 />
</div>
</div>
</template>
<script>
... 생략
computed: {
movies() {
return this.$store.state.movie.movies
},
message() {
return this.$store.state.movie.message
}
}
}
</script>