|
@@ -1,4 +1,4 @@
|
1
|
|
-import { useState } from "react";
|
|
1
|
+import { useState, useEffect } from "react";
|
2
|
2
|
import {
|
3
|
3
|
Box,
|
4
|
4
|
Typography,
|
|
@@ -13,6 +13,11 @@ import { createCart } from "../../redux/slices/cartSlice";
|
13
|
13
|
// Utility function to check if an object is empty
|
14
|
14
|
const isEmptyObject = (obj) => Object.keys(obj).length === 0 && obj.constructor === Object;
|
15
|
15
|
|
|
16
|
+// Check if every key in the target object matches the source object
|
|
17
|
+const hasMatchingProperties = (source, target) => {
|
|
18
|
+ return Object.entries(target).every(([key, value]) => source[key] === value);
|
|
19
|
+};
|
|
20
|
+
|
16
|
21
|
const ProductDetails = () => {
|
17
|
22
|
|
18
|
23
|
const dispatch = useDispatch()
|
|
@@ -20,7 +25,113 @@ const ProductDetails = () => {
|
20
|
25
|
const cart = useSelector((state) => state.cart.cart)
|
21
|
26
|
|
22
|
27
|
const [quantity, setQuantity] = useState(1)
|
23
|
|
- const [selectedSize, setSelectedSize] = useState(null)
|
|
28
|
+ const [variantSelection, setVariantSelection] = useState({
|
|
29
|
+ price: 0,
|
|
30
|
+ currencyCode: "",
|
|
31
|
+ quantityAvailable: 0
|
|
32
|
+ })
|
|
33
|
+ const [variants, setVariants] = useState([])
|
|
34
|
+
|
|
35
|
+ useEffect(() => {
|
|
36
|
+ if (product) {
|
|
37
|
+
|
|
38
|
+ console.log("Product: ", product)
|
|
39
|
+
|
|
40
|
+ let productVariants = product?.variants?.nodes
|
|
41
|
+
|
|
42
|
+ // get all variant type
|
|
43
|
+ if (!productVariants || productVariants?.length == 0) return
|
|
44
|
+
|
|
45
|
+ // we want to get the title for each variant
|
|
46
|
+ const uniqueOptions = {};
|
|
47
|
+ productVariants.forEach(variant => {
|
|
48
|
+ variant.selectedOptions.forEach(option => {
|
|
49
|
+ if (!uniqueOptions[option.name]) {
|
|
50
|
+ uniqueOptions[option.name] = new Set();
|
|
51
|
+ }
|
|
52
|
+ uniqueOptions[option.name].add(option.value);
|
|
53
|
+ });
|
|
54
|
+ });
|
|
55
|
+
|
|
56
|
+ const VariantsArr = Object.entries(uniqueOptions).map(([key, valueSet]) => ({
|
|
57
|
+ name: key,
|
|
58
|
+ options: Array.from(valueSet),
|
|
59
|
+ }));
|
|
60
|
+
|
|
61
|
+ // get variants value
|
|
62
|
+ setVariants(VariantsArr)
|
|
63
|
+
|
|
64
|
+ // setting Initial value for variants selection
|
|
65
|
+ setVariantSelection((prev) => {
|
|
66
|
+
|
|
67
|
+ let newVariantSelection = { ...prev }
|
|
68
|
+
|
|
69
|
+ // setting inital selection
|
|
70
|
+ VariantsArr.forEach(({ name, options }) => {
|
|
71
|
+ newVariantSelection = { ...newVariantSelection, [name]: options[0] }
|
|
72
|
+ })
|
|
73
|
+
|
|
74
|
+ // find variant price if it all match initial variant selection
|
|
75
|
+ for (const { selectedOptions, price, id, quantityAvailable } of productVariants) {
|
|
76
|
+ let { amount, currencyCode } = price;
|
|
77
|
+
|
|
78
|
+ // Convert array to object
|
|
79
|
+ const optionsObject = selectedOptions.reduce(
|
|
80
|
+ (a, { name, value }) => ({ ...a, [name]: value }),
|
|
81
|
+ {}
|
|
82
|
+ );
|
|
83
|
+
|
|
84
|
+ if (hasMatchingProperties(newVariantSelection, optionsObject)) {
|
|
85
|
+ newVariantSelection = { ...newVariantSelection, amount, currencyCode, id, quantityAvailable };
|
|
86
|
+ break; // Exit the loop when condition is met
|
|
87
|
+ }
|
|
88
|
+ }
|
|
89
|
+
|
|
90
|
+ return newVariantSelection
|
|
91
|
+
|
|
92
|
+ })
|
|
93
|
+
|
|
94
|
+ }
|
|
95
|
+
|
|
96
|
+ }, [product])
|
|
97
|
+
|
|
98
|
+ useEffect(() => {
|
|
99
|
+ console.log("variantSelection: ", variantSelection)
|
|
100
|
+ }, [variantSelection])
|
|
101
|
+
|
|
102
|
+ const handleVariantClick = (name, value) => {
|
|
103
|
+ setVariantSelection({ ...variantSelection, [name]: value })
|
|
104
|
+
|
|
105
|
+ setVariantSelection((prev) => {
|
|
106
|
+
|
|
107
|
+ let newVariantSelection = { ...prev }
|
|
108
|
+ newVariantSelection = { ...newVariantSelection, [name]: value }
|
|
109
|
+ let productVariants = product?.variants?.nodes
|
|
110
|
+
|
|
111
|
+ // find variant price if it all match initial variant selection
|
|
112
|
+ for (const { selectedOptions, price, id, quantityAvailable } of productVariants) {
|
|
113
|
+ let { amount, currencyCode } = price;
|
|
114
|
+
|
|
115
|
+ // Convert array to object
|
|
116
|
+ const optionsObject = selectedOptions.reduce(
|
|
117
|
+ (a, { name, value }) => ({ ...a, [name]: value }),
|
|
118
|
+ {}
|
|
119
|
+ );
|
|
120
|
+
|
|
121
|
+ if (hasMatchingProperties(newVariantSelection, optionsObject)) {
|
|
122
|
+ newVariantSelection = { ...newVariantSelection, amount, currencyCode, id, quantityAvailable };
|
|
123
|
+ if(quantityAvailable == 0) setQuantity(0)
|
|
124
|
+ else setQuantity(1)
|
|
125
|
+ break; // Exit the loop when condition is met
|
|
126
|
+ }
|
|
127
|
+ }
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+ return newVariantSelection
|
|
131
|
+
|
|
132
|
+ })
|
|
133
|
+
|
|
134
|
+ }
|
24
|
135
|
|
25
|
136
|
const handleCart = () => {
|
26
|
137
|
|
|
@@ -31,7 +142,7 @@ const ProductDetails = () => {
|
31
|
142
|
if (isEmptyObject(cart) || isEmptyObject(cartHistory)) {
|
32
|
143
|
|
33
|
144
|
dispatch(createCart());
|
34
|
|
-
|
|
145
|
+
|
35
|
146
|
} else {
|
36
|
147
|
|
37
|
148
|
// // Update cart content
|
|
@@ -62,9 +173,6 @@ const ProductDetails = () => {
|
62
|
173
|
setQuantity((prevQuantity) => (prevQuantity > 1 ? prevQuantity - 1 : 1));
|
63
|
174
|
};
|
64
|
175
|
|
65
|
|
- const handleSizeClick = (size) => {
|
66
|
|
- setSelectedSize(size);
|
67
|
|
- };
|
68
|
176
|
|
69
|
177
|
return (
|
70
|
178
|
<Box sx={{ position: "relative" }}>
|
|
@@ -82,42 +190,58 @@ const ProductDetails = () => {
|
82
|
190
|
variant="body1"
|
83
|
191
|
sx={{ fontWeight: "bold" }}
|
84
|
192
|
>
|
85
|
|
- {`${product?.compareAtPriceRange?.minVariantPrice?.currencyCode}
|
86
|
|
- ${parseFloat(product?.compareAtPriceRange?.minVariantPrice?.amount).toFixed(2)}`}
|
|
193
|
+ {`${variantSelection.currencyCode} ${parseFloat(variantSelection.amount).toFixed(2)}`}
|
|
194
|
+ </Typography>
|
|
195
|
+ <Typography
|
|
196
|
+ variant="body1"
|
|
197
|
+ >
|
|
198
|
+ IN STOCK: {`${variantSelection?.quantityAvailable}`}
|
87
|
199
|
</Typography>
|
88
|
200
|
</Box>
|
89
|
201
|
|
90
|
|
- {/* Section 2: Size */}
|
|
202
|
+ {/* Section 2: Variants */}
|
91
|
203
|
<Box>
|
92
|
|
- <Typography variant="body1" sx={{ fontWeight: "bold", color: "#000", mb: 2 }}>
|
93
|
|
- Size
|
94
|
|
- </Typography>
|
95
|
|
- <Box display="flex" gap={2}>
|
96
|
|
- {["S", "M", "L", "XL"].map((size) => (
|
97
|
|
- <Button
|
98
|
|
- key={size}
|
99
|
|
- variant={selectedSize === size ? "contained" : "outlined"}
|
100
|
|
- color={selectedSize === size ? "primary" : "primary"}
|
101
|
|
- sx={{ color: selectedSize === size ? "#FFF" : "#000" }}
|
102
|
|
- onClick={() => handleSizeClick(size)}
|
103
|
|
- >
|
104
|
|
- {size}
|
105
|
|
- </Button>
|
106
|
|
- ))}
|
107
|
|
- </Box>
|
|
204
|
+
|
|
205
|
+ {variants.map(({ name, options }, index) => {
|
|
206
|
+
|
|
207
|
+ return (
|
|
208
|
+ <>
|
|
209
|
+ <Typography variant="body1" sx={{ fontWeight: "bold", color: "#000", mb:1 }}>
|
|
210
|
+ {name}
|
|
211
|
+ </Typography>
|
|
212
|
+
|
|
213
|
+ <Box display="flex" gap={2} sx={{ mb: 2 }}>
|
|
214
|
+ {options?.map((value) => (
|
|
215
|
+ <Button
|
|
216
|
+ key={value}
|
|
217
|
+ variant={variantSelection[name] === value ? "contained" : "outlined"}
|
|
218
|
+ color={variantSelection[name] === value ? "primary" : "primary"}
|
|
219
|
+ sx={{ color: variantSelection[name] === value ? "#FFF" : "#000" }}
|
|
220
|
+ onClick={() => handleVariantClick(name, value)}
|
|
221
|
+ >
|
|
222
|
+ {value}
|
|
223
|
+ </Button>
|
|
224
|
+ ))}
|
|
225
|
+ </Box>
|
|
226
|
+ </>
|
|
227
|
+ )
|
|
228
|
+
|
|
229
|
+ })}
|
|
230
|
+
|
108
|
231
|
</Box>
|
109
|
232
|
|
110
|
233
|
|
111
|
234
|
{/* Section 3: Quantity */}
|
112
|
235
|
<Box sx={{ mb: 5 }}>
|
113
|
236
|
<Typography variant="body1" sx={{ fontWeight: "bold", color: "#000", mb: 2 }}>
|
114
|
|
- Qunatity
|
|
237
|
+ Quantity
|
115
|
238
|
</Typography>
|
116
|
239
|
<Box display="flex" alignItems="center" gap={2}>
|
117
|
240
|
<Button
|
118
|
241
|
variant="contained"
|
119
|
242
|
color="primary"
|
120
|
243
|
sx={{ width: "35px" }}
|
|
244
|
+ disabled={variantSelection?.quantityAvailable == 0}
|
121
|
245
|
onClick={handleDecrement}
|
122
|
246
|
>
|
123
|
247
|
<RemoveIcon />
|
|
@@ -135,6 +259,7 @@ const ProductDetails = () => {
|
135
|
259
|
variant="contained"
|
136
|
260
|
color="primary"
|
137
|
261
|
sx={{ width: "35px" }}
|
|
262
|
+ disabled={variantSelection?.quantityAvailable == 0}
|
138
|
263
|
onClick={handleIncrement}
|
139
|
264
|
>
|
140
|
265
|
<AddIcon />
|