-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCart.tsx
65 lines (59 loc) · 2.39 KB
/
Cart.tsx
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
import React from "react";
import {ButtonGroup, Collapse, IconButton, Typography} from "@mui/material";
import {useDispatch, useSelector} from "react-redux";
import cartInterface from "../interfaces/cart";
import {RootState} from "../store";
import {ExpandLess, ExpandMore, Delete, ShoppingBag} from "@mui/icons-material";
import {cartActions} from "../store/cart";
const Cart = () => {
const dispatch = useDispatch();
const cart: cartInterface = useSelector((state: RootState) => state.cartReducer.dict);
const [open, setOpen] = React.useState(true);
const dropDownHandler = () => {
setOpen(!open);
};
const deleteCartHandler = () => {
dispatch(cartActions.deleteCart());
};
return (
<div className={"cart"}>
<div className={"cart-header"}>
<ShoppingBag className={"mx-2"}/>
<Typography>Cart</Typography>
<div className={"grow"}/>
<ButtonGroup>
{Object.keys(cart).length > 0 &&
<IconButton onClick={deleteCartHandler}>
<Delete className={"text-white"}/>
</IconButton>
}
{!open &&
<IconButton onClick={dropDownHandler}>
<ExpandMore className={"text-white"}/>
</IconButton>
}
{open &&
<IconButton onClick={dropDownHandler}>
<ExpandLess className={"text-white"}/>
</IconButton>
}
</ButtonGroup>
</div>
<Collapse in={open} timeout="auto" unmountOnExit>
{Object.keys(cart).length > 0 &&
Object.keys(cart).map(key => {
const cartItem = cart[key];
return (
<div key={key} className={"cart-item"}>
<Typography>{cartItem.name}</Typography>
<Typography>{cartItem.count} Items</Typography>
<Typography>{cartItem.price * cartItem.count}c</Typography>
</div>
)
})
}
</Collapse>
</div>
);
};
export default Cart;