11import { Dispatch , SetStateAction } from 'react' ;
2-
32import { Product , ProductType } from '@/types/product' ;
43
54interface ProductFiltersProps {
@@ -38,31 +37,33 @@ const ProductFilters = ({
3837 ) ,
3938 ) ,
4039 ) . sort ( ) as string [ ] ;
40+
4141 // 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 ) ) ;
5248
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+ }
6162 } ;
6263
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 )
6667 } ) ) ;
6768
6869 const toggleSize = ( size : string ) => {
@@ -134,14 +135,16 @@ const ProductFilters = ({
134135 </ div >
135136 </ div >
136137
137- < div >
138+ < div className = "mb-8" >
138139 < h3 className = "font-semibold mb-4" > FARGE</ h3 >
139140 < div className = "grid grid-cols-3 gap-2" >
140141 { colors . map ( ( color ) => (
141142 < button
142143 key = { color . name }
143144 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+ } ${
145148 selectedColors . includes ( color . name )
146149 ? 'ring-2 ring-offset-2 ring-gray-900'
147150 : ''
@@ -150,14 +153,14 @@ const ProductFilters = ({
150153 />
151154 ) ) }
152155 </ 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 >
160156 </ 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 >
161164 </ div >
162165 </ div >
163166 ) ;
0 commit comments