@@ -87,19 +87,32 @@ const Products2: NextPage = ({
87
87
const [ selectedSizes , setSelectedSizes ] = useState < string [ ] > ( [ ] ) ;
88
88
const [ selectedColors , setSelectedColors ] = useState < string [ ] > ( [ ] ) ;
89
89
const [ priceRange , setPriceRange ] = useState < [ number , number ] > ( [ 0 , 1000 ] ) ;
90
- const [ productTypes , setProductTypes ] = useState < ProductType [ ] > ( [
91
- { id : 't-shirts' , name : 'T-Shirts' , checked : false } ,
92
- { id : 'gensere' , name : 'Gensere' , checked : false } ,
93
- { id : 'singlet' , name : 'Singlet' , checked : false } ,
94
- { id : 'skjorter' , name : 'Skjorter' , checked : false }
95
- ] ) ;
90
+ // Get unique product types from products
91
+ const initialProductTypes = Array . from ( new Set (
92
+ products ?. flatMap ( product =>
93
+ product . productCategories ?. nodes . map ( cat => ( {
94
+ id : cat . slug ,
95
+ name : cat . name ,
96
+ checked : false
97
+ } ) ) || [ ]
98
+ ) || [ ]
99
+ ) ) . sort ( ( a , b ) => a . name . localeCompare ( b . name ) ) ;
100
+
101
+ const [ productTypes , setProductTypes ] = useState < ProductType [ ] > ( initialProductTypes ) ;
96
102
97
103
const toggleProductType = ( id : string ) => {
98
104
setProductTypes ( prev => prev . map ( type =>
99
105
type . id === id ? { ...type , checked : ! type . checked } : type
100
106
) ) ;
101
107
} ;
102
108
109
+ const resetFilters = ( ) => {
110
+ setSelectedSizes ( [ ] ) ;
111
+ setSelectedColors ( [ ] ) ;
112
+ setPriceRange ( [ 0 , 1000 ] ) ;
113
+ setProductTypes ( prev => prev . map ( type => ( { ...type , checked : false } ) ) ) ;
114
+ } ;
115
+
103
116
// Filter products based on selected filters
104
117
const filteredProducts = products ?. filter ( ( product : Product ) => {
105
118
// Filter by price
@@ -180,13 +193,14 @@ const Products2: NextPage = ({
180
193
productTypes = { productTypes }
181
194
toggleProductType = { toggleProductType }
182
195
products = { products }
196
+ resetFilters = { resetFilters }
183
197
/>
184
198
185
199
{ /* Main Content */ }
186
200
< div className = "flex-1" >
187
201
< div className = "flex justify-between items-center mb-8" >
188
202
< h1 className = "text-2xl font-medium" >
189
- Herreklær < span className = "text-gray-500" > ({ products . length } )</ span >
203
+ Herreklær < span className = "text-gray-500" > ({ sortedProducts . length } )</ span >
190
204
</ h1 >
191
205
192
206
< div className = "flex items-center gap-4" >
0 commit comments