|
1 | | -// src/feature/performanceNews/performanceMain/queries.ts |
2 | | -import { useQuery } from '@tanstack/react-query'; |
3 | | -import { getPerformanceMain } from './api'; |
| 1 | +// src/features/performanceMain/queries.ts |
| 2 | +import { useMemo } from "react"; |
| 3 | +import { useQueries } from "@tanstack/react-query"; |
| 4 | +import { |
| 5 | + getPerformanceMainOngoing, |
| 6 | + getPerformanceMainPast, |
| 7 | + getPerformanceMainUpComing, |
| 8 | +} from "./api"; |
| 9 | + |
| 10 | +type SectionParams = { isUsed?: boolean }; |
| 11 | +type MainSectionParams = { |
| 12 | + ongoing?: SectionParams; |
| 13 | + upcoming?: SectionParams; |
| 14 | + past?: SectionParams; |
| 15 | +}; |
4 | 16 |
|
5 | 17 | export const performanceMainKeys = { |
6 | | - all: ['performanceMain'] as const, |
| 18 | + all: ["performanceMain"] as const, |
| 19 | + |
| 20 | + ongoing: (params: SectionParams) => [...performanceMainKeys.all, "ongoing", params] as const, |
| 21 | + |
| 22 | + upcoming: (params: SectionParams) => [...performanceMainKeys.all, "upcoming", params] as const, |
| 23 | + |
| 24 | + past: (params: SectionParams) => [...performanceMainKeys.all, "past", params] as const, |
7 | 25 | }; |
8 | 26 |
|
9 | | -export const usePerformanceMain = () => { |
10 | | - return useQuery({ |
11 | | - queryKey: performanceMainKeys.all, |
12 | | - queryFn: getPerformanceMain, |
13 | | - staleTime: Infinity, // 탭 이동/리렌더로 재요청 X |
14 | | - gcTime: 1000 * 60 * 60, // 1시간 동안 캐시 유지(원하면 더 늘려도 됨) |
15 | | - refetchOnMount: false, |
16 | | - refetchOnWindowFocus: false, |
17 | | - refetchOnReconnect: false, |
| 27 | +const commonQueryOptions = { |
| 28 | + staleTime: Infinity, |
| 29 | + gcTime: 1000 * 60 * 60, |
| 30 | + refetchOnMount: false, |
| 31 | + refetchOnWindowFocus: false, |
| 32 | + refetchOnReconnect: false, |
| 33 | +} as const; |
| 34 | + |
| 35 | +export const usePerformanceMainSections = (params: MainSectionParams = {}) => { |
| 36 | + const ongoingParams = params.ongoing ?? {}; |
| 37 | + const upcomingParams = params.upcoming ?? {}; |
| 38 | + const pastParams = params.past ?? {}; |
| 39 | + |
| 40 | + const results = useQueries({ |
| 41 | + queries: [ |
| 42 | + { |
| 43 | + queryKey: performanceMainKeys.ongoing(ongoingParams), |
| 44 | + queryFn: () => getPerformanceMainOngoing(ongoingParams), |
| 45 | + ...commonQueryOptions, |
| 46 | + }, |
| 47 | + { |
| 48 | + queryKey: performanceMainKeys.upcoming(upcomingParams), |
| 49 | + queryFn: () => getPerformanceMainUpComing(upcomingParams), |
| 50 | + ...commonQueryOptions, |
| 51 | + }, |
| 52 | + { |
| 53 | + queryKey: performanceMainKeys.past(pastParams), |
| 54 | + queryFn: () => getPerformanceMainPast(pastParams), |
| 55 | + ...commonQueryOptions, |
| 56 | + }, |
| 57 | + ], |
18 | 58 | }); |
| 59 | + |
| 60 | + const [ongoing, upcoming, past] = results; |
| 61 | + |
| 62 | + return useMemo( |
| 63 | + () => ({ |
| 64 | + ongoing: ongoing.data ?? [], |
| 65 | + upcoming: upcoming.data ?? [], |
| 66 | + past: past.data ?? [], |
| 67 | + |
| 68 | + section: { |
| 69 | + ongoing: { |
| 70 | + isLoading: ongoing.isLoading, |
| 71 | + isFetching: ongoing.isFetching, |
| 72 | + isError: ongoing.isError, |
| 73 | + error: ongoing.error, |
| 74 | + }, |
| 75 | + upcoming: { |
| 76 | + isLoading: upcoming.isLoading, |
| 77 | + isFetching: upcoming.isFetching, |
| 78 | + isError: upcoming.isError, |
| 79 | + error: upcoming.error, |
| 80 | + }, |
| 81 | + past: { |
| 82 | + isLoading: past.isLoading, |
| 83 | + isFetching: past.isFetching, |
| 84 | + isError: past.isError, |
| 85 | + error: past.error, |
| 86 | + }, |
| 87 | + }, |
| 88 | + }), |
| 89 | + [ |
| 90 | + ongoing.data, |
| 91 | + upcoming.data, |
| 92 | + past.data, |
| 93 | + ongoing.isLoading, |
| 94 | + upcoming.isLoading, |
| 95 | + past.isLoading, |
| 96 | + ongoing.isFetching, |
| 97 | + upcoming.isFetching, |
| 98 | + past.isFetching, |
| 99 | + ongoing.isError, |
| 100 | + upcoming.isError, |
| 101 | + past.isError, |
| 102 | + ongoing.error, |
| 103 | + upcoming.error, |
| 104 | + past.error, |
| 105 | + results, |
| 106 | + ] |
| 107 | + ); |
19 | 108 | }; |
0 commit comments