260303_01_ENH: 카테고리 파트 구조 정리
This commit is contained in:
32
src/query/keys.ts
Normal file
32
src/query/keys.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
import type {ProductResponse, StatisticsRequest} from "@/types/lg";
|
||||
|
||||
const sortRecord = <T,>(obj?: Record<string, T> | null): Record<string, T> => {
|
||||
if (!obj) return {};
|
||||
return Object.fromEntries(
|
||||
Object.keys(obj)
|
||||
.sort()
|
||||
.map((k) => [k, obj[k]])
|
||||
);
|
||||
};
|
||||
|
||||
const sortNumberArrayRecord = (obj?: Record<string, number[]> | null): Record<string, number[]> => {
|
||||
if (!obj) return {};
|
||||
return Object.fromEntries(
|
||||
Object.keys(obj)
|
||||
.sort()
|
||||
.map((k) => [k, [...(obj[k] ?? [])].sort((a, b) => a - b)])
|
||||
);
|
||||
};
|
||||
|
||||
const normalizeReqForKey = (req: StatisticsRequest) => ({
|
||||
variableIdMap: sortRecord(req.variableIdMap),
|
||||
bannerIdList: [...(req.bannerIdList ?? [])].sort(),
|
||||
filterCondition: sortNumberArrayRecord(req.filterCondition as any), // normalizeFilterCondition 결과면 Record<string, number[]>
|
||||
option: req.option ?? null,
|
||||
});
|
||||
|
||||
export const productStatisticsKey = (req: StatisticsRequest) =>
|
||||
["statistics", "product", normalizeReqForKey(req)] as const;
|
||||
|
||||
export const bannerStatisticsKey = (req: StatisticsRequest) =>
|
||||
["statistics", "banner", normalizeReqForKey(req)] as const;
|
||||
28
src/query/statistics.ts
Normal file
28
src/query/statistics.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import {BannerResponse, ProductResponse, StatisticsRequest} from "@/types/lg";
|
||||
import {useQuery} from "@tanstack/react-query";
|
||||
import {bannerStatisticsKey, productStatisticsKey} from "@/query/keys";
|
||||
import {fetchBannerStatistics, fetchProductStatistics} from "@/lib/lg-dashboard";
|
||||
|
||||
export function useProductStatisticsQuery(req: StatisticsRequest, enabled = true) {
|
||||
return useQuery<ProductResponse>({
|
||||
queryKey: productStatisticsKey(req),
|
||||
queryFn: () => fetchProductStatistics(req),
|
||||
enabled,
|
||||
staleTime: 60_000,
|
||||
gcTime: 10 * 60_000,
|
||||
});
|
||||
}
|
||||
|
||||
export function useBannerStatisticsQuery(req: StatisticsRequest, enabled = true) {
|
||||
return useQuery<BannerResponse>({
|
||||
queryKey: bannerStatisticsKey(req),
|
||||
queryFn: () => fetchBannerStatistics(req),
|
||||
enabled,
|
||||
/*
|
||||
refetchOnMount: "always",
|
||||
staleTime: 0,
|
||||
*/
|
||||
staleTime: 60_000,
|
||||
gcTime: 10 * 60_000,
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user