All files / src/components Product.jsx

72.72% Statements 8/11
64.28% Branches 9/14
57.14% Functions 4/7
72.72% Lines 8/11

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                                        2x 10x 10x 10x 10x                 10x                                                                                                                       100x                       1x                        
import React, { useState } from "react";
import {
  Card,
  CardMedia,
  CardContent,
  CardActions,
  Typography,
  Button,
  Box,
  Select,
  MenuItem,
  FormControl,
  InputLabel,
  Chip,
  Tooltip,
} from "@mui/material";
import { useLanguage } from "../contexts/LanguageContext";
import { useNavigate } from "react-router-dom";
import AddShoppingCartIcon from "@mui/icons-material/AddShoppingCart";
 
const Product = ({ product = {}, onAddToCart = () => {} }) => {
  const [quantity, setQuantity] = useState(1);
  const { t } = useLanguage();
  const navigate = useNavigate();
  const safeProduct = {
    id: product.id ?? "unknown",
    image: product.image ?? "",
    name: product.name ?? "Produto",
    description: product.description ?? "",
    category: product.category ?? "Geral",
    price: Number.isFinite(product.price) ? product.price : 0,
  };
 
  return (
    <Card
      sx={{
        height: "100%",
        display: "flex",
        flexDirection: "column",
        transition: "transform 0.2s ease, box-shadow 0.2s ease",
        "&:hover": {
          transform: "translateY(-4px)",
          boxShadow: 6,
        },
      }}
    >
      <Box id={`product-card-image-wrapper-${safeProduct.id}`}
        onClick={() => navigate(`/product/${safeProduct.id}`)}
        sx={{ cursor: "pointer", p: 1 }}
      >
        <CardMedia
          component="img"
          height="200"
          image={safeProduct.image}
          alt={safeProduct.name}
          sx={{ objectFit: "contain" }}
        />
      </Box>
      <CardContent sx={{ flexGrow: 1, pb: 1 }}>
        <Chip
          label={safeProduct.category}
          size="small"
          color="primary"
          variant="outlined"
          sx={{ mb: 1, fontSize: "0.7rem" }}
        />
        <Typography 
          variant="h6" 
          component="h2" 
          gutterBottom 
          sx={{ fontSize: "1rem", fontWeight: 700, lineHeight: 1.3, cursor: "pointer", "&:hover": { color: "primary.main" } }}
          onClick={() => navigate(`/product/${safeProduct.id}`)}
        >
          {safeProduct.name}
        </Typography>
        <Typography variant="body2" color="text.secondary" sx={{ mb: 1.5, fontSize: "0.82rem" }}>
          {safeProduct.description}
        </Typography>
        <Typography variant="h6" color="primary" fontWeight={700}>
          R$ {safeProduct.price.toFixed(2)}
        </Typography>
      </CardContent>
 
      <CardActions sx={{ px: 2, pb: 2, gap: 1, flexWrap: "wrap" }}>
        <FormControl size="small" sx={{ minWidth: 70 }}>
          <InputLabel id={`qty-label-${product.id}`}>{t("cart_item.qty").replace(":", "")}</InputLabel>
          <Select
            labelId={`qty-label-${safeProduct.id}`}
            value={quantity}
            label={t("cart_item.qty").replace(":", "")}
            onChange={(e) => setQuantity(e.target.value)}
          >
            {[...Array(10).keys()].map((x) => (
              <MenuItem key={x + 1} value={x + 1}>
                {x + 1}
              </MenuItem>
            ))}
          </Select>
        </FormControl>
        <Tooltip title="Adicionar ao carrinho">
          <Button
            variant="contained"
            color="secondary"
            size="small"
            startIcon={<AddShoppingCartIcon />}
            onClick={() => onAddToCart(safeProduct, quantity)}
            sx={{ flexGrow: 1 }}
          >
            {t("product.add_to_cart")}
          </Button>
        </Tooltip>
      </CardActions>
    </Card>
  );
};
 
export default Product;