Skip to content

Commit 8e5f7a6

Browse files
committed
Avoid hardcoded color filter
1 parent b0064ce commit 8e5f7a6

File tree

2 files changed

+35
-10
lines changed

2 files changed

+35
-10
lines changed

src/components/Product/ProductCard.component.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,18 @@ interface ProductCardProps {
1212
};
1313
}
1414

15-
const ProductCard = ({ databaseId, name, price, slug, image }: ProductCardProps) => {
15+
const ProductCard = ({
16+
databaseId,
17+
name,
18+
price,
19+
slug,
20+
image,
21+
}: ProductCardProps) => {
1622
const [isFavorite, setIsFavorite] = useState(false);
1723

1824
return (
1925
<div className="group">
20-
<div className="aspect-[3/4] relative overflow-hidden bg-gray-100">
26+
<div className="aspect-[3/4] overflow-hidden bg-gray-100 relative">
2127
<button
2228
onClick={() => setIsFavorite(!isFavorite)}
2329
className="absolute right-4 top-4 z-10 rounded-full bg-white p-2 hover:scale-110 transition-transform duration-200"
@@ -45,6 +51,7 @@ const ProductCard = ({ databaseId, name, price, slug, image }: ProductCardProps)
4551
alt={name}
4652
fill
4753
className="w-full h-full object-cover object-center transition duration-300 group-hover:scale-105"
54+
priority={databaseId === 1}
4855
sizes="(max-width: 640px) 100vw, (max-width: 1024px) 50vw, 25vw"
4956
/>
5057
) : (

src/components/Product/ProductFilters.component.tsx

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ interface Product {
1212
name: string;
1313
}[];
1414
};
15+
allPaColors?: {
16+
nodes: {
17+
name: string;
18+
}[];
19+
};
1520
}
1621

1722
interface ProductFiltersProps {
@@ -43,14 +48,27 @@ const ProductFilters = ({
4348
product.allPaSizes?.nodes.map((node: { name: string }) => node.name) || []
4449
)
4550
)).sort() as string[];
46-
const colors = [
47-
{ name: 'Svart', class: 'bg-black' },
48-
{ name: 'Brun', class: 'bg-brown-500' },
49-
{ name: 'Beige', class: 'bg-[#D2B48C]' },
50-
{ name: 'Grå', class: 'bg-gray-500' },
51-
{ name: 'Hvit', class: 'bg-white border border-gray-300' },
52-
{ name: 'Blå', class: 'bg-blue-500' }
53-
];
51+
// Get unique colors from all products
52+
const availableColors = Array.from(new Set(
53+
products.flatMap((product: Product) =>
54+
product.allPaColors?.nodes.map((node: { name: string }) => node.name) || []
55+
)
56+
)).sort() as string[];
57+
58+
// Map color names to their CSS classes
59+
const colorMap: { [key: string]: string } = {
60+
'Svart': 'bg-black',
61+
'Brun': 'bg-brown-500',
62+
'Beige': 'bg-[#D2B48C]',
63+
'Grå': 'bg-gray-500',
64+
'Hvit': 'bg-white border border-gray-300',
65+
'Blå': 'bg-blue-500'
66+
};
67+
68+
const colors = availableColors.map(colorName => ({
69+
name: colorName,
70+
class: colorMap[colorName] || 'bg-gray-300' // Fallback color if not in map
71+
}));
5472

5573
const toggleSize = (size: string) => {
5674
setSelectedSizes(prev =>

0 commit comments

Comments
 (0)