Skip to content

Commit

Permalink
feat: Response 객체에 totalElements 값 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
xodms0309 committed Oct 12, 2023
1 parent a860bb6 commit 838d38e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
19 changes: 15 additions & 4 deletions frontend/src/hooks/queries/recipe/useInfiniteRecipeCommentQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,30 @@ 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;
};

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;
},
}
Expand Down
1 change: 1 addition & 0 deletions frontend/src/types/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,6 @@ export interface MemberRecipeResponse {

export interface CommentResponse {
hasNext: boolean;
totalElements: number | null;
comments: Comment[];
}

0 comments on commit 838d38e

Please sign in to comment.