All files / src/components/ProductDetailsModal ProductDetailsModal.tsx

100% Statements 23/23
100% Branches 28/28
100% Functions 5/5
100% Lines 19/19

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                                                    16x 16x 16x   16x   16x   16x 1x                 15x 6x                       12x 9x   9x 9x   9x 4x 4x   4x         4x     9x                                                                                                                                                              
import Dialog from '@mui/material/Dialog';
import DialogTitle from '@mui/material/DialogTitle';
import DialogContent from '@mui/material/DialogContent';
import DialogActions from '@mui/material/DialogActions';
import Button from '@mui/material/Button';
import Typography from '@mui/material/Typography';
import Box from '@mui/material/Box';
import { useProductDetails } from '../../hooks/useProductDetails';
import OlympicLoader from './../OlympicLoader';
import { ErrorDisplay } from '../ErrorDisplay';
import { useTranslation } from 'react-i18next';
import { formatCurrency, formatDate } from '../../utils/format';
import Chip from '@mui/material/Chip';
import { useCartStore } from '../../stores/useCartStore';
import { useAddToCart } from '../../hooks/useAddToCart';
import { API_BASE_URL } from '../../config';
import placeholderImg from '../../assets/products/placeholder.png';
 
interface Props {
  open: boolean;
  productId: number | null;
  lang: string;
  onClose: () => void;
}
 
export function ProductDetailsModal({ open, productId, lang, onClose }: Props) {
  const { t } = useTranslation(['ticket', 'cart']);
  const cartItems = useCartStore(s => s.items);
  const addToCart = useAddToCart();
 
  const { product, loading, error } = useProductDetails(open ? productId : null, lang);
 
  const titleId = 'product-title';
 
  if (loading) {
    return (
      <Dialog open={open} onClose={onClose}>
        <DialogContent sx={{ textAlign: 'center', py: 4 }}>
          <OlympicLoader />
        </DialogContent>
      </Dialog>
    );
  }
 
  if (error || !product) {
    return (
      <Dialog open={open} onClose={onClose}>
        <ErrorDisplay
          title={t('ticket:errors.title')}
          message={t('ticket:errors.not_found')}
          showRetry={false}
          showHome={false}
        />
      </Dialog>
    );
  }
 
  const fmtCur = (v: number)   => formatCurrency(v, lang, 'EUR');
  const dateStr  = formatDate(product.product_details.date, lang);
 
  const soldOut = product.stock_quantity === 0;
  const finalPrice = (product.price) * (1 - (product.sale));
 
  const handleBuy = async () => {
    const existing = cartItems.find(i => i.id === product.id.toString());
    const newQty = (existing?.quantity ?? 0) + 1;
 
    const ok = await addToCart(
      product.id.toString(),
      newQty,
      product.stock_quantity
    );
    if (ok) onClose();
  };
 
  return (
    <Dialog
      open={open}
      onClose={onClose}
      maxWidth="sm"
      fullWidth
      aria-labelledby={titleId}
    >
      <DialogTitle id={titleId}>
        {product.name}
      </DialogTitle>
 
      <DialogContent dividers>
        {product && (
          <Box component="div">
            <Box
              component="img"
              src={
                product.product_details.image
                  ? `${API_BASE_URL}/products/images/${product.product_details.image}`
                  : placeholderImg
              }
              alt={product.name}
              loading="lazy"
              sx={{ width: '100%', height: 200, objectFit: 'cover', mb: 2 }}
            />
 
            <Typography variant="body1">
              {dateStr}{product.product_details.time && ` – ${product.product_details.time}`}
            </Typography>
 
            <Typography variant="body1" color="text.secondary" sx={{ mb: 1 }}>
              {product.product_details.location}
            </Typography>
 
            <Typography variant='body2' color="text.secondary" sx={{ mb: 1 }}>
              {t('tickets.category', { category: product.product_details.category })}
            </Typography>
 
            <Typography variant="body2" sx={{ mb: 1 }}>
              {product.product_details.description}
            </Typography>
 
            <Typography variant="body1" color="text.secondary">
              {t('ticket:tickets.places', { count: product.product_details.places })}
            </Typography>
 
            <Box sx={{ display: 'flex', alignItems: 'baseline', gap: 1 , mt: 1 }}>
              {product.sale > 0 && (
                <Typography variant="body2" sx={{ textDecoration: 'line-through' }}>
                  {fmtCur(product.price)}
                </Typography>
              )}
              <Typography variant="subtitle1" fontWeight="bold">
                {fmtCur(finalPrice)}
              </Typography>
              {product.sale > 0 && <Chip label={`–${Math.round(product.sale*100)}%`} size="small" />}
            </Box>
            <Typography variant="body2" color={soldOut ? 'error.main' : 'text.secondary'} sx={{ mt:1 }}>
              { soldOut ? t('ticket:tickets.out_of_stock') : t('ticket:tickets.available', {count: product.stock_quantity}) }
            </Typography>
            </Box>
        )}
      </DialogContent>
 
      <DialogActions>
        <Button onClick={onClose}>{t('ticket:tickets.close')}</Button>
        <Button
          variant="contained"
          onClick={handleBuy}
          disabled={soldOut}
        >
          {soldOut ? t('ticket:tickets.out_of_stock') : t('ticket:tickets.buy')}
        </Button>
      </DialogActions>
    </Dialog>
  );
}