|
1 | 1 | import { Dispatch, SetStateAction } from 'react';
|
2 | 2 |
|
3 |
| -interface ProductType { |
4 |
| - id: string; |
5 |
| - name: string; |
6 |
| - checked: boolean; |
7 |
| -} |
8 |
| - |
9 |
| -interface Product { |
10 |
| - allPaSizes?: { |
11 |
| - nodes: { |
12 |
| - name: string; |
13 |
| - }[]; |
14 |
| - }; |
15 |
| - allPaColors?: { |
16 |
| - nodes: { |
17 |
| - name: string; |
18 |
| - }[]; |
19 |
| - }; |
20 |
| -} |
| 3 | +import { Product, ProductType } from '@/types/product'; |
21 | 4 |
|
22 | 5 | interface ProductFiltersProps {
|
23 | 6 | selectedSizes: string[];
|
@@ -45,46 +28,52 @@ const ProductFilters = ({
|
45 | 28 | resetFilters,
|
46 | 29 | }: ProductFiltersProps) => {
|
47 | 30 | // Get unique sizes from all products
|
48 |
| - const sizes = Array.from(new Set( |
49 |
| - products.flatMap((product: Product) => |
50 |
| - product.allPaSizes?.nodes.map((node: { name: string }) => node.name) || [] |
51 |
| - ) |
52 |
| - )).sort() as string[]; |
| 31 | + const sizes = Array.from( |
| 32 | + new Set( |
| 33 | + products.flatMap( |
| 34 | + (product: Product) => |
| 35 | + product.allPaSizes?.nodes.map( |
| 36 | + (node: { name: string }) => node.name, |
| 37 | + ) || [], |
| 38 | + ), |
| 39 | + ), |
| 40 | + ).sort() as string[]; |
53 | 41 | // Get unique colors from all products
|
54 |
| - const availableColors = Array.from(new Set( |
55 |
| - products.flatMap((product: Product) => |
56 |
| - product.allPaColors?.nodes.map((node: { name: string }) => node.name) || [] |
57 |
| - ) |
58 |
| - )).sort() as string[]; |
| 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[]; |
59 | 52 |
|
60 | 53 | // Map color names to their CSS classes
|
61 | 54 | const colorMap: { [key: string]: string } = {
|
62 |
| - 'Svart': 'bg-black', |
63 |
| - 'Brun': 'bg-brown-500', |
64 |
| - 'Beige': 'bg-[#D2B48C]', |
65 |
| - 'Grå': 'bg-gray-500', |
66 |
| - 'Hvit': 'bg-white border border-gray-300', |
67 |
| - 'Blå': 'bg-blue-500' |
| 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', |
68 | 61 | };
|
69 | 62 |
|
70 |
| - const colors = availableColors.map(colorName => ({ |
| 63 | + const colors = availableColors.map((colorName) => ({ |
71 | 64 | name: colorName,
|
72 |
| - class: colorMap[colorName] || 'bg-gray-300' // Fallback color if not in map |
| 65 | + class: colorMap[colorName] || 'bg-gray-300', // Fallback color if not in map |
73 | 66 | }));
|
74 | 67 |
|
75 | 68 | const toggleSize = (size: string) => {
|
76 |
| - setSelectedSizes(prev => |
77 |
| - prev.includes(size) |
78 |
| - ? prev.filter(s => s !== size) |
79 |
| - : [...prev, size] |
| 69 | + setSelectedSizes((prev) => |
| 70 | + prev.includes(size) ? prev.filter((s) => s !== size) : [...prev, size], |
80 | 71 | );
|
81 | 72 | };
|
82 | 73 |
|
83 | 74 | const toggleColor = (color: string) => {
|
84 |
| - setSelectedColors(prev => |
85 |
| - prev.includes(color) |
86 |
| - ? prev.filter(c => c !== color) |
87 |
| - : [...prev, color] |
| 75 | + setSelectedColors((prev) => |
| 76 | + prev.includes(color) ? prev.filter((c) => c !== color) : [...prev, color], |
88 | 77 | );
|
89 | 78 | };
|
90 | 79 |
|
@@ -115,7 +104,9 @@ const ProductFilters = ({
|
115 | 104 | min="0"
|
116 | 105 | max="1000"
|
117 | 106 | value={priceRange[1]}
|
118 |
| - onChange={(e) => setPriceRange([priceRange[0], parseInt(e.target.value)])} |
| 107 | + onChange={(e) => |
| 108 | + setPriceRange([priceRange[0], parseInt(e.target.value)]) |
| 109 | + } |
119 | 110 | className="w-full"
|
120 | 111 | />
|
121 | 112 | <div className="flex justify-between mt-2">
|
@@ -158,17 +149,17 @@ const ProductFilters = ({
|
158 | 149 | title={color.name}
|
159 | 150 | />
|
160 | 151 | ))}
|
161 |
| - </div> |
| 152 | + </div> |
162 | 153 |
|
163 |
| - <button |
164 |
| - onClick={resetFilters} |
165 |
| - className="w-full mt-8 py-2 px-4 bg-gray-100 text-gray-700 rounded hover:bg-gray-200 transition-colors" |
166 |
| - > |
167 |
| - Resett filter |
168 |
| - </button> |
| 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 | + </div> |
169 | 161 | </div>
|
170 | 162 | </div>
|
171 |
| - </div> |
172 | 163 | );
|
173 | 164 | };
|
174 | 165 |
|
|
0 commit comments