Skip to content

Commit

Permalink
Revert "[FE] refactor: 무한스크롤 페이징 관련 api 변경 적용 (#781)"
Browse files Browse the repository at this point in the history
This reverts commit 9342c22.
  • Loading branch information
xodms0309 authored Oct 18, 2023
1 parent 9342c22 commit d73714a
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 16 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}&lastReviewId=${pageParam}`,
queries: `?sort=${sort}&page=${pageParam}`,
credentials: true,
});

Expand All @@ -20,8 +20,9 @@ const useInfiniteProductReviewsQuery = (productId: number, sort: string) => {
({ pageParam = 0 }) => fetchProductReviews(pageParam, productId, sort),
{
getNextPageParam: (prevResponse: ProductReviewResponse) => {
const lastCursor = prevResponse.reviews.length ? prevResponse.reviews[prevResponse.reviews.length - 1].id : 0;
return prevResponse.hasNext ? lastCursor : undefined;
const isLast = prevResponse.page.lastPage;
const nextPage = prevResponse.page.requestPage + 1;
return isLast ? undefined : nextPage;
},
}
);
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: `?lastProductId=${pageParam}&sort=${sort}`,
queries: `?page=${pageParam}&sort=${sort}`,
});

const data: CategoryProductResponse = await res.json();
Expand All @@ -19,10 +19,9 @@ const useInfiniteProductsQuery = (categoryId: number, sort = 'reviewCount,desc')
({ pageParam = 0 }) => fetchProducts(pageParam, categoryId, sort),
{
getNextPageParam: (prevResponse: CategoryProductResponse) => {
const lastCursor = prevResponse.products.length
? prevResponse.products[prevResponse.products.length - 1].id
: 0;
return prevResponse.hasNext ? lastCursor : undefined;
const isLast = prevResponse.page.lastPage;
const nextPage = prevResponse.page.requestPage + 1;
return isLast ? undefined : nextPage;
},
}
);
Expand Down
9 changes: 8 additions & 1 deletion frontend/src/mocks/data/pbProducts.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
{
"hasNext": false,
"page": {
"totalDataCount": 99,
"totalPages": 10,
"firstPage": true,
"lastPage": false,
"requestPage": 1,
"requestSize": 10
},
"products": [
{
"id": 11,
Expand Down
9 changes: 8 additions & 1 deletion frontend/src/mocks/data/products.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
{
"hasNext": false,
"page": {
"totalDataCount": 99,
"totalPages": 10,
"firstPage": true,
"lastPage": false,
"requestPage": 1,
"requestSize": 10
},
"products": [
{
"id": 1,
Expand Down
9 changes: 8 additions & 1 deletion frontend/src/mocks/data/reviews.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
{
"hasNext": false,
"page": {
"totalDataCount": 99,
"totalPages": 10,
"firstPage": true,
"lastPage": false,
"requestPage": 1,
"requestSize": 10
},
"reviews": [
{
"id": 1,
Expand Down
9 changes: 7 additions & 2 deletions frontend/src/mocks/handlers/productHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export const productHandlers = [
rest.get('/api/categories/:categoryId/products', (req, res, ctx) => {
const sortOptions = req.url.searchParams.get('sort');
const categoryId = req.params.categoryId;
const page = Number(req.url.searchParams.get('page'));

if (sortOptions === null) {
return res(ctx.status(400));
Expand All @@ -36,7 +37,7 @@ export const productHandlers = [

let products = commonProducts;

if (Number(categoryId) >= 6 && Number(categoryId) <= 9) {
if (Number(categoryId) >= 7 && Number(categoryId) <= 9) {
products = pbProducts;
}

Expand All @@ -52,7 +53,11 @@ export const productHandlers = [
sortOrder === 'asc' ? cur[key] - next[key] : next[key] - cur[key]
),
};
return res(ctx.status(200), ctx.json(sortedProducts), ctx.delay(500));
return res(
ctx.status(200),
ctx.json({ page: sortedProducts.page, products: products.products.slice(page * 10, (page + 1) * 10) }),
ctx.delay(500)
);
}),

rest.get('/api/products/:productId', (req, res, ctx) => {
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({ hasNext: sortedReviews.hasNext, reviews: sortedReviews.reviews }),
ctx.json({ page: sortedReviews.page, 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: boolean;
page: Page;
products: Product[];
}
export interface ProductReviewResponse {
hasNext: boolean;
page: Page;
reviews: Review[];
}

Expand Down

0 comments on commit d73714a

Please sign in to comment.