-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup.txt
196 lines (183 loc) · 6.14 KB
/
backup.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
{/* <div className="grid gap-2">
<p className="text-sm font-medium">Colors</p>
<RadioGroup className="flex items-center" name="color">
{product.productColors.map((color, index) => (
<Link
key={color.id}
href={`?ci=${index}&si=${sizeIndex}`}
replace={true}
scroll={false}
className={cn({
"pointer-events-none": color.stock === 0,
})}
>
<Label
htmlFor={color.id}
className="p-2 border cursor-pointer flex items-center space-x-2 rounded-lg"
>
<RadioGroupItem
value={color.id}
id={color.id}
disabled={color.stock === 0}
checked={colorIndex === index}
/>
<span
className={cn("leading-none capitalize", {
"text-muted-foreground": color.stock === 0,
})}
>
{color.color.name}
</span>
<div
className="h-4 w-4 rounded-full"
style={{
backgroundColor: color.color.hexCode,
}}
></div>
</Label>
</Link>
))}
</RadioGroup>
</div>
<div className="grid gap-2">
<p className="text-sm font-medium">Sizes</p>
<RadioGroup
defaultValue={product.productSizes[sizeIndex].id}
className="flex items-center"
name="size"
>
{product.productSizes.map((size, index) => (
<Link
key={size.id}
href={`?ci=${colorIndex}&si=${index}`}
replace={true}
scroll={false}
className={cn({
"pointer-events-none": size.stock === 0,
})}
>
<Label
htmlFor={size.id}
className="p-2 border cursor-pointer flex items-center space-x-2 rounded-lg"
>
<RadioGroupItem
value={size.id}
id={size.id}
disabled={size.stock === 0}
checked={sizeIndex === index}
/>
<span
className={cn("leading-none uppercase", {
"text-muted-foreground": size.stock === 0,
})}
>
{size.size.name}
</span>
</Label>
</Link>
))}
</RadioGroup>
</div> */}
{/* <ProductCardButton product={product} /> */}
{/* <div className="flex space-x-2">
<ToggleGroup type="single" className="space-x-3">
{colors.map((color) => (
<ToggleGroupItem value={color} key={color} className="p-0">
<label htmlFor={color} className="rounded-full p-3 cursor-pointer">
<div
className="w-5 h-5 rounded-full cursor-pointer"
style={{ backgroundColor: color }}
></div>
</label>
</ToggleGroupItem>
))}
</ToggleGroup>
</div> */}
{/* <select name="category" defaultValue="default" className="border px-3 py-2 capitalize">
<option value="default" disabled>
Category
</option>
{categories.map((category) => (
<option key={category} value={category} className="capitalize">
{category}
</option>
))}
</select>
<Button type="submit">Apply Filters</Button> */}
const { toast } = useToast();
const { cart, setCart } = useCart()();
const [quantity, setQuantity] = useState(
cart.find((item) => item.id === product.id)?.quantity || 0
);
const [isInCart, setIsInCart] = useState(cart.some((item) => item.id === product.id));
const searchParams = useSearchParams();
const color = product.productColors[+(searchParams.get("ci") || 0)].color.hexCode;
const size = product.productSizes[+(searchParams.get("si") || 0)].size.name;
useEffect(() => {
setCart(
cart.map((item) => {
if (item.id === product.id) {
return {
...item,
size,
color,
};
}
return item;
})
);
}, [color, size]);
const addToCart = () => {
setIsInCart(true);
updateCart(1);
const newCart = [
...cart,
{
...product,
quantity: 1,
size,
color,
},
];
setCart(newCart);
toast({
title: "Added to cart",
description: `${product.name} has been added to your cart.`,
});
};
const updateCart = (newQuantity: number) => {
setQuantity(newQuantity);
const updatedCart = cart.map((item) => {
if (item.id === product.id) {
return {
...item,
quantity: newQuantity,
size,
color,
};
}
return item;
});
setCart(updatedCart);
};
const increaseQuantity = () => {
const newQuantity = quantity + 1;
updateCart(newQuantity);
};
const decreaseQuantity = () => {
if (quantity === 1) {
const updatedCart = cart.filter((item) => item.id !== product.id);
setCart(updatedCart);
setIsInCart(false);
setQuantity(0);
} else if (quantity > 1) {
const newQuantity = quantity - 1;
updateCart(newQuantity);
}
};
const handleRemoveButton = () => {
const newCart = cart.filter((item) => item.id !== product.id);
setCart(newCart);
setIsInCart(false);
setQuantity(0);
};