Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FE] refactor: 무한스크롤 페이징 관련 api 변경 적용 #781

Merged
merged 3 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}`,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

요건 초기값 0으로 주는거죠??

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

넹 아래의 pageParam에서 초기값 0으로 설정하고 있습니다

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;
Comment on lines +23 to +24
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

},
}
);
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: `?page=${pageParam}&sort=${sort}`,
queries: `?lastProductId=${pageParam}&sort=${sort}`,
});

const data: CategoryProductResponse = await res.json();
Expand All @@ -19,9 +19,10 @@ const useInfiniteProductsQuery = (categoryId: number, sort = 'reviewCount,desc')
({ pageParam = 0 }) => fetchProducts(pageParam, categoryId, sort),
{
getNextPageParam: (prevResponse: CategoryProductResponse) => {
const isLast = prevResponse.page.lastPage;
const nextPage = prevResponse.page.requestPage + 1;
return isLast ? undefined : nextPage;
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/pbProducts.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,
"products": [
{
"id": 11,
Expand Down
9 changes: 1 addition & 8 deletions frontend/src/mocks/data/products.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,
"products": [
{
"id": 1,
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
9 changes: 2 additions & 7 deletions frontend/src/mocks/handlers/productHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ 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 @@ -37,7 +36,7 @@ export const productHandlers = [

let products = commonProducts;

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

Expand All @@ -53,11 +52,7 @@ export const productHandlers = [
sortOrder === 'asc' ? cur[key] - next[key] : next[key] - cur[key]
),
};
return res(
ctx.status(200),
ctx.json({ page: sortedProducts.page, products: products.products.slice(page * 10, (page + 1) * 10) }),
ctx.delay(500)
);
return res(ctx.status(200), ctx.json(sortedProducts), 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({ 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 {
page: Page;
hasNext: boolean;
products: Product[];
}
export interface ProductReviewResponse {
page: Page;
hasNext: boolean;
reviews: Review[];
}

Expand Down
Loading