All files / src/components Cart.jsx

100% Statements 9/9
70% Branches 7/10
57.14% Functions 4/7
100% Lines 8/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                    1x           10x 10x 10x 16x       16x   10x                                                                                     16x                                                                                                                                                          
import React from "react";
import {
  Container,
  Typography,
  Paper,
  List,
  Divider,
  Box,
  Button,
  Stack,
} from "@mui/material";
import ShoppingCartOutlinedIcon from "@mui/icons-material/ShoppingCartOutlined";
import ArrowBackIcon from "@mui/icons-material/ArrowBack";
import { Link } from "react-router-dom";
import CartItem from "./CartItem";
import CheckoutButton from "./CheckoutButton";
import { useLanguage } from "../contexts/LanguageContext";
 
const Cart = ({
  cartItems = [],
  onUpdateCart = () => {},
  onRemoveFromCart = () => {},
  setCartItems = () => {},
}) => {
  const { t } = useLanguage();
  const safeCartItems = Array.isArray(cartItems) ? cartItems : [];
  const totalPrice = safeCartItems.reduce(
    (total, item) => total + item.price * item.quantity,
    0
  );
 
  const totalItems = safeCartItems.reduce((sum, item) => sum + item.quantity, 0);
 
  return (
    <Container maxWidth="md">
      <Typography variant="h4" component="h1" gutterBottom fontWeight={700}>
        {t("cart.title")}
      </Typography>
 
      {safeCartItems.length === 0 ? (
        <Paper
          elevation={0}
          sx={{
            textAlign: "center",
            py: 8,
            px: 4,
            borderRadius: 3,
            border: "2px dashed",
            borderColor: "divider",
          }}
        >
          <ShoppingCartOutlinedIcon
            sx={{ fontSize: 80, color: "text.disabled", mb: 2 }}
          />
          <Typography variant="h6" color="text.secondary" gutterBottom>
            {t("cart.empty_title")}
          </Typography>
          <Typography variant="body2" color="text.disabled" sx={{ mb: 3 }}>
            {t("cart.empty_desc")}
          </Typography>
          <Button
            component={Link}
            to="/"
            variant="contained"
            startIcon={<ArrowBackIcon />}
          >
            {t("cart.go_to_catalog")}
          </Button>
        </Paper>
      ) : (
        <Box id="cart-content-wrapper" sx={{ display: "flex", flexDirection: { xs: "column", md: "row" }, gap: 3 }}>
          {/* Lista de itens */}
          <Box id="cart-item-list-wrapper" sx={{ flex: 1 }}>
            <Paper elevation={2} sx={{ borderRadius: 3, overflow: "hidden" }}>
              <List disablePadding>
                {safeCartItems.map((item, index) => (
                  <React.Fragment key={item.id}>
                    <CartItem
                      item={item}
                      onUpdateCart={onUpdateCart}
                      onRemoveFromCart={onRemoveFromCart}
                    />
                    {index < safeCartItems.length - 1 && <Divider />}
                  </React.Fragment>
                ))}
              </List>
            </Paper>
          </Box>
 
          {/* Resumo do pedido */}
          <Box id="cart-order-summary-wrapper" sx={{ width: { xs: "100%", md: 300 } }}>
            <Paper elevation={2} sx={{ borderRadius: 3, p: 3 }}>
              <Typography variant="h6" gutterBottom fontWeight={700}>
                {t("cart.order_summary")}
              </Typography>
              <Divider sx={{ mb: 2 }} />
 
              <Stack spacing={1} sx={{ mb: 2 }}>
                <Box id="cart-order-items-count" sx={{ display: "flex", justifyContent: "space-between" }}>
                  <Typography color="text.secondary">
                    {t("cart.items", { count: totalItems })}
                  </Typography>
                  <Typography>R$ {totalPrice.toFixed(2)}</Typography>
                </Box>
                <Box id="cart-order-shipping" sx={{ display: "flex", justifyContent: "space-between" }}>
                  <Typography color="text.secondary">{t("cart.shipping")}</Typography>
                  <Typography color="success.main" fontWeight={600}>
                    {t("cart.free")}
                  </Typography>
                </Box>
              </Stack>
 
              <Divider sx={{ mb: 2 }} />
 
              <Box id="cart-order-total"
                sx={{
                  display: "flex",
                  justifyContent: "space-between",
                  mb: 3,
                }}
              >
                <Typography variant="h6" fontWeight={700}>
                  {t("cart.total")}
                </Typography>
                <Typography variant="h6" fontWeight={700} color="primary">
                  R$ {totalPrice.toFixed(2)}
                </Typography>
              </Box>
 
              <CheckoutButton
                cartItems={safeCartItems}
                setCartItems={setCartItems}
              />
 
              <Button
                component={Link}
                to="/"
                fullWidth
                variant="text"
                startIcon={<ArrowBackIcon />}
                sx={{ mt: 1 }}
              >
                {t("cart.continue_shopping")}
              </Button>
            </Paper>
          </Box>
        </Box>
      )}
    </Container>
  );
};
 
export default Cart;