|
@@ -1,26 +1,121 @@
|
1
|
|
-import React, { useEffect } from 'react';
|
2
|
|
-import { Box, Typography, Button } from '@mui/material';
|
|
1
|
+import { useEffect, useState } from 'react';
|
|
2
|
+import { Box, Typography, Button, FormControl, Select, MenuItem, InputBase } from '@mui/material';
|
3
|
3
|
import Grid from '@mui/material/Grid2';
|
4
|
|
-
|
|
4
|
+import { styled } from "@mui/material";
|
5
|
5
|
//REDUX
|
6
|
6
|
import { useSelector, useDispatch } from 'react-redux';
|
7
|
|
-import { fetchProducts } from '../../redux/slices/productSlice';
|
|
7
|
+import { fetchProducts, fetchProductsByCollection } from '../../redux/slices/productSlice';
|
|
8
|
+
|
|
9
|
+//UTIL FUNCTION
|
|
10
|
+function getAllTags(data) {
|
|
11
|
+ const products = data || [];
|
|
12
|
+ const allTags = products.flatMap(product => product.tags);
|
|
13
|
+ const uniqueTags = [...new Set(allTags)];
|
|
14
|
+ return uniqueTags;
|
|
15
|
+}
|
|
16
|
+
|
|
17
|
+const BootstrapInput = styled(InputBase)(({ theme }) => ({
|
|
18
|
+ 'label + &': {
|
|
19
|
+ marginTop: theme.spacing(3),
|
|
20
|
+ },
|
|
21
|
+ '& .MuiInputBase-input': {
|
|
22
|
+ position: 'relative',
|
|
23
|
+ backgroundColor: "#2E2E2E",
|
|
24
|
+ border: '1px solid #ced4da',
|
|
25
|
+ color: "#FFF",
|
|
26
|
+ fontSize: 13,
|
|
27
|
+ padding: '5px 0',
|
|
28
|
+ paddingRight: '50px !important',
|
|
29
|
+ paddingLeft: "10px",
|
|
30
|
+ transition: theme.transitions.create(['border-color', 'box-shadow']),
|
|
31
|
+ '&:focus': {
|
|
32
|
+ borderRadius: 4,
|
|
33
|
+ borderColor: '#80bdff',
|
|
34
|
+ boxShadow: '0 0 0 0.2rem rgba(0,123,255,.25)',
|
|
35
|
+ },
|
|
36
|
+ },
|
|
37
|
+ '& .MuiSvgIcon-root': {
|
|
38
|
+ color: "#FFF !important"
|
|
39
|
+ },
|
|
40
|
+}));
|
8
|
41
|
|
9
|
42
|
const ProductList = ({ size = 99999 }) => {
|
10
|
43
|
|
11
|
|
- const products = useSelector((state) => state.products.products.data)
|
|
44
|
+ const products = useSelector((state) => state.products.products.data) // only used as referenced
|
|
45
|
+ const [filteredProducts, setFilteredProducts] = useState([]) // this one is the actual data to be rendered
|
|
46
|
+ const [categoryFilterOption, setCategoryFilterOption] = useState([])
|
12
|
47
|
const dispatch = useDispatch();
|
13
|
48
|
|
|
49
|
+ //filter
|
|
50
|
+ const [category, setCategory] = useState('all');
|
|
51
|
+ const [sort, setSort] = useState('all')
|
|
52
|
+
|
14
|
53
|
useEffect(() => {
|
15
|
54
|
|
16
|
|
- dispatch(fetchProducts())
|
|
55
|
+ // if user come from select collection
|
|
56
|
+ if (sessionStorage.getItem('amber-select-collection')) {
|
|
57
|
+
|
|
58
|
+ let { id } = JSON.parse(sessionStorage.getItem('amber-select-collection'))
|
|
59
|
+ dispatch(fetchProductsByCollection({ collectionId: id }))
|
|
60
|
+
|
|
61
|
+ } else if (sessionStorage.getItem('amber-select-category')) {
|
|
62
|
+
|
|
63
|
+ }
|
17
|
64
|
|
18
|
65
|
}, [])
|
19
|
66
|
|
20
|
67
|
useEffect(() => {
|
21
|
|
- if(products) console.log(products)
|
|
68
|
+
|
|
69
|
+ let productType = sessionStorage.getItem('amber-select-product-type')
|
|
70
|
+ let newFilteredProducts = products.filter(
|
|
71
|
+ (product) => product.productType === productType
|
|
72
|
+ );
|
|
73
|
+
|
|
74
|
+ const categoryList = getAllTags(newFilteredProducts) // yes we use tags as category, while I was coding this, this is the only way to implement category
|
|
75
|
+
|
|
76
|
+ setFilteredProducts(newFilteredProducts)
|
|
77
|
+ setCategoryFilterOption(categoryList)
|
|
78
|
+
|
22
|
79
|
}, [products])
|
23
|
80
|
|
|
81
|
+
|
|
82
|
+ useEffect(() => {
|
|
83
|
+
|
|
84
|
+ if (products?.length > 0) {
|
|
85
|
+
|
|
86
|
+ let productType = sessionStorage.getItem('amber-select-product-type')
|
|
87
|
+
|
|
88
|
+ let newFilteredProducts = products.filter(
|
|
89
|
+ (product) => {
|
|
90
|
+
|
|
91
|
+ if(category == 'all'){
|
|
92
|
+ return product.productType === productType
|
|
93
|
+ } else {
|
|
94
|
+ return product.productType === productType && product.tags.includes(category)
|
|
95
|
+ }
|
|
96
|
+
|
|
97
|
+ }
|
|
98
|
+ );
|
|
99
|
+
|
|
100
|
+ setFilteredProducts(newFilteredProducts)
|
|
101
|
+
|
|
102
|
+ }
|
|
103
|
+
|
|
104
|
+ }, [category])
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+ useEffect(() => {
|
|
109
|
+ if (products) console.log(products)
|
|
110
|
+ }, [products])
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+ const handleChange = (event) => {
|
|
116
|
+ //setInput({ ...input, [event.target.name]: event.target.value });
|
|
117
|
+ };
|
|
118
|
+
|
24
|
119
|
const renderProduct = (id, img_url, title, collection, price, currency, extra_desc) => {
|
25
|
120
|
|
26
|
121
|
return (
|
|
@@ -41,7 +136,7 @@ const ProductList = ({ size = 99999 }) => {
|
41
|
136
|
width: '100%',
|
42
|
137
|
aspectRatio: '3 / 4',
|
43
|
138
|
objectFit: 'cover',
|
44
|
|
- objectPosition:'top center'
|
|
139
|
+ objectPosition: 'top center'
|
45
|
140
|
}}
|
46
|
141
|
/>
|
47
|
142
|
|
|
@@ -70,28 +165,83 @@ const ProductList = ({ size = 99999 }) => {
|
70
|
165
|
}
|
71
|
166
|
|
72
|
167
|
return (
|
73
|
|
- <Box sx={{ mb: 5 }}>
|
74
|
|
- <Grid container spacing={1} columns={12}>
|
75
|
|
- {products.map((product, index) => {
|
76
|
|
-
|
77
|
|
- let {id, title, compareAtPriceRange, images, collections} = product
|
78
|
|
- let price = compareAtPriceRange.maxVariantPrice.amount
|
79
|
|
- let currency = compareAtPriceRange.maxVariantPrice.currencyCode
|
80
|
|
- let img_url = images.nodes[0].url
|
81
|
|
- let collection_name = collections.nodes[0]?.title
|
82
|
|
-
|
83
|
|
- // ID
|
84
|
|
- const parts = id.split('/');
|
85
|
|
- let prodID = parts[parts.length - 1];
|
86
|
|
-
|
87
|
|
-
|
88
|
|
- if (index < size) {
|
89
|
|
- return renderProduct(prodID, img_url, title, collection_name, price, currency, "" )
|
90
|
|
- }
|
|
168
|
+ <>
|
|
169
|
+ {/* FILTER */}
|
|
170
|
+ <Box
|
|
171
|
+ sx={{
|
|
172
|
+ display: "flex",
|
|
173
|
+ justifyContent: "space-between",
|
|
174
|
+ alignItems: "center",
|
|
175
|
+ backgroundColor: "background.black",
|
|
176
|
+ color: "white",
|
|
177
|
+ px: 2, // Add padding around the box
|
|
178
|
+ my: 4
|
|
179
|
+ }}
|
|
180
|
+ >
|
|
181
|
+ {/* Left Side: Page Title */}
|
|
182
|
+ <Typography variant="body2">
|
|
183
|
+ {`${filteredProducts.length} Item`}
|
|
184
|
+ </Typography>
|
|
185
|
+
|
|
186
|
+ {/* Right Side: Option Inputs */}
|
|
187
|
+ <Box sx={{ display: "flex", gap: 2 }}>
|
|
188
|
+
|
|
189
|
+ <FormControl sx={{ m: 1, display: "flex", flexDirection: "row" }} variant="standard">
|
|
190
|
+ <Typography variant="body2" sx={{ mr: 1, my: "auto" }}>Filter By Category</Typography>
|
|
191
|
+ <Select
|
|
192
|
+ value={category}
|
|
193
|
+ onChange={(event) => {
|
|
194
|
+ setCategory(event.target.value);
|
|
195
|
+ }}
|
|
196
|
+ input={<BootstrapInput />}
|
|
197
|
+ name="type"
|
|
198
|
+ >
|
|
199
|
+ <MenuItem value={'all'}>All</MenuItem>
|
|
200
|
+ {categoryFilterOption?.map((data) => (<MenuItem value={data}>{data}</MenuItem>))}
|
|
201
|
+ </Select>
|
|
202
|
+ </FormControl>
|
|
203
|
+
|
|
204
|
+ <FormControl sx={{ m: 1, display: { xs: "none", sm: "none", md: "flex" }, flexDirection: "row" }} variant="standard">
|
|
205
|
+ <Typography variant="body2" sx={{ mr: 1, my: "auto" }}>Sort By</Typography>
|
|
206
|
+ <Select
|
|
207
|
+ value={sort}
|
|
208
|
+ onChange={handleChange}
|
|
209
|
+ input={<BootstrapInput />}
|
|
210
|
+ name="sort"
|
|
211
|
+ >
|
|
212
|
+ <MenuItem defaultValue value={''}>All</MenuItem>
|
|
213
|
+ <MenuItem defaultValue value={'all'}>Newest</MenuItem>
|
|
214
|
+
|
|
215
|
+ </Select>
|
|
216
|
+ </FormControl>
|
91
|
217
|
|
92
|
|
- })}
|
93
|
|
- </Grid>
|
94
|
|
- </Box>
|
|
218
|
+ </Box>
|
|
219
|
+ </Box>
|
|
220
|
+
|
|
221
|
+ {/* LIST */}
|
|
222
|
+ <Box sx={{ mb: 5 }}>
|
|
223
|
+ <Grid container spacing={1} columns={12}>
|
|
224
|
+ {filteredProducts.map((product, index) => {
|
|
225
|
+
|
|
226
|
+ let { id, title, compareAtPriceRange, images, collections } = product
|
|
227
|
+ let price = compareAtPriceRange.maxVariantPrice.amount
|
|
228
|
+ let currency = compareAtPriceRange.maxVariantPrice.currencyCode
|
|
229
|
+ let img_url = images.nodes[0].url
|
|
230
|
+ let collection_name = collections.nodes[0]?.title
|
|
231
|
+
|
|
232
|
+ // ID
|
|
233
|
+ const parts = id.split('/');
|
|
234
|
+ let prodID = parts[parts.length - 1];
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+ if (index < size) {
|
|
238
|
+ return renderProduct(prodID, img_url, title, collection_name, price, currency, "")
|
|
239
|
+ }
|
|
240
|
+
|
|
241
|
+ })}
|
|
242
|
+ </Grid>
|
|
243
|
+ </Box>
|
|
244
|
+ </>
|
95
|
245
|
);
|
96
|
246
|
};
|
97
|
247
|
|