Skip to content

Commit eb0389c

Browse files
committed
Reset filter button
1 parent 1a4c11e commit eb0389c

File tree

4 files changed

+104
-84
lines changed

4 files changed

+104
-84
lines changed

src/components/Product/ProductFilters.component.tsx

+10-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ interface ProductFiltersProps {
2929
productTypes: ProductType[];
3030
toggleProductType: (id: string) => void;
3131
products: Product[];
32+
resetFilters: () => void;
3233
}
3334

3435
const ProductFilters = ({
@@ -41,6 +42,7 @@ const ProductFilters = ({
4142
productTypes,
4243
toggleProductType,
4344
products,
45+
resetFilters,
4446
}: ProductFiltersProps) => {
4547
// Get unique sizes from all products
4648
const sizes = Array.from(new Set(
@@ -156,10 +158,17 @@ const ProductFilters = ({
156158
title={color.name}
157159
/>
158160
))}
159-
</div>
160161
</div>
162+
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>
161169
</div>
162170
</div>
171+
</div>
163172
);
164173
};
165174

src/pages/products2.tsx

+5-83
Original file line numberDiff line numberDiff line change
@@ -7,77 +7,8 @@ import client from '@/utils/apollo/ApolloClient';
77
import { FETCH_ALL_PRODUCTS_QUERY } from '@/utils/gql/GQL_QUERIES';
88
import type { NextPage, GetStaticProps, InferGetStaticPropsType } from 'next';
99

10-
interface Image {
11-
__typename: string;
12-
sourceUrl?: string;
13-
}
14-
15-
interface Node {
16-
__typename: string;
17-
price: string;
18-
regularPrice: string;
19-
salePrice?: string;
20-
}
21-
22-
interface Variations {
23-
__typename: string;
24-
nodes: Node[];
25-
}
26-
27-
interface ProductCategory {
28-
name: string;
29-
slug: string;
30-
}
31-
32-
interface ColorNode {
33-
name: string;
34-
}
35-
36-
interface SizeNode {
37-
name: string;
38-
}
39-
40-
interface AttributeNode {
41-
name: string;
42-
value: string;
43-
}
44-
45-
interface Product {
46-
__typename: string;
47-
databaseId: number;
48-
name: string;
49-
onSale: boolean;
50-
slug: string;
51-
image: Image;
52-
price: string;
53-
regularPrice: string;
54-
salePrice?: string;
55-
productCategories?: {
56-
nodes: ProductCategory[];
57-
};
58-
allPaColors?: {
59-
nodes: ColorNode[];
60-
};
61-
allPaSizes?: {
62-
nodes: SizeNode[];
63-
};
64-
variations: {
65-
nodes: Array<{
66-
price: string;
67-
regularPrice: string;
68-
salePrice?: string;
69-
attributes?: {
70-
nodes: AttributeNode[];
71-
};
72-
}>;
73-
};
74-
}
75-
76-
interface ProductType {
77-
id: string;
78-
name: string;
79-
checked: boolean;
80-
}
10+
import { Product, ProductCategory, ProductType, SizeNode, ColorNode } from '@/types/product';
11+
import { getUniqueProductTypes } from '@/utils/functions/productUtils';
8112

8213
const Products2: NextPage = ({
8314
products,
@@ -87,18 +18,9 @@ const Products2: NextPage = ({
8718
const [selectedSizes, setSelectedSizes] = useState<string[]>([]);
8819
const [selectedColors, setSelectedColors] = useState<string[]>([]);
8920
const [priceRange, setPriceRange] = useState<[number, number]>([0, 1000]);
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);
21+
const [productTypes, setProductTypes] = useState<ProductType[]>(() =>
22+
products ? getUniqueProductTypes(products) : []
23+
);
10224

10325
const toggleProductType = (id: string) => {
10426
setProductTypes(prev => prev.map(type =>

src/types/product.ts

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
export interface Image {
2+
__typename: string;
3+
sourceUrl?: string;
4+
}
5+
6+
export interface Node {
7+
__typename: string;
8+
price: string;
9+
regularPrice: string;
10+
salePrice?: string;
11+
}
12+
13+
export interface ProductCategory {
14+
name: string;
15+
slug: string;
16+
}
17+
18+
export interface ColorNode {
19+
name: string;
20+
}
21+
22+
export interface SizeNode {
23+
name: string;
24+
}
25+
26+
export interface AttributeNode {
27+
name: string;
28+
value: string;
29+
}
30+
31+
export interface Product {
32+
__typename: string;
33+
databaseId: number;
34+
name: string;
35+
onSale: boolean;
36+
slug: string;
37+
image: Image;
38+
price: string;
39+
regularPrice: string;
40+
salePrice?: string;
41+
productCategories?: {
42+
nodes: ProductCategory[];
43+
};
44+
allPaColors?: {
45+
nodes: ColorNode[];
46+
};
47+
allPaSizes?: {
48+
nodes: SizeNode[];
49+
};
50+
variations: {
51+
nodes: Array<{
52+
price: string;
53+
regularPrice: string;
54+
salePrice?: string;
55+
attributes?: {
56+
nodes: AttributeNode[];
57+
};
58+
}>;
59+
};
60+
}
61+
62+
export interface ProductType {
63+
id: string;
64+
name: string;
65+
checked: boolean;
66+
}

src/utils/functions/productUtils.ts

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { Product, ProductCategory, ProductType } from '@/types/product';
2+
3+
export const getUniqueProductTypes = (products: Product[]): ProductType[] => {
4+
// Use Map to ensure unique categories by slug
5+
const categoryMap = new Map<string, ProductType>();
6+
7+
products?.forEach((product) => {
8+
product.productCategories?.nodes.forEach((cat: ProductCategory) => {
9+
if (!categoryMap.has(cat.slug)) {
10+
categoryMap.set(cat.slug, {
11+
id: cat.slug,
12+
name: cat.name,
13+
checked: false,
14+
});
15+
}
16+
});
17+
});
18+
19+
// Convert Map values to array and sort by name
20+
return Array.from(categoryMap.values()).sort((a, b) =>
21+
a.name.localeCompare(b.name),
22+
);
23+
};

0 commit comments

Comments
 (0)