Skip to content

Commit 1a4c11e

Browse files
committed
Dynamic product type
1 parent 8e5f7a6 commit 1a4c11e

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

src/pages/products2.tsx

+21-7
Original file line numberDiff line numberDiff line change
@@ -87,19 +87,32 @@ const Products2: NextPage = ({
8787
const [selectedSizes, setSelectedSizes] = useState<string[]>([]);
8888
const [selectedColors, setSelectedColors] = useState<string[]>([]);
8989
const [priceRange, setPriceRange] = useState<[number, number]>([0, 1000]);
90-
const [productTypes, setProductTypes] = useState<ProductType[]>([
91-
{ id: 't-shirts', name: 'T-Shirts', checked: false },
92-
{ id: 'gensere', name: 'Gensere', checked: false },
93-
{ id: 'singlet', name: 'Singlet', checked: false },
94-
{ id: 'skjorter', name: 'Skjorter', checked: false }
95-
]);
90+
// Get unique product types from products
91+
const initialProductTypes = Array.from(new Set(
92+
products?.flatMap(product =>
93+
product.productCategories?.nodes.map(cat => ({
94+
id: cat.slug,
95+
name: cat.name,
96+
checked: false
97+
})) || []
98+
) || []
99+
)).sort((a, b) => a.name.localeCompare(b.name));
100+
101+
const [productTypes, setProductTypes] = useState<ProductType[]>(initialProductTypes);
96102

97103
const toggleProductType = (id: string) => {
98104
setProductTypes(prev => prev.map(type =>
99105
type.id === id ? { ...type, checked: !type.checked } : type
100106
));
101107
};
102108

109+
const resetFilters = () => {
110+
setSelectedSizes([]);
111+
setSelectedColors([]);
112+
setPriceRange([0, 1000]);
113+
setProductTypes(prev => prev.map(type => ({ ...type, checked: false })));
114+
};
115+
103116
// Filter products based on selected filters
104117
const filteredProducts = products?.filter((product: Product) => {
105118
// Filter by price
@@ -180,13 +193,14 @@ const Products2: NextPage = ({
180193
productTypes={productTypes}
181194
toggleProductType={toggleProductType}
182195
products={products}
196+
resetFilters={resetFilters}
183197
/>
184198

185199
{/* Main Content */}
186200
<div className="flex-1">
187201
<div className="flex justify-between items-center mb-8">
188202
<h1 className="text-2xl font-medium">
189-
Herreklær <span className="text-gray-500">({products.length})</span>
203+
Herreklær <span className="text-gray-500">({sortedProducts.length})</span>
190204
</h1>
191205

192206
<div className="flex items-center gap-4">

0 commit comments

Comments
 (0)