Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/entities/machine/api/useGetMachines.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { useQuery } from "@tanstack/react-query";
import { get, machineQueryKeys, machineUrl } from "@/shared/api";
import type { BaseResponseType } from "@/shared/api/types";
import { STALE_TIME } from "@/shared/constants/queryOptions";
import type { MachineResponseType } from "../model/types";

export const useGetMachines = (params: { floor?: number } = {}) => {
return useQuery({
staleTime: STALE_TIME.MACHINE,
queryKey: machineQueryKeys.getMachines(params),
queryFn: async () => {
const [washers, dryers] = await Promise.all([
Expand Down
2 changes: 2 additions & 0 deletions src/entities/report/api/useGetMalfunctionReports.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useQuery } from "@tanstack/react-query";
import { reportQueryKeys } from "@/shared/api";
import type { BaseResponseType } from "@/shared/api/types";
import { STALE_TIME } from "@/shared/constants/queryOptions";
import type { ReportParamsType, ReportResponseType } from "../model/types";
import { getMalfunctionReports as fetchMalfunctionReports } from "./getMalfunctionReports";

Expand All @@ -11,6 +12,7 @@ export const useGetMalfunctionReports = (
const queryKey = reportQueryKeys.getMalfunctionReports(params || {});

return useQuery({
staleTime: STALE_TIME.REPORT,
queryKey,
queryFn: () => fetchMalfunctionReports(params),
initialData,
Expand Down
2 changes: 2 additions & 0 deletions src/entities/reservation/api/useGetReservations.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useQuery } from "@tanstack/react-query";
import { reservationQueryKeys } from "@/shared/api";
import { STALE_TIME } from "@/shared/constants/queryOptions";
import type { ReservationItem, ReservationParamsType } from "../model/types";
import { getReservations as fetchReservations } from "./getReservations";

Expand All @@ -10,6 +11,7 @@ export const useGetReservations = (
const queryKey = reservationQueryKeys.getReservations(params || {});

return useQuery({
staleTime: STALE_TIME.RESERVATION,
queryKey,
queryFn: () => fetchReservations(params),
initialData,
Expand Down
2 changes: 2 additions & 0 deletions src/entities/user/api/useGetMyInfo.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { useQuery } from "@tanstack/react-query";
import { get, userQueryKeys, userUrl } from "@/shared/api";
import type { BaseResponseType } from "@/shared/api/types";
import { STALE_TIME } from "@/shared/constants/queryOptions";
import type { MyInfoType } from "../model/types";

export const useGetMyInfo = () => {
return useQuery({
staleTime: STALE_TIME.MY_INFO,
queryKey: userQueryKeys.getMyInfo(),
queryFn: () => get<BaseResponseType<MyInfoType>>(userUrl.getMyInfo()),
});
Expand Down
4 changes: 3 additions & 1 deletion src/entities/user/api/useGetUsers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useQuery, keepPreviousData } from "@tanstack/react-query";
import { keepPreviousData, useQuery } from "@tanstack/react-query";
import { userQueryKeys } from "@/shared/api";
import { STALE_TIME } from "@/shared/constants/queryOptions";
import type { ManagedUserItem, UserParamsType } from "../model/types";
import { getUsers as fetchUsers } from "./getUsers";

Expand All @@ -10,6 +11,7 @@ export const useGetUsers = (
const queryKey = userQueryKeys.getUsers(params || {});

return useQuery({
staleTime: STALE_TIME.USER,
queryKey,
queryFn: () => fetchUsers(params),
initialData,
Expand Down
18 changes: 18 additions & 0 deletions src/shared/constants/queryOptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const SECOND = 1000;
const MINUTE = 60 * SECOND;

/**
* 도메인별 신선도 기준.
* 세탁기 상태는 사용자가 기기를 쓰는 즉시 바뀌므로 짧게,
* 관리자 계정 정보처럼 세션 중 사실상 고정인 값은 길게 잡는다.
*/
export const STALE_TIME = {
MACHINE: 30 * SECOND,
RESERVATION: MINUTE,
REPORT: 3 * MINUTE,
USER: 3 * MINUTE,
MY_INFO: 30 * MINUTE,
} as const;

export const DEFAULT_STALE_TIME = MINUTE;
export const DEFAULT_GC_TIME = 10 * MINUTE;
Comment thread
LeeSangHyeok0731 marked this conversation as resolved.
Outdated
26 changes: 18 additions & 8 deletions src/shared/lib/TanStackProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,28 @@

import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { ReactQueryDevtools } from "@tanstack/react-query-devtools";
import type { PropsWithChildren } from "react";
import { type PropsWithChildren, useState } from "react";

import {
DEFAULT_GC_TIME,
DEFAULT_STALE_TIME,
} from "@/shared/constants/queryOptions";
import { cn } from "@/shared/lib/cn";

const TanStackProvider = ({ children }: PropsWithChildren) => {
const queryClient = new QueryClient({
defaultOptions: {
queries: {
retry: false,
},
},
});
// QueryClient를 렌더마다 새로 만들면 캐시가 통째로 버려지므로 인스턴스를 고정한다.
const [queryClient] = useState(
() =>
new QueryClient({
defaultOptions: {
queries: {
retry: false,
staleTime: DEFAULT_STALE_TIME,
gcTime: DEFAULT_GC_TIME,
},
},
}),
);

return (
<QueryClientProvider client={queryClient}>
Expand Down
Loading