Skip to content

Commit

Permalink
feat: (#619) 마감된 게시물 상세정보 1시간 캐싱하기
Browse files Browse the repository at this point in the history
  • Loading branch information
chsua committed Sep 14, 2023
1 parent ddc6b8b commit adfd841
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions frontend/src/hooks/query/post/usePostDetail.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,35 @@
import { useQuery } from '@tanstack/react-query';
import { useQuery, useQueryClient } from '@tanstack/react-query';

import { PostInfo } from '@type/post';

import { getPost, getPostForGuest } from '@api/post';

import { QUERY_KEY } from '@constants/queryKey';

import { checkClosedPost } from '@utils/time';

export const usePostDetail = (isLoggedIn: boolean, postId: number) => {
const fetchApi = isLoggedIn ? getPost : getPostForGuest;

const queryClient = useQueryClient();
const POST_DETAIL_QUERY_KEY = [QUERY_KEY.POST_DETAIL, postId, isLoggedIn];

const { data, isError, isLoading, error } = useQuery<PostInfo>(
[QUERY_KEY.POST_DETAIL, postId, isLoggedIn],
POST_DETAIL_QUERY_KEY,
() => fetchApi(postId),
{
suspense: true,

onSuccess: data => {
if (checkClosedPost(data.deadline)) {
queryClient.setQueryDefaults(POST_DETAIL_QUERY_KEY, {
cacheTime: 60 * 60 * 1000,
staleTime: 60 * 60 * 1000,
});
}

return data;
},
}
);

Expand Down

0 comments on commit adfd841

Please sign in to comment.