|
1 | | -import { useQuery } from "@tanstack/react-query"; |
| 1 | +import { queryOptions, useQuery } from "@tanstack/react-query"; |
2 | 2 |
|
3 | | -import { CommunityQueryKeys, communityApi } from "./api"; |
| 3 | +import { communityApi } from "./api"; |
| 4 | +import { |
| 5 | + COMMUNITY_POST_LIST_GC_TIME, |
| 6 | + COMMUNITY_POST_LIST_STALE_TIME, |
| 7 | + communityPostListQueryKey, |
| 8 | + sortCommunityPosts, |
| 9 | +} from "./postListQuery"; |
4 | 10 |
|
5 | 11 | interface UseGetPostListProps { |
6 | 12 | boardCode: string; |
7 | 13 | category?: string | null; |
8 | 14 | } |
9 | 15 |
|
| 16 | +export const getPostListQueryOptions = ({ boardCode, category = null }: UseGetPostListProps) => |
| 17 | + queryOptions({ |
| 18 | + queryKey: communityPostListQueryKey(boardCode, category), |
| 19 | + queryFn: async () => { |
| 20 | + const response = await communityApi.getPostList(boardCode, category); |
| 21 | + return sortCommunityPosts(response.data); |
| 22 | + }, |
| 23 | + staleTime: COMMUNITY_POST_LIST_STALE_TIME, |
| 24 | + gcTime: COMMUNITY_POST_LIST_GC_TIME, |
| 25 | + }); |
| 26 | + |
10 | 27 | /** |
11 | 28 | * @description 게시글 목록 조회 훅 |
12 | 29 | */ |
13 | 30 | const useGetPostList = ({ boardCode, category = null }: UseGetPostListProps) => { |
14 | | - return useQuery({ |
15 | | - queryKey: [CommunityQueryKeys.postList, boardCode, category], |
16 | | - queryFn: () => communityApi.getPostList(boardCode, category), |
17 | | - staleTime: Infinity, |
18 | | - gcTime: 1000 * 60 * 30, // 30분 |
19 | | - select: (response) => { |
20 | | - return [...response.data].sort((a, b) => { |
21 | | - return new Date(b.updatedAt).getTime() - new Date(a.updatedAt).getTime(); |
22 | | - }); |
23 | | - }, |
24 | | - }); |
| 31 | + return useQuery(getPostListQueryOptions({ boardCode, category })); |
25 | 32 | }; |
26 | 33 |
|
27 | 34 | export default useGetPostList; |
0 commit comments