All files / src/components/CartItemDisplay CartItemDisplay.tsx

100% Statements 5/5
100% Branches 25/25
100% Functions 1/1
100% Lines 5/5

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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198                                          18x 18x 18x 9x                                                                                                                                                                     9x                                                                                                                                                                                    
import Paper from "@mui/material/Paper";
import type { CartItem } from "../../stores/useCartStore";
import TableRow from "@mui/material/TableRow";
import Box from "@mui/material/Box";
import Typography from "@mui/material/Typography";
import Chip from "@mui/material/Chip";
import { formatCurrency, formatDate } from "../../utils/format";
import QuantityInput from "../QuantityInput";
import { useTranslation } from 'react-i18next';
import TableCell from "@mui/material/TableCell";
 
 
interface CartItemDisplayProps {
  item: CartItem;
  lang: string;
  adjustQty: (item: CartItem, newQty: number) => void;
  isMobile: boolean;
  disabled?: boolean;
}
 
export function CartItemDisplay({ item, lang, adjustQty, isMobile, disabled = false }: CartItemDisplayProps) {
  const { t } = useTranslation('cart');
  const discountPct = Math.round((item.discountRate ?? 0) * 100)
  if (isMobile) {
    return (
      <Paper key={item.id} sx={{ p: 2, border: '1px solid', borderColor: 'divider', borderRadius: 1 }}>
        <Box sx={{ display: 'flex', flexDirection: 'column', gap: 1.5 }}>
          {/* Nom et badges */}
          <Box>
            <Typography
              variant="subtitle1"
              sx={{ wordBreak: 'break-word' }}
              gutterBottom
            >
              {item.name}
            </Typography>
            <Box sx={{ display: 'flex', flexWrap: 'wrap', gap: 0.5, mb: 1 }}>
              <Chip
                label={formatDate(
                  item.date,
                  lang,
                  { day: '2-digit', month: '2-digit', year: 'numeric' }
                )}
                size="small"
              />
              {item.time && <Chip label={item.time} size="small" />}
              <Chip label={item.location} size="small" />
            </Box>
            <Chip
              label={t('cart.remaining', {
                count: item.availableQuantity,
              })}
              size="small"
              color={item.availableQuantity > 0 ? 'success' : 'error'}
            />
          </Box>
 
          {/* Bloc Prix unitaire, Quantité et Total */}
          <Box sx={{ display: 'flex', flexDirection: 'column', gap: 1 }}>
            {/* Prix unitaire centré */}
            <Box sx={{ textAlign: 'center' }}>
              {(item.discountRate ?? 0) > 0 ? (
                <Box>
                  <Typography
                    variant="body2"
                    component="span"
                    sx={{
                      textDecoration: 'line-through',
                      color: 'text.secondary',
                      mr: 0.5,
                    }}
                  >
                    {formatCurrency(item.originalPrice ?? 0, lang, 'EUR')}
                  </Typography>
                  <Typography
                    variant="body1"
                    component="span"
                    sx={{ fontWeight: 'bold' }}
                  >
                    {formatCurrency(item.price, lang, 'EUR')}
                  </Typography>
                  <Chip 
                    label={`-${discountPct}%`}
                    size="small"
                    color="secondary"
                    sx={{ ml: 1 }}
                  />
                </Box>
              ) : (
                <Typography>{formatCurrency(item.price, lang, 'EUR')}</Typography>
              )}
            </Box>
 
            {/* Quantité (boutons + TextField) centré */}
            <QuantityInput item={item} adjustQty={adjustQty} />
 
            {/* Total aligné à droite, avec “Total : ” */}
            <Box sx={{ textAlign: 'right' }}>
              <Typography variant="body1">
                {t('table.total_price', {total:formatCurrency(item.quantity * item.price, lang, 'EUR')})}
              </Typography>
            </Box>
          </Box>
        </Box>
      </Paper>
    );
  }
  return (
    <TableRow
      key={item.id}
      sx={{
        // Si c'est la dernière rangée, on supprime la bordure du bas sur tous les <td>
        '&:last-child td': {
          borderBottom: 'none',
        },
      }}
    >
      {/* Colonne “Produit” */}
      <TableCell sx={{ minWidth: 200 }}>
        <Box sx={{ display: 'flex', alignItems: 'flex-start' }}>
          <Box sx={{ flexGrow: 1 }}>
            <Typography
              variant="subtitle1"
              component="div"
              sx={{ wordBreak: 'break-word' }}
            >
              {item.name}
            </Typography>
            <Box sx={{ mt: 0.5, display: 'flex', flexWrap: 'wrap', gap: 0.5 }}>
              <Chip
                label={formatDate(
                  item.date,
                  lang,
                  { day: '2-digit', month: '2-digit', year: 'numeric' }
                )}
                size="small"
              />
              {item.time && <Chip label={item.time} size="small" />}
              <Chip label={item.location} size="small" />
            </Box>
            <Box sx={{ mt: 0.5 }}>
              <Chip
                label={t('cart.remaining', {
                  count: item.availableQuantity,
                })}
                size="small"
                color={item.availableQuantity > 0 ? 'success' : 'error'}
              />
            </Box>
          </Box>
        </Box>
      </TableCell>
 
      {/* Colonne “Prix unitaire” */}
      <TableCell align="right" sx={{ minWidth: 120 }}>
        {(item.discountRate ?? 0) > 0 ? (
          <Box sx={{ textAlign: 'right' }}>
            <Typography
              variant="body2"
              component="span"
              sx={{ textDecoration: 'line-through', color: 'text.secondary', mr: 0.5 }}
            >
              {formatCurrency(item.originalPrice ?? 0, lang, 'EUR')}
            </Typography>
            <Typography
              variant="body1"
              component="span"
              sx={{ fontWeight: 'bold' }}
            >
              {formatCurrency(item.price, lang, 'EUR')}
            </Typography>
            <Chip 
              label={`-${discountPct}%`}
              size="small"
              color="secondary"
              sx={{ ml: 1 }}
            />
          </Box>
        ) : (
          <Typography>{formatCurrency(item.price, lang, 'EUR')}</Typography>
        )}
      </TableCell>
 
      {/* Colonne “Quantité” (boutons + TextField) */}
      <TableCell align="center" sx={{ minWidth: 160 }}>
        <QuantityInput item={item} adjustQty={adjustQty} disabled={disabled} />
      </TableCell>
 
      {/* Colonne “Total” */}
      <TableCell align="right" sx={{ minWidth: 120 }}>
        <Typography variant="body1">
          {formatCurrency(item.quantity * item.price, lang, 'EUR')}
        </Typography>
      </TableCell>
    </TableRow>
  );
}