Skip to content

Commit

Permalink
refactor: 리뷰, 상품 페이징 변경 적용
Browse files Browse the repository at this point in the history
  • Loading branch information
xodms0309 committed Oct 17, 2023
1 parent 737d07a commit 547284e
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});

Expand All @@ -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;
},
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
},
}
Expand Down
9 changes: 1 addition & 8 deletions frontend/src/mocks/data/reviews.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
{
"page": {
"totalDataCount": 99,
"totalPages": 10,
"firstPage": true,
"lastPage": false,
"requestPage": 1,
"requestSize": 10
},
"hasNext": false,
"reviews": [
{
"id": 1,
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/mocks/handlers/reviewHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
}),
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/types/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
}

Expand Down

0 comments on commit 547284e

Please sign in to comment.