-
Notifications
You must be signed in to change notification settings - Fork 3
[REFACTOR] 필터링 및 검색 v2 로직으로 수정 #304
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
15de0ae
fix: v2 필터링 api로 마이그레이션
seong-hui 31ee8ea
delete: 사용하지 않는 파일 삭제
seong-hui 2bd24ea
refactor: 필터 및 검색 기능 SSR 적용을 위한 리팩토링- 기존 클라이언트 사이드에서 상태를 관리하고 데이터를 가…
seong-hui 72aae14
refactor(filter): Jotai atom 대신 filterListInstance로 필터 상태 관리
seong-hui 81c53a6
fix: API 요청 시 필터 파라미터가 적용되지 않는 문제 해결
seong-hui da25731
fix: 반복적으로 사용되는 필터 추출 로직 분리
seong-hui a475aef
fix: undefined값이 url에 포함되지 않도록 로직 수정
seong-hui d68d131
fix: useQuery훅을 통해서 필터링 리스트 가져오도록 로직 수정 및 Loading 컴포넌트 추가
seong-hui 428a065
fix: 가격 필터링도 가능하도록 priceAtom 연동
seong-hui 16a02b9
Merge branch 'develop' into feat/#290/filter-ssr
seong-hui File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,4 +29,9 @@ dist-ssr | |
|
|
||
| .next | ||
| next-env.d.ts | ||
| dist | ||
| dist | ||
|
|
||
| CLAUDE.md | ||
|
|
||
| # TypeScript build cache | ||
| tsconfig.tsbuildinfo | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,38 +1,74 @@ | ||
| import { FilterType, PriceType } from '@apis/filter/type'; | ||
| import instance, { getAxiosInstance } from '@apis/instance'; | ||
| import { FilterType, PriceType, TemplestaySearchParamsV2 } from '@apis/filter/type'; | ||
| import instance from '@apis/instance'; | ||
| import MESSAGES from '@apis/messages'; | ||
| import { isAxiosError } from 'axios'; | ||
|
|
||
| export const fetchFilteredList = async ( | ||
| filterData: FilterType & { price: PriceType; content: string }, | ||
| page: number, | ||
| userId?: string, | ||
| ) => { | ||
| const axiosInstance = getAxiosInstance(); | ||
|
|
||
| // v2 API | ||
| export const fetchFilteredListV2 = async (params: TemplestaySearchParamsV2) => { | ||
| try { | ||
| const response = await axiosInstance.post(`/search?page=${page}&userId=${userId}`, { | ||
| ...filterData, | ||
| }); | ||
| const response = await instance.get('/v2/api/templestay', { params }); | ||
|
|
||
| return response.data; | ||
| return response.data.data; | ||
| } catch (error) { | ||
| if (isAxiosError(error)) throw error; | ||
| else throw new Error(MESSAGES.UNKNOWN_ERROR); | ||
| } | ||
| }; | ||
|
|
||
| export const fetchFilteredCount = async ( | ||
| filterData: FilterType & { price: PriceType; content: string }, | ||
| ) => { | ||
| try { | ||
| const response = await instance.post('/public/filter/count', { | ||
| ...filterData, | ||
| }); | ||
| // 필터 데이터를 v2 API 파라미터로 변환하는 헬퍼 함수 | ||
| export const convertToV2Params = ( | ||
| groupedFilters: FilterType, | ||
| price: PriceType, | ||
| search: string, | ||
| page: number, | ||
| userId?: string, | ||
| sort?: string, | ||
| ): TemplestaySearchParamsV2 => { | ||
| const params: TemplestaySearchParamsV2 = { | ||
| page, | ||
| search: search && search.trim() !== '' ? search : undefined, | ||
| min: price.minPrice > 0 ? price.minPrice : undefined, | ||
| max: price.maxPrice < 30 ? price.maxPrice : undefined, | ||
| sort: sort && sort.trim() !== '' ? sort : undefined, | ||
| userId: userId && userId.trim() !== '' ? userId : undefined, | ||
| }; | ||
|
|
||
| return response.data; | ||
| } catch (error) { | ||
| if (isAxiosError(error)) throw error; | ||
| else throw new Error(MESSAGES.UNKNOWN_ERROR); | ||
| // 각 필터 그룹의 선택된 아이템들을 콤마로 구분된 문자열로 변환 | ||
| if (groupedFilters.region) { | ||
| const selectedRegions = Object.entries(groupedFilters.region) | ||
| .filter(([, isSelected]) => isSelected) | ||
| .map(([region]) => region); | ||
| if (selectedRegions.length > 0) { | ||
| params.region = selectedRegions.join(','); | ||
| } | ||
| } | ||
|
|
||
| if (groupedFilters.type) { | ||
| const selectedTypes = Object.entries(groupedFilters.type) | ||
| .filter(([, isSelected]) => isSelected) | ||
| .map(([type]) => type); | ||
| if (selectedTypes.length > 0) { | ||
| params.type = selectedTypes.join(','); | ||
| } | ||
| } | ||
|
|
||
| if (groupedFilters.activity) { | ||
| const selectedActivities = Object.entries(groupedFilters.activity) | ||
| .filter(([, isSelected]) => isSelected) | ||
| .map(([activity]) => activity); | ||
| if (selectedActivities.length > 0) { | ||
| params.activity = selectedActivities.join(','); | ||
| } | ||
| } | ||
|
|
||
| if (groupedFilters.etc) { | ||
| const selectedEtc = Object.entries(groupedFilters.etc) | ||
| .filter(([, isSelected]) => isSelected) | ||
| .map(([etc]) => etc); | ||
| if (selectedEtc.length > 0) { | ||
| params.etc = selectedEtc.join(','); | ||
| } | ||
| } | ||
|
|
||
| return params; | ||
| }; | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,27 +1,34 @@ | ||
| import { fetchFilteredList } from '@apis/filter/axios'; | ||
| import { fetchFilteredListV2, convertToV2Params } from '@apis/filter/axios'; | ||
| import { FetchFilteredListProps } from '@apis/filter/type'; | ||
| import { useMutation } from '@tanstack/react-query'; | ||
| import queryClient from 'src/queryClient'; | ||
|
|
||
| const useFetchFilteredList = () => { | ||
| // v2 api 사용하는 hook | ||
| const useFetchFilteredListV2 = () => { | ||
| return useMutation({ | ||
| mutationFn: ({ | ||
| groupedFilters, | ||
| adjustedPrice, | ||
| searchQuery, | ||
| page, | ||
| userId, | ||
| }: FetchFilteredListProps) => { | ||
| return fetchFilteredList( | ||
| { ...groupedFilters, price: adjustedPrice, content: searchQuery }, | ||
| sort, | ||
| }: FetchFilteredListProps & { sort?: string }) => { | ||
| const params = convertToV2Params( | ||
| groupedFilters, | ||
| adjustedPrice, | ||
| searchQuery, | ||
| page, | ||
| userId, | ||
| sort, | ||
| ); | ||
| return fetchFilteredListV2(params); | ||
| }, | ||
| onSuccess: (data, { groupedFilters, page, userId }: FetchFilteredListProps) => { | ||
| queryClient.setQueryData(['filteredList', groupedFilters, page, userId], data); | ||
| onSuccess: (data, variables) => { | ||
| const { groupedFilters, page, userId } = variables; | ||
| queryClient.setQueryData(['filteredListV2', groupedFilters, page, userId], data); | ||
| }, | ||
| }); | ||
| }; | ||
|
|
||
| export default useFetchFilteredList; | ||
| export default useFetchFilteredListV2; |
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
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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
각 필터 그룹에 대해 동일한 패턴의 코드가 반복되고 있어, 해당 부분을 별도의 헬퍼 함수로 분리한 후 이를 활용해 각 필터 값을 추출하는 방식으로 리팩토링하면 좋을 것 같습니다 !