aboutsummaryrefslogtreecommitdiffstats
path: root/thallium-frontend/src/components/CartStatus.tsx
blob: 8fd54d9ba321b91ada8a0d3d0a10a715f4f2909a (plain) (blame)
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
import { useSelector } from "react-redux";
import { RootState } from "../store";
import styled from "styled-components";
import Button from "./forms/Button";
import { useNavigate } from "react-router-dom";

const StatusHolder = styled.span`
    margin-top: 1rem;
    margin-bottom: 1rem;

`;

const CartStatus = () => {
    const cart = useSelector((state: RootState) => state.cart);
    const total = cart.cart.reduce((acc, item) => acc + item.quantity, 0);
    const price = cart.cart.reduce((acc, item) => acc + (parseFloat(item.estPrice) * item.quantity), 0);

    const navigate = useNavigate();

    return <>
        <StatusHolder>You currently have {total} item{total !== 1 ? "s" : null} in your cart, totalling ${price.toFixed(2)} USD</StatusHolder>
        {total > 0 ? <Button onClick={() => {
            navigate("/checkout");
        }}>
            Checkout &gt;
        </Button> : null}
    </>;
};

export default CartStatus;