/* global React */ /* Cart drawer PARTAGÉ (panneau latéral) — s'ouvre au clic sur le panier du header, sur toutes les pages. Source unique : le panier serveur (GET /panier enrichi nom/réf/image, POST /panier/modifier). Émet l'événement 'ec-cart-changed' après modif pour que la page (barre + badge) se resynchronise. */ function EcCartDrawer({ open, onClose }) { const cfg = window.EXTRANET_CONFIG || {}; const T = (window.EcUI && window.EcUI.T) || {}; const base = cfg.base_url || ''; const CARTON = (window.PORTAIL_CONFIG && window.PORTAIL_CONFIG.conditionnement) || 3; const FRANCO = parseFloat(cfg.franco_ht) || 250; const PRIMARY = T.primary || 'oklch(0.62 0.14 40)'; const INK = T.ink || 'oklch(0.255 0.016 48)'; const BORDER = T.border || 'oklch(0.88 0.012 75)'; const MUTE = T.mute || 'oklch(0.55 0.012 65)'; const SERIF = T.serif || "'Playfair Display',serif"; const SANS = T.sans || "'Hanken Grotesk',sans-serif"; const fmt = (n) => (Math.round((n || 0) * 100) / 100).toFixed(2).replace('.', ',') + ' €'; const [lignes, setLignes] = React.useState([]); const [total, setTotal] = React.useState(0); const [loading, setLoading] = React.useState(false); const load = React.useCallback(() => { setLoading(true); fetch(base + '/panier', { credentials: 'same-origin' }) .then(r => r.json()) .then(d => { setLignes(d.lignes || []); setTotal(parseFloat(d.total_ht) || 0); setLoading(false); }) .catch(() => setLoading(false)); }, [base]); React.useEffect(() => { if (open) load(); }, [open, load]); const setQty = (l, newQty) => { setLignes(ls => ls.map(x => x.product_id === l.product_id ? { ...x, quantite: newQty, total_ht: l.prix_ht * newQty } : x).filter(x => x.quantite > 0)); setTotal(t => t + l.prix_ht * (newQty - l.quantite)); fetch(base + '/panier/modifier', { method: 'POST', headers: { 'Content-Type': 'application/json' }, credentials: 'same-origin', body: JSON.stringify({ product_id: l.product_id, quantite: newQty }) }) .then(() => { load(); try { window.dispatchEvent(new Event('ec-cart-changed')); } catch (e) {} }) .catch(() => {}); }; const units = lignes.reduce((s, l) => s + l.quantite, 0); const francoText = total >= FRANCO ? '✓ Franco de port acquis' : ('Plus que ' + fmt(FRANCO - total) + ' pour le franco de port'); return (
); }