/* global React, window, BOXES, PICKUP_WINDOWS, CAL_DAYS, PH, Price, fmt */ // ======================================================================= // Carrinho + drawer + checkout · WooCommerce ou simulado // // Lógica: // - Se window.ZUZU_JD_CONFIG.wcEnabled === true → usa WC AJAX // * "Adicionar" chama ?wc-ajax=add_to_cart (sem redirecionar) // * "Fechar pedido" redireciona para wc_get_cart_url() // - Caso contrário → drawer simulado original (toast + fake checkout) // ======================================================================= const { useState: useStateC, useEffect: useEffectC } = React; /* ---------- helpers ---------- */ function findBox(id) { return window.BOXES.find((b) => b.id === id); } const cfg = window.ZUZU_JD_CONFIG || {}; const wcEnabled = !!cfg.wcEnabled; const wcIds = cfg.wcIds || {}; const wcCartUrl = cfg.wcCartUrl || '/cart'; /* ===================== WC AJAX helpers ===================== */ async function wcAddToCart(productId) { const ajaxUrl = (window.wc_add_to_cart_params?.wc_ajax_url || '/?wc-ajax=%%endpoint%%') .replace('%%endpoint%%', 'add_to_cart'); const body = new URLSearchParams({ product_id: productId, quantity: 1 }); try { const res = await fetch(ajaxUrl, { method : 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' }, body : body.toString(), }); const json = await res.json(); return !json.error; } catch { return false; } } /* ===================== BoxModal ===================== */ function BoxModal({ openId, onClose, onAdd, inCart }) { const box = openId ? findBox(openId) : null; const [activeThumb, setActiveThumb] = useStateC(0); const [loading, setLoading] = useStateC(false); useEffectC(() => { setActiveThumb(0); }, [openId]); useEffectC(() => { function onEsc(e) { if (e.key === 'Escape') onClose(); } if (openId) window.addEventListener('keydown', onEsc); return () => window.removeEventListener('keydown', onEsc); }, [openId, onClose]); async function handleAdd() { if (!box) return; setLoading(true); await onAdd(box.id); setLoading(false); } return (
{box.poetic} {box.tagline}