Skip to content

Commit ff55404

Browse files
committed
Fix color
1 parent 46d1a49 commit ff55404

File tree

3 files changed

+36
-31
lines changed

3 files changed

+36
-31
lines changed

src/components/Product/ProductFilters.component.tsx

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { Dispatch, SetStateAction } from 'react';
2-
32
import { Product, ProductType } from '@/types/product';
43

54
interface 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
);

src/types/product.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export interface ProductCategory {
1717

1818
export interface ColorNode {
1919
name: string;
20+
slug: string;
2021
}
2122

2223
export interface SizeNode {

src/utils/gql/GQL_QUERIES.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ export const FETCH_ALL_PRODUCTS_QUERY = gql`
137137
allPaColors {
138138
nodes {
139139
name
140+
slug
140141
}
141142
}
142143
allPaSizes {

0 commit comments

Comments
 (0)