|
@@ -0,0 +1,149 @@
|
|
1
|
+import { useEffect, useState } from "react";
|
|
2
|
+import { Drawer, Box, Typography, IconButton } from "@mui/material";
|
|
3
|
+import Grid from '@mui/material/Grid2';
|
|
4
|
+import CloseIcon from "@mui/icons-material/Close";
|
|
5
|
+import { updateItemQuantity } from "../../redux/slices/cartSlice";
|
|
6
|
+import AddIcon from '@mui/icons-material/Add';
|
|
7
|
+import RemoveIcon from '@mui/icons-material/Remove';
|
|
8
|
+import { useSelector, useDispatch } from 'react-redux';
|
|
9
|
+
|
|
10
|
+const SideCart = ({ open, onClose }) => {
|
|
11
|
+
|
|
12
|
+ const dispatch = useDispatch()
|
|
13
|
+ const cart = useSelector((state) => state.cart.cart)
|
|
14
|
+ const [cartProducts, setCartProducts] = useState([]) // this is being used as view only, will not used as a way to manipulate cart in any way
|
|
15
|
+
|
|
16
|
+ useEffect(() => {
|
|
17
|
+
|
|
18
|
+ if (cart?.lines?.nodes?.length == 0) window.location.href = "/"
|
|
19
|
+ setCartProducts(cart?.lines?.nodes || [])
|
|
20
|
+
|
|
21
|
+ }, [cart])
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+ const handleUpdateCart = ({ quantity, lineId}) => {
|
|
25
|
+
|
|
26
|
+ let cartHistory = localStorage.getItem('amber-cart');
|
|
27
|
+ cartHistory = cartHistory ? JSON.parse(cartHistory) : {};
|
|
28
|
+
|
|
29
|
+ dispatch(updateItemQuantity({
|
|
30
|
+ cartId:cartHistory.id,
|
|
31
|
+ lineId,
|
|
32
|
+ quantity,
|
|
33
|
+ }))
|
|
34
|
+
|
|
35
|
+ }
|
|
36
|
+
|
|
37
|
+ return (
|
|
38
|
+ <Drawer
|
|
39
|
+ open={open}
|
|
40
|
+ onClose={onClose}
|
|
41
|
+ anchor="right"
|
|
42
|
+ sx={{
|
|
43
|
+ "& .MuiDrawer-paper": {
|
|
44
|
+ backgroundColor: "white",
|
|
45
|
+ color: "black",
|
|
46
|
+ width: {
|
|
47
|
+ xs: "90%",
|
|
48
|
+ sm: "90%",
|
|
49
|
+ md: "30%",
|
|
50
|
+ },
|
|
51
|
+ padding: 2,
|
|
52
|
+ },
|
|
53
|
+ }}
|
|
54
|
+ >
|
|
55
|
+ <Box>
|
|
56
|
+ {/* Header */}
|
|
57
|
+ <Box
|
|
58
|
+ sx={{
|
|
59
|
+ display: "flex",
|
|
60
|
+ justifyContent: "space-between",
|
|
61
|
+ alignItems: "center",
|
|
62
|
+ marginBottom: 2,
|
|
63
|
+ }}
|
|
64
|
+ >
|
|
65
|
+ <Typography variant="h6">Your Cart</Typography>
|
|
66
|
+ <IconButton onClick={onClose}>
|
|
67
|
+ <CloseIcon />
|
|
68
|
+ </IconButton>
|
|
69
|
+ </Box>
|
|
70
|
+
|
|
71
|
+ {/* Product List */}
|
|
72
|
+ {cartProducts && cartProducts?.length > 0 ? (
|
|
73
|
+ cartProducts.map(({ id, cost, merchandise, quantity }, index) => {
|
|
74
|
+
|
|
75
|
+ let { amount, currencyCode } = cost.totalAmount
|
|
76
|
+ let { image, product, title } = merchandise
|
|
77
|
+
|
|
78
|
+ // ID
|
|
79
|
+ const parts = product?.id?.split('/');
|
|
80
|
+ let prodID = parts[parts.length - 1];
|
|
81
|
+
|
|
82
|
+ return (
|
|
83
|
+ <Box
|
|
84
|
+ key={index}
|
|
85
|
+ sx={{
|
|
86
|
+ display: "flex",
|
|
87
|
+ alignItems: "center",
|
|
88
|
+ padding: 1,
|
|
89
|
+ borderBottom: "1px solid #eee",
|
|
90
|
+ }}
|
|
91
|
+ >
|
|
92
|
+ <Grid container>
|
|
93
|
+ {/* Product Image */}
|
|
94
|
+ <Grid item size={4}>
|
|
95
|
+ <Box
|
|
96
|
+ component="img"
|
|
97
|
+ src={merchandise?.image?.src}
|
|
98
|
+ alt={product.name}
|
|
99
|
+ sx={{
|
|
100
|
+ width: "100%",
|
|
101
|
+ height: "auto",
|
|
102
|
+ borderRadius: 2,
|
|
103
|
+ }}
|
|
104
|
+ />
|
|
105
|
+ </Grid>
|
|
106
|
+
|
|
107
|
+ {/* Product Details */}
|
|
108
|
+ <Grid item size={8} sx={{ paddingLeft: 1 }}>
|
|
109
|
+ <Typography variant="body1" sx={{lineHeight:1, fontWeight: "bold"}}>
|
|
110
|
+ {product?.title}
|
|
111
|
+ </Typography>
|
|
112
|
+ <Typography variant='body2' sx={{ fontWeight: "bold" }}>{`${currencyCode} ${parseFloat(amount).toFixed(2)}`}</Typography>
|
|
113
|
+ <Typography variant='body2' sx={{ fontWeight: "bold" }} >{title}</Typography>
|
|
114
|
+ <Box sx={{ display: "flex", justifyContent: "center" }}>
|
|
115
|
+
|
|
116
|
+ <IconButton onClick={() => {
|
|
117
|
+
|
|
118
|
+ handleUpdateCart({ quantity: quantity - 1, lineId:id })
|
|
119
|
+ }}>
|
|
120
|
+ <RemoveIcon sx={{ fontSize: 16, margin: "0 15px" }} />
|
|
121
|
+ </IconButton>
|
|
122
|
+
|
|
123
|
+ <p style={{ fontSize: 20, fontWeight: "bold" }}>{quantity}</p>
|
|
124
|
+
|
|
125
|
+ <IconButton onClick={() => {
|
|
126
|
+
|
|
127
|
+ handleUpdateCart({ quantity: quantity + 1, lineId:id })
|
|
128
|
+ }}>
|
|
129
|
+ <AddIcon sx={{ fontSize: 16, margin: "0 15px" }} />
|
|
130
|
+ </IconButton>
|
|
131
|
+
|
|
132
|
+ </Box>
|
|
133
|
+ </Grid>
|
|
134
|
+ </Grid>
|
|
135
|
+ </Box>
|
|
136
|
+ )
|
|
137
|
+
|
|
138
|
+ })
|
|
139
|
+ ) : (
|
|
140
|
+ <Typography variant="body2" color="text.secondary">
|
|
141
|
+ Your cart is empty.
|
|
142
|
+ </Typography>
|
|
143
|
+ )}
|
|
144
|
+ </Box>
|
|
145
|
+ </Drawer>
|
|
146
|
+ );
|
|
147
|
+};
|
|
148
|
+
|
|
149
|
+export default SideCart;
|