-
Vue 프로젝트 영화 검색 코드 리팩토링프로젝트/영화 검색 (Vue) 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>
'프로젝트 > 영화 검색 (Vue)' 카테고리의 다른 글
Vue 프로젝트 영화 아이템 - 텍스트 말줄임 표시와 배경 흐림 처리 (0) 2021.08.06 Vue 프로젝트 영화 아이템 - 기본 출력 (0) 2021.08.06 Vue 프로젝트 영화 목록 ID 중복 제거 (0) 2021.08.03 Vue 프로젝트 영화 검색 추가 요청 (0) 2021.08.03 Vue 프로젝트 영화 검색 (0) 2021.08.02