1
1
import { Dispatch , SetStateAction } from 'react' ;
2
-
3
2
import { Product , ProductType } from '@/types/product' ;
4
3
5
4
interface ProductFiltersProps {
@@ -38,31 +37,33 @@ const ProductFilters = ({
38
37
) ,
39
38
) ,
40
39
) . sort ( ) as string [ ] ;
40
+
41
41
// Get unique colors from all products
42
- const availableColors = Array . from (
43
- new Set (
44
- products . flatMap (
45
- ( product : Product ) =>
46
- product . allPaColors ?. nodes . map (
47
- ( node : { name : string } ) => node . name ,
48
- ) || [ ] ,
49
- ) ,
50
- ) ,
51
- ) . sort ( ) as string [ ] ;
42
+ const availableColors = products
43
+ . flatMap ( ( product : Product ) => product . allPaColors ?. nodes || [ ] )
44
+ . filter ( ( color , index , self ) =>
45
+ index === self . findIndex ( ( c ) => c . slug === color . slug )
46
+ )
47
+ . sort ( ( a , b ) => a . name . localeCompare ( b . name ) ) ;
52
48
53
- // Map color names to their CSS classes
54
- const colorMap : { [ key : string ] : string } = {
55
- Svart : 'bg-black' ,
56
- Brun : 'bg-brown-500' ,
57
- Beige : 'bg-[#D2B48C]' ,
58
- Grå : 'bg-gray-500' ,
59
- Hvit : 'bg-white border border-gray-300' ,
60
- Blå : 'bg-blue-500' ,
49
+ const getColorClass = ( slug : string ) => {
50
+ switch ( slug . toLowerCase ( ) ) {
51
+ case 'blue' :
52
+ return 'bg-blue-500' ;
53
+ case 'grey' :
54
+ return 'bg-gray-500' ;
55
+ case 'red' :
56
+ return 'bg-red-500' ;
57
+ case 'green' :
58
+ return 'bg-green-500' ;
59
+ default :
60
+ return 'bg-gray-100 border border-gray-300' ;
61
+ }
61
62
} ;
62
63
63
- const colors = availableColors . map ( ( colorName ) => ( {
64
- name : colorName ,
65
- class : colorMap [ colorName ] || 'bg-gray-300' , // Fallback color if not in map
64
+ const colors = availableColors . map ( ( color ) => ( {
65
+ name : color . name ,
66
+ class : getColorClass ( color . slug )
66
67
} ) ) ;
67
68
68
69
const toggleSize = ( size : string ) => {
@@ -134,14 +135,16 @@ const ProductFilters = ({
134
135
</ div >
135
136
</ div >
136
137
137
- < div >
138
+ < div className = "mb-8" >
138
139
< h3 className = "font-semibold mb-4" > FARGE</ h3 >
139
140
< div className = "grid grid-cols-3 gap-2" >
140
141
{ colors . map ( ( color ) => (
141
142
< button
142
143
key = { color . name }
143
144
onClick = { ( ) => toggleColor ( color . name ) }
144
- className = { `w-8 h-8 rounded-full ${ color . class } ${
145
+ className = { `w-8 h-8 rounded-full flex items-center justify-center text-xs ${
146
+ color . class
147
+ } ${
145
148
selectedColors . includes ( color . name )
146
149
? 'ring-2 ring-offset-2 ring-gray-900'
147
150
: ''
@@ -150,14 +153,14 @@ const ProductFilters = ({
150
153
/>
151
154
) ) }
152
155
</ div >
153
-
154
- < button
155
- onClick = { resetFilters }
156
- className = "w-full mt-8 py-2 px-4 bg-gray-100 text-gray-700 rounded hover:bg-gray-200 transition-colors"
157
- >
158
- Resett filter
159
- </ button >
160
156
</ div >
157
+
158
+ < button
159
+ onClick = { resetFilters }
160
+ className = "w-full mt-8 py-2 px-4 bg-gray-100 text-gray-700 rounded hover:bg-gray-200 transition-colors"
161
+ >
162
+ Resett filter
163
+ </ button >
161
164
</ div >
162
165
</ div >
163
166
) ;
0 commit comments