[6주차 과제] TanStack Query 연습#6
Open
yumin-kim2 wants to merge 32 commits into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🏡 과제 명세
💡 과제
any사용 금지✍️ 구현 요약 및 새로 배운 점
전체 폴더 구조 정리
처음에는 컴포넌트를 전부 공통
components에 둘지 고민했는데, 구현하면서 아래처럼 정리했습니다.공통으로 쓸 수 있는 컴포넌트는 src/components, 특정 페이지에서만 쓰는 컴포넌트는 해당 페이지 폴더 안의 components에 두는 방식으로 정리했습니다.
API 요청 구조 분리
TMDB 요청에서 반복되는 baseURL과 Authorization 설정은 movieClient.ts로 분리했습니다.
그리고 기능별 API는 파일을 나눠 관리했습니다.
무한 스크롤 구현
처음에는 useQuery로 1페이지만 불러오다가, 무한 스크롤 구현을 위해 useInfiniteQuery로 변경했습니다.
useInfiniteQuery의 응답은 페이지별로 쌓이기 때문에, flatMap으로 영화 배열을 하나로 합쳐 렌더링했습니다.
스크롤 하단 감지는 IntersectionObserver를 사용했습니다.
이를 통해 사용자가 페이지 하단에 도달했을 때 다음 페이지를 자동으로 불러오도록 구현했습니다.
별점 필터링
별점 필터링은 vote_average.gte를 사용해서 선택한 평점 이상인 영화만 조회하도록 구현했습니다.
필터 값은 useState로 관리하고, queryKey에 포함해서 필터 변경 시 TanStack Query가 새로운 요청을 보내도록 했습니다.
별점 저장 및 삭제
별점 입력은 0.5 ~ 10.0 범위만 저장되도록 검증했습니다.
저장/삭제 성공 시에는 메시지를 출력했습니다.
💦 구현 과정에서 어려웠던 & 고민했던 부분
컴포넌트 구조 고민
처음에는 컴포넌트를 전부 공통 components 폴더에 넣으려고 했지만, 페이지에서만 쓰는 컴포넌트까지 모두 공통 폴더에 두면 오히려 찾기 어려울 것 같았습니다.
그래서 아래처럼 기준을 나눴습니다.
이 구조로 바꾸면서 MovieCard, RatingFilter, MovieBasicInfo, MovieRatingForm의 위치가 더 명확해졌습니다.
별점 삭제 후 값이 남아있던 문제
별점 삭제 API는 성공했는데, 상세 페이지를 나갔다가 다시 들어오면 input에 이전 값이 남아 있는 문제가 있었습니다.
문제 원인은 삭제 후 rated movies 조회 결과가 바로 반영되지 않거나, 조회 실패 시 기존 상태가 남아 있었기 때문입니다.
그래서 조회 실패 시 input을 비우고, 삭제 성공 시 쿼리를 무효화하도록 처리했습니다.
이 과정을 통해 TanStack Query의 캐시 갱신이 왜 필요한지 이해하게 되었습니다.
🙋♀️ 리뷰 요청 포인트 & 질문
pages/.../components안에 두는 구조가 적절한지 궁금합니다.useInfiniteQuery와IntersectionObserver를 함께 사용한 무한 스크롤 구현 방식이 적절한지 봐주시면 좋겠습니다.ThemeProvider/GlobalStyle사용 방식이 과제 조건을 충족하기에 충분한지 확인 부탁드립니다.📷 결과물
https://drive.google.com/file/d/1c0Ch63YGIEFqRUg6MiR6w6oerbCELbl_/view?usp=sharing
https://drive.google.com/file/d/19xIwNpump_Bzn_3qyEk-Eq4jt8bLRc38/view?usp=sharing