From 547284eabd73c9e7be828b4554b060cfa6b64eac Mon Sep 17 00:00:00 2001 From: TaeeunKim Date: Wed, 18 Oct 2023 00:24:31 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20=EB=A6=AC=EB=B7=B0,=20=EC=83=81?= =?UTF-8?q?=ED=92=88=20=ED=8E=98=EC=9D=B4=EC=A7=95=20=EB=B3=80=EA=B2=BD=20?= =?UTF-8?q?=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../queries/product/useInfiniteProductReviewsQuery.ts | 7 +++---- .../hooks/queries/product/useInfiniteProductsQuery.ts | 6 ++++-- frontend/src/mocks/data/reviews.json | 9 +-------- frontend/src/mocks/handlers/reviewHandlers.ts | 2 +- frontend/src/types/response.ts | 4 ++-- 5 files changed, 11 insertions(+), 17 deletions(-) diff --git a/frontend/src/hooks/queries/product/useInfiniteProductReviewsQuery.ts b/frontend/src/hooks/queries/product/useInfiniteProductReviewsQuery.ts index 0fb4d604b..8142c77e0 100644 --- a/frontend/src/hooks/queries/product/useInfiniteProductReviewsQuery.ts +++ b/frontend/src/hooks/queries/product/useInfiniteProductReviewsQuery.ts @@ -6,7 +6,7 @@ import type { ProductReviewResponse } from '@/types/response'; const fetchProductReviews = async (pageParam: number, productId: number, sort: string) => { const res = await productApi.get({ params: `/${productId}/reviews`, - queries: `?sort=${sort}&page=${pageParam}`, + queries: `?sort=${sort}&lastReviewId=${pageParam}`, credentials: true, }); @@ -20,9 +20,8 @@ const useInfiniteProductReviewsQuery = (productId: number, sort: string) => { ({ pageParam = 0 }) => fetchProductReviews(pageParam, productId, sort), { getNextPageParam: (prevResponse: ProductReviewResponse) => { - const isLast = prevResponse.page.lastPage; - const nextPage = prevResponse.page.requestPage + 1; - return isLast ? undefined : nextPage; + const lastCursor = prevResponse.reviews.length ? prevResponse.reviews[prevResponse.reviews.length - 1].id : 0; + return prevResponse.hasNext ? lastCursor : undefined; }, } ); diff --git a/frontend/src/hooks/queries/product/useInfiniteProductsQuery.ts b/frontend/src/hooks/queries/product/useInfiniteProductsQuery.ts index 40067960d..1b9fdd57f 100644 --- a/frontend/src/hooks/queries/product/useInfiniteProductsQuery.ts +++ b/frontend/src/hooks/queries/product/useInfiniteProductsQuery.ts @@ -6,7 +6,7 @@ import type { CategoryProductResponse } from '@/types/response'; const fetchProducts = async (pageParam: number, categoryId: number, sort = 'reviewCount,desc') => { const res = await categoryApi.get({ params: `/${categoryId}/products`, - queries: `?id=${pageParam}&sort=${sort}`, + queries: `?lastProductId=${pageParam}&sort=${sort}`, }); const data: CategoryProductResponse = await res.json(); @@ -19,7 +19,9 @@ const useInfiniteProductsQuery = (categoryId: number, sort = 'reviewCount,desc') ({ pageParam = 0 }) => fetchProducts(pageParam, categoryId, sort), { getNextPageParam: (prevResponse: CategoryProductResponse) => { - const lastCursor = prevResponse.products[prevResponse.products.length - 1].id; + const lastCursor = prevResponse.products.length + ? prevResponse.products[prevResponse.products.length - 1].id + : 0; return prevResponse.hasNext ? lastCursor : undefined; }, } diff --git a/frontend/src/mocks/data/reviews.json b/frontend/src/mocks/data/reviews.json index 54b9719ce..5f9d129ee 100644 --- a/frontend/src/mocks/data/reviews.json +++ b/frontend/src/mocks/data/reviews.json @@ -1,12 +1,5 @@ { - "page": { - "totalDataCount": 99, - "totalPages": 10, - "firstPage": true, - "lastPage": false, - "requestPage": 1, - "requestSize": 10 - }, + "hasNext": false, "reviews": [ { "id": 1, diff --git a/frontend/src/mocks/handlers/reviewHandlers.ts b/frontend/src/mocks/handlers/reviewHandlers.ts index 5c007c605..656891e55 100644 --- a/frontend/src/mocks/handlers/reviewHandlers.ts +++ b/frontend/src/mocks/handlers/reviewHandlers.ts @@ -42,7 +42,7 @@ export const reviewHandlers = [ return res( ctx.status(200), - ctx.json({ page: sortedReviews.page, reviews: sortedReviews.reviews }), + ctx.json({ hasNext: sortedReviews.hasNext, reviews: sortedReviews.reviews }), ctx.delay(1000) ); }), diff --git a/frontend/src/types/response.ts b/frontend/src/types/response.ts index 523637aeb..bac466ebf 100644 --- a/frontend/src/types/response.ts +++ b/frontend/src/types/response.ts @@ -14,11 +14,11 @@ export interface Page { } export interface CategoryProductResponse { - hasNext: true; + hasNext: boolean; products: Product[]; } export interface ProductReviewResponse { - page: Page; + hasNext: boolean; reviews: Review[]; }