From 838d38e7e9d5e4ca53e7c0efb1f0b4981af4c172 Mon Sep 17 00:00:00 2001 From: TaeeunKim Date: Thu, 12 Oct 2023 21:38:39 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20Response=20=EA=B0=9D=EC=B2=B4=EC=97=90?= =?UTF-8?q?=20totalElements=20=EA=B0=92=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../recipe/useInfiniteRecipeCommentQuery.ts | 19 +++++++++++++++---- frontend/src/types/response.ts | 1 + 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/frontend/src/hooks/queries/recipe/useInfiniteRecipeCommentQuery.ts b/frontend/src/hooks/queries/recipe/useInfiniteRecipeCommentQuery.ts index 85cc1d4d9..994d830ef 100644 --- a/frontend/src/hooks/queries/recipe/useInfiniteRecipeCommentQuery.ts +++ b/frontend/src/hooks/queries/recipe/useInfiniteRecipeCommentQuery.ts @@ -3,8 +3,17 @@ import { useSuspendedInfiniteQuery } from '../useSuspendedInfiniteQuery'; import { recipeApi } from '@/apis'; import type { CommentResponse } from '@/types/response'; -const fetchRecipeComments = async (pageParam: number, recipeId: number) => { - const response = await recipeApi.get({ params: `/${recipeId}/comments`, queries: `?lastId=${pageParam}` }); +interface PageParam { + lastId: number; + totalElements: number | null; +} + +const fetchRecipeComments = async (pageParam: PageParam, recipeId: number) => { + const { lastId, totalElements } = pageParam; + const response = await recipeApi.get({ + params: `/${recipeId}/comments`, + queries: `?lastId=${lastId}&totalElements=${totalElements}`, + }); const data: CommentResponse = await response.json(); return data; }; @@ -12,10 +21,12 @@ const fetchRecipeComments = async (pageParam: number, recipeId: number) => { const useInfiniteRecipeCommentQuery = (recipeId: number) => { return useSuspendedInfiniteQuery( ['recipeComment', recipeId], - ({ pageParam = 0 }) => fetchRecipeComments(pageParam, recipeId), + ({ pageParam }) => fetchRecipeComments(pageParam, recipeId), { getNextPageParam: (prevResponse: CommentResponse) => { - const lastCursor = prevResponse.comments[prevResponse.comments.length - 1].id; + const lastId = prevResponse.comments[prevResponse.comments.length - 1].id; + const totalElements = prevResponse.totalElements; + const lastCursor = { lastId: lastId, totalElements: totalElements }; return prevResponse.hasNext ? lastCursor : undefined; }, } diff --git a/frontend/src/types/response.ts b/frontend/src/types/response.ts index 7f6fd28fd..fa17b469a 100644 --- a/frontend/src/types/response.ts +++ b/frontend/src/types/response.ts @@ -66,5 +66,6 @@ export interface MemberRecipeResponse { export interface CommentResponse { hasNext: boolean; + totalElements: number | null; comments: Comment[]; }