forked from IT-Cotato/12th-OnGil-FE
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproduct-list-container.tsx
More file actions
70 lines (60 loc) · 2.08 KB
/
product-list-container.tsx
File metadata and controls
70 lines (60 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { api } from '@/lib/api-client';
import { ProductSearchResult } from '@/types/domain/product';
import { ProductSortType } from '@/types/enums';
import { ProductList } from './product-list';
import { getMyWishlist } from '@/app/actions/wishlist';
interface ProductListContainerProps {
params: Promise<{ parentId: string; id: string }>;
searchParams: Promise<{ sortType?: string; page?: string }>;
}
export default async function ProductListContainer({
params,
searchParams,
}: ProductListContainerProps) {
const { parentId, id: subCategoryId } = await params;
const { sortType = ProductSortType.POPULAR, page = '0' } = await searchParams;
// 쿼리 파라미터 검증
const validSortTypes = Object.values(ProductSortType);
const safeSortType = validSortTypes.includes(sortType as ProductSortType)
? (sortType as ProductSortType)
: ProductSortType.POPULAR;
const safePage =
Number.isFinite(Number(page)) && Number(page) >= 0 ? Number(page) : 0;
// subCategoryId 유효성 검증
const parsedCategoryId = Number(subCategoryId);
const safeCategoryId =
Number.isFinite(parsedCategoryId) && parsedCategoryId > 0
? parsedCategoryId
: null;
if (safeCategoryId === null) {
throw new Error('Invalid category ID');
}
const [result, wishlistItems] = await Promise.all([
api.get<ProductSearchResult>('/products', {
params: {
categoryId: safeCategoryId,
sortType: safeSortType,
page: safePage,
size: 20,
},
}),
getMyWishlist(),
]);
const wishlistByProductId = new Map(
wishlistItems.map((item) => [item.productId, item.wishlistId]),
);
const productsWithWishlist = result.products.content.map((product) => ({
...product,
isLiked: wishlistByProductId.has(product.id),
wishlistId: wishlistByProductId.get(product.id),
}));
const totalElements = result.products.totalElements;
return (
<ProductList
products={productsWithWishlist}
totalElements={totalElements}
productDetailFrom={`/category/${parentId}/${subCategoryId}`}
showWishlistButton
/>
);
}