Skip to content

Commit 2b825cb

Browse files
committed
[feat] 칵테일 정렬기능
1 parent f02f555 commit 2b825cb

File tree

2 files changed

+56
-24
lines changed

2 files changed

+56
-24
lines changed

src/domains/recipe/api/fetchRecipe.ts

Lines changed: 54 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { useInfiniteQuery, useQuery, useQueryClient } from '@tanstack/react-quer
44
import { Cocktail, Sort } from '../types/types';
55
import { useEffect, useRef } from 'react';
66

7+
78
interface CocktailResponse {
89
data: Cocktail[];
910
}
@@ -23,6 +24,11 @@ interface CocktailFilter extends SearchFilters {
2324
sortBy?: Sort;
2425
}
2526

27+
interface PageParam {
28+
lastId: number;
29+
lastValue: number | string;
30+
}
31+
2632
const fetchKeep = async (): Promise<Set<number>> => {
2733
const res = await fetch(`${getApi}/me/bar`, {
2834
method: 'GET',
@@ -37,15 +43,15 @@ const fetchKeep = async (): Promise<Set<number>> => {
3743
};
3844

3945
const fetchRecipe = async (
40-
lastId: number | null,
46+
pageParam:PageParam|null,
4147
size: number,
42-
sortBy?: Sort
48+
sortBy?: Sort,
4349
): Promise<Cocktail[]> => {
4450
const url = new URL(`${getApi}/cocktails`);
4551
url.searchParams.set('size', String(size));
46-
if (lastId !== null) {
47-
url.searchParams.set('lastId', String(lastId));
48-
url.searchParams.set('lastValue', String(lastId));
52+
if (pageParam) {
53+
url.searchParams.set('lastId', String(pageParam.lastId));
54+
url.searchParams.set('lastValue', String(pageParam.lastValue));
4955
}
5056

5157
if (sortBy) {
@@ -96,18 +102,18 @@ const hasActiveFilters = (filters: SearchFilters): boolean => {
96102

97103
export const useCocktailsInfiniteQuery = (size: number = 20, sortBy?: Sort) => {
98104
const user = useAuthStore((state) => state.user);
99-
const queryClient = useQueryClient()
100-
const prevSortBy = useRef(sortBy)
101-
102-
useEffect(() => {
103-
if (prevSortBy.current !== undefined && prevSortBy.current !== sortBy) {
104-
queryClient.removeQueries({
105-
queryKey: ['cocktails', 'infinite'],
106-
});
107-
prevSortBy.current = sortBy;
108-
}
109-
}, [sortBy, queryClient]);
110-
105+
const queryClient = useQueryClient();
106+
const prevSortBy = useRef(sortBy);
107+
108+
useEffect(() => {
109+
if (prevSortBy.current !== undefined && prevSortBy.current !== sortBy) {
110+
queryClient.removeQueries({
111+
queryKey: ['cocktails', 'infinite', prevSortBy.current],
112+
});
113+
}
114+
prevSortBy.current = sortBy;
115+
}, [sortBy, queryClient]);
116+
111117
return useInfiniteQuery({
112118
queryKey: ['cocktails', 'infinite', sortBy, size, user?.id],
113119
queryFn: async ({ pageParam }) => {
@@ -123,14 +129,38 @@ export const useCocktailsInfiniteQuery = (size: number = 20, sortBy?: Sort) => {
123129

124130
return cocktails;
125131
},
126-
getNextPageParam: (lastpage) => {
127-
if (lastpage.length < size) return undefined;
128-
return lastpage[lastpage.length - 1]?.cocktailId ?? undefined;
132+
getNextPageParam: (lastPage) => {
133+
if (lastPage.length < size) {
134+
return undefined;
135+
}
136+
137+
const lastItem = lastPage[lastPage.length - 1];
138+
if (!lastItem) return undefined;
139+
140+
141+
let lastValue: number | string;
142+
143+
switch (sortBy) {
144+
case 'keeps':
145+
lastValue = lastItem.keepCount ?? lastItem.cocktailId;
146+
break;
147+
case 'comments':
148+
lastValue = lastItem.commentCount ?? lastItem.cocktailId;
149+
break;
150+
case 'recent':
151+
default:
152+
lastValue = lastItem.cocktailId;
153+
break;
154+
}
155+
156+
return {
157+
lastId: lastItem.cocktailId,
158+
lastValue: lastValue,
159+
};
129160
},
130-
initialPageParam: null as number | null,
131-
refetchOnMount: true, // 마운트 시 재요청 (기본값: true)
132-
refetchOnWindowFocus: true, // 윈도우 포커스 시 재요청 (기본값: true)
133-
refetchOnReconnect: true, // 재연결 시 재요청
161+
initialPageParam: null as PageParam | null,
162+
refetchOnMount: false,
163+
refetchOnWindowFocus: false,
134164
});
135165
};
136166

src/domains/recipe/types/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ export interface Cocktail {
55
cocktailImgUrl: string;
66
cocktailNameKo: string;
77
isKeep: boolean;
8+
keepCount?: number;
9+
commentCount?: number;
810
}
911

1012
export interface RecommendCocktail {

0 commit comments

Comments
 (0)