All files / src/pages TicketsPage.tsx

100% Statements 48/48
100% Branches 27/27
100% Functions 16/16
100% Lines 31/31

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                                        46x 46x 132x 46x     46x         46x   46x   46x 29x 10x   10x 10x 10x 10x 10x 10x   10x         46x   46x   46x 4x             2x                 42x                 1x                       3x 3x 3x 3x         3x               1x                       1x        
import { useState, useEffect } from 'react';
import Box from '@mui/material/Box';
import OlympicLoader from '../components/OlympicLoader';
import Seo from '../components/Seo';
import Pagination from '@mui/material/Pagination';
import Typography from '@mui/material/Typography';
import { ProductsFilters } from '../components/ProductsFilters';
import { ProductGrid } from '../components/ProductGrid';
import { useLanguageStore } from '../stores/useLanguageStore';
import type { Filters } from '../hooks/useProducts';
import { useProducts } from '../hooks/useProducts';
import { PageWrapper } from '../components/PageWrapper';
import { ProductDetailsModal } from '../components/ProductDetailsModal';
import { useTranslation } from 'react-i18next';
import { ErrorDisplay } from '../components/ErrorDisplay';
import { formatCurrency, formatDate } from '../utils/format';
import { useAddToCart } from '../hooks/useAddToCart';
import { useCartStore } from '../stores/useCartStore';
 
export default function ProductsPage() {
  const { t } = useTranslation('ticket');
  const lang = useLanguageStore(s => s.lang);
  const cartItems = useCartStore(s => s.items);
  const addToCart = useAddToCart();
 
  // --- États ---
  const [filters, setFilters] = useState<Filters>({
    name:'', category:'', location:'', date:'', places:0,
    sortBy:'name', order:'asc', perPage:15, page:1,
  });
 
  const { products, total, loading, error, validationErrors } = useProducts(filters, lang);
 
  const [detailsId, setDetailsId] = useState<number | null>(null);
 
  useEffect(() => {
  if (validationErrors) {
    const cleanup: Partial<Filters> = {};
 
    if (validationErrors.sort_by)   { cleanup.sortBy = 'name'; cleanup.order = 'asc'; }
    if (validationErrors.date)      { cleanup.date   = ''; }
    if (validationErrors.name)      { cleanup.name   = ''; }
    if (validationErrors.category)  { cleanup.category = ''; }
    if (validationErrors.location)  { cleanup.location = ''; }
    if (validationErrors.places)    { cleanup.places = 0; }
 
    setFilters(f => ({ ...f, ...cleanup }));
  }
}, [validationErrors]);
 
  // --- Helpers de format ---
  const fmtCur = (v: number)   => formatCurrency(v, lang, 'EUR');
 
  const fmtDate = (iso?: string) => formatDate(iso, lang);
 
  if (error) {
    return (
      <PageWrapper>
        <ErrorDisplay
          title={t('errors.title')}
          message={t('errors.unexpected')}
          showRetry
          retryButtonText={t('errors.retry')}
          onRetry={() => setFilters(f => ({ ...f }))}
          showHome
          homeButtonText={t('errors.home')}
        />
      </PageWrapper>
  );
}
  
  // --- Rendu normal loader / grille / pagination ---
  return (
    <>
      <Seo title={t('tickets.seo_title')} description={t('tickets.seo_description')} />
      <PageWrapper disableCard>
        <Typography variant="h4" sx={{ px:2 }}>
          {t('tickets.title')}
        </Typography>
        <Box sx={{ display:'flex', flexDirection:{ xs:'column', md:'row' }, gap:2, p:2 }}>
          {/* Sidebar filtres */}
          <ProductsFilters filters={filters} onChange={upd=>setFilters(f=>({...f,...upd}))}/>
 
          {/* Contenu principal */}
          <Box component="main" flex={1}>
            {loading
              ? <Box textAlign="center" py={8}><OlympicLoader/></Box>
              : <ProductGrid
                  products={products}
                  fmtCur={fmtCur}
                  fmtDate={fmtDate}
                  onViewDetails={setDetailsId}
                  onBuy={async (prod) => {
                    const existing = cartItems.find(i => i.id === prod.id.toString());
                    const currentQty = existing?.quantity ?? 0;
                    const newQty = currentQty + 1;
                    const ok = await addToCart(
                      prod.id.toString(),
                      newQty,
                      prod.stock_quantity
                    );
                    if (ok) setDetailsId(null);}}
                />
              }
            {!loading && products.length>0 && (
              <Box textAlign="center" mt={4}>
                <Pagination
                  count={Math.ceil(total/filters.perPage)||1}
                  page={filters.page}
                  onChange={(_,p)=>setFilters(f=>({...f,page:p}))}
                />
              </Box>
            )}
          </Box>
        </Box>
      </PageWrapper>
 
      <ProductDetailsModal
        open={detailsId !== null}
        productId={detailsId}
        lang={lang}
        onClose={() => setDetailsId(null)}
      />
    </>
  );
}