Skip to content

Commit 18eca5a

Browse files
committed
[WRFE-66](feat): 상세 카테고리 메뉴바에 전체 항목 추가
1 parent 8bfefeb commit 18eca5a

File tree

1 file changed

+66
-45
lines changed
  • apps/front/wraffle-webview/app/category

1 file changed

+66
-45
lines changed

apps/front/wraffle-webview/app/category/page.tsx

Lines changed: 66 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use client';
22

33
import {useSearchParams} from 'next/navigation';
4-
import {useState} from 'react';
4+
import {useEffect, useState} from 'react';
55
import {categories} from '@/entities/category';
66
import type {CategoryItem} from '@/entities/category/type';
77
import {Header} from '@/shared/ui';
@@ -13,13 +13,13 @@ import {RaffleCard} from '@wraffle/ui';
1313
const sampleProducts = [
1414
{
1515
id: 2,
16-
name: '럭셔리 시계 경품 래플',
16+
name: '이거 삼성 상품임',
1717
price: 10000,
1818
thumbnailUrl:
1919
'https://github.com/user-attachments/assets/4a104905-0106-4b8a-8dcd-06926162e2e6',
2020
scrapCount: 89,
2121
isBookmarked: false,
22-
categoryId: 17,
22+
categoryId: 18,
2323
hashtags: [{id: 8, name: '사진'}],
2424
},
2525
{
@@ -70,29 +70,49 @@ const sampleProducts = [
7070

7171
const CategoryPage = () => {
7272
const searchParams = useSearchParams();
73-
7473
const categoryName = searchParams.get('view');
7574

7675
const [selectedCategory, setSelectedCategory] = useState<number | null>(null);
76+
const [isLoading, setIsLoading] = useState(true);
7777

78-
// 상위 카테고리 찾기
7978
const currentCategory = categoryName
80-
? categories.find(category => category.name === categoryName) // 상위 카테고리를 찾음
81-
: null; // 이미 상위 카테고리인 경우
79+
? categories.find(category => category.name === categoryName)
80+
: null;
81+
82+
let filteredCategories = currentCategory
83+
? categories.filter(category => category.parentId === currentCategory.id)
84+
: categories.filter(category => category.parentId === null);
85+
86+
filteredCategories = [
87+
{
88+
id: 0,
89+
name: '전체',
90+
parentId: currentCategory?.id || null,
91+
depth: 0,
92+
},
93+
...filteredCategories,
94+
];
8295

83-
// 상위 카테고리가 존재하면 해당하는 하위 카테고리 필터링
84-
const filteredCategories = currentCategory
85-
? categories.filter(category => category.parentId === currentCategory?.id) // 하위 카테고리 필터링
86-
: categories.filter(category => category.parentId === null); // 상위 카테고리 필터링
96+
useEffect(() => {
97+
if (currentCategory) {
98+
setSelectedCategory(0);
99+
}
100+
setTimeout(() => setIsLoading(false), 1000); // 로딩 시뮬레이션
101+
}, [currentCategory]);
87102

88-
const filteredProducts = selectedCategory
89-
? sampleProducts.filter(
90-
sampleProducts => sampleProducts.categoryId === selectedCategory,
91-
)
92-
: [];
103+
const filteredProducts =
104+
selectedCategory === 0
105+
? sampleProducts.filter(product =>
106+
filteredCategories.some(
107+
cat => cat.id !== 0 && cat.id === product.categoryId,
108+
),
109+
)
110+
: sampleProducts.filter(
111+
product => product.categoryId === selectedCategory,
112+
);
93113

94114
const handleSelectCategory = (category: CategoryItem) => {
95-
setSelectedCategory(category.id); // 선택된 카테고리 상태만 업데이트
115+
setSelectedCategory(category.id);
96116
};
97117

98118
return (
@@ -128,34 +148,35 @@ const CategoryPage = () => {
128148
<CategoryList categories={filteredCategories} />
129149
</section>
130150
)}
131-
{selectedCategory && (
132-
<section className='p-4'>
133-
<div
134-
className='grid justify-center gap-[20px]'
135-
style={{
136-
gridTemplateColumns: 'repeat(auto-fit, 160px)',
137-
}}
138-
>
139-
{filteredProducts.length > 0 ? (
140-
filteredProducts.map(product => (
141-
<RaffleCard
142-
key={product.id}
143-
name={product.name}
144-
thumbnailUrl={product.thumbnailUrl}
145-
price={product.price.toString()}
146-
scrapCount={product.scrapCount}
147-
isBookmarked={product.isBookmarked}
148-
hashtags={product.hashtags}
149-
/>
150-
))
151-
) : (
152-
<Typography size='h5' className='text-gray-500'>
153-
상품 추가 예정입니다.
154-
</Typography>
155-
)}
156-
</div>
157-
</section>
158-
)}
151+
152+
<section className='p-4'>
153+
<div
154+
className='grid justify-center gap-[20px]'
155+
style={{gridTemplateColumns: 'repeat(auto-fit, 160px)'}}
156+
>
157+
{isLoading ? (
158+
<Typography size='h5' className='text-gray-500'>
159+
로딩 중...
160+
</Typography>
161+
) : filteredProducts.length > 0 ? (
162+
filteredProducts.map(product => (
163+
<RaffleCard
164+
key={product.id}
165+
name={product.name}
166+
thumbnailUrl={product.thumbnailUrl}
167+
price={product.price.toString()}
168+
scrapCount={product.scrapCount}
169+
isBookmarked={product.isBookmarked}
170+
hashtags={product.hashtags}
171+
/>
172+
))
173+
) : (
174+
<Typography size='h5' className='text-gray-500'>
175+
상품 추가 예정입니다.
176+
</Typography>
177+
)}
178+
</div>
179+
</section>
159180
<BottomNavigation />
160181
</div>
161182
);

0 commit comments

Comments
 (0)