-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathCart.tsx
86 lines (84 loc) · 2.52 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import {
Sheet,
SheetContent,
SheetHeader,
SheetTitle,
SheetTrigger,
} from './ui/sheet';
import {
HiMinus,
HiOutlinePlus,
HiOutlineShoppingCart,
HiOutlineTrash,
} from 'react-icons/hi';
import { Button } from './ui/button';
import { useAppDispatch, useAppSelector } from '@/redux/hook';
import {
addtoCart,
removeFromCart,
removeOne,
} from '@/redux/feature/cart/CartSlice/cartSlice';
export default function Cart() {
//!! redux query
const { products, total } = useAppSelector((state) => state.Cart);
const dispatch = useAppDispatch();
return (
<Sheet>
<SheetTrigger>
<Button variant="ghost">
<HiOutlineShoppingCart size="25" />
</Button>
</SheetTrigger>
<SheetContent className="overflow-auto relative">
<SheetHeader>
<SheetTitle>Cart</SheetTitle>
<h1>Total: {total.toFixed(2)}</h1>
</SheetHeader>
<div className="space-y-5">
{products.map((product) => (
<div
className="border h-44 p-5 flex justify-between rounded-md"
key={product.name}
>
<div className="border-r pr-5 shrink-0">
<img src={product?.image} alt="" className="h-full" />
</div>
<div className="px-2 w-full flex flex-col gap-3">
<h1 className="text-2xl self-center">{product?.name}</h1>
<p>Quantity: {product.quantity}</p>
<p className="text-xl">
Total Price: {(product.price * product.quantity!).toFixed(2)}$
</p>
</div>
<div className="border-l pl-5 flex flex-col justify-between">
<Button
onClick={() => {
dispatch(addtoCart(product));
}}
>
<HiOutlinePlus size="20" />
</Button>
<Button
onClick={() => {
dispatch(removeOne(product));
}}
>
<HiMinus size="20" />
</Button>
<Button
onClick={() => {
dispatch(removeFromCart(product));
}}
variant="destructive"
className="bg-red-500 hover:bg-red-400"
>
<HiOutlineTrash size="20" />
</Button>
</div>
</div>
))}
</div>
</SheetContent>
</Sheet>
);
}