1
1
'use client' ;
2
2
3
3
import { useSearchParams } from 'next/navigation' ;
4
- import { useState } from 'react' ;
4
+ import { useEffect , useState } from 'react' ;
5
5
import { categories } from '@/entities/category' ;
6
6
import type { CategoryItem } from '@/entities/category/type' ;
7
7
import { Header } from '@/shared/ui' ;
@@ -13,13 +13,13 @@ import {RaffleCard} from '@wraffle/ui';
13
13
const sampleProducts = [
14
14
{
15
15
id : 2 ,
16
- name : '럭셔리 시계 경품 래플 ' ,
16
+ name : '이거 삼성 상품임 ' ,
17
17
price : 10000 ,
18
18
thumbnailUrl :
19
19
'https://github.com/user-attachments/assets/4a104905-0106-4b8a-8dcd-06926162e2e6' ,
20
20
scrapCount : 89 ,
21
21
isBookmarked : false ,
22
- categoryId : 17 ,
22
+ categoryId : 18 ,
23
23
hashtags : [ { id : 8 , name : '사진' } ] ,
24
24
} ,
25
25
{
@@ -70,29 +70,49 @@ const sampleProducts = [
70
70
71
71
const CategoryPage = ( ) => {
72
72
const searchParams = useSearchParams ( ) ;
73
-
74
73
const categoryName = searchParams . get ( 'view' ) ;
75
74
76
75
const [ selectedCategory , setSelectedCategory ] = useState < number | null > ( null ) ;
76
+ const [ isLoading , setIsLoading ] = useState ( true ) ;
77
77
78
- // 상위 카테고리 찾기
79
78
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
+ ] ;
82
95
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 ] ) ;
87
102
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
+ ) ;
93
113
94
114
const handleSelectCategory = ( category : CategoryItem ) => {
95
- setSelectedCategory ( category . id ) ; // 선택된 카테고리 상태만 업데이트
115
+ setSelectedCategory ( category . id ) ;
96
116
} ;
97
117
98
118
return (
@@ -128,34 +148,35 @@ const CategoryPage = () => {
128
148
< CategoryList categories = { filteredCategories } />
129
149
</ section >
130
150
) }
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 >
159
180
< BottomNavigation />
160
181
</ div >
161
182
) ;
0 commit comments