import { useState } from "react"; import { useNavigate } from "react-router-dom"; import { useCart } from "../context/CartContext"; import "./ProductCard.css"; interface ProductCardProps { id: number; name: string; price: number; unit: string; image: string; stock: number; category: string; prices?: Array<{ quantity: number; price: number }>; hasVideo?: boolean; videoUrl?: string; // ✨ Nouveau prop pour l'URL de la vidéo } function ProductCard({ id, name, price, unit = "g", image, stock, category = "autre", prices, hasVideo = false, videoUrl, }: ProductCardProps) { const navigate = useNavigate(); const { addToCart } = useCart(); const [showQuantitySelect, setShowQuantitySelect] = useState(false); const [selectedQuantity, setSelectedQuantity] = useState( null, ); const [showSuccess, setShowSuccess] = useState(false); const [showVideo, setShowVideo] = useState(false); // ✨ État pour afficher/masquer la vidéo const isOutOfStock = stock === 0; const normalizedCategory = (category || "autre").toLowerCase().trim(); const handleDetailsClick = (e: React.MouseEvent) => { e.stopPropagation(); navigate(`/user/product/${id}`); }; // ✨ Handler pour afficher/masquer la vidéo const handleVideoToggle = (e: React.MouseEvent) => { e.stopPropagation(); setShowVideo(!showVideo); }; // ✨ Handler pour fermer la vidéo const handleCloseVideo = (e: React.MouseEvent) => { e.stopPropagation(); setShowVideo(false); }; const handleQuickAddClick = (e: React.MouseEvent) => { e.stopPropagation(); console.log("🔘 Bouton cliqué", { id, name, category: normalizedCategory, isOutOfStock, prices: prices?.length || 0, showQuantitySelect, }); if (!isOutOfStock && prices && prices.length > 0) { setShowQuantitySelect(!showQuantitySelect); } }; const handleQuantitySelect = (e: React.ChangeEvent) => { const selectedGrams = Number(e.target.value); setSelectedQuantity(selectedGrams); const priceOption = prices?.find((p) => p.quantity === selectedGrams); if (priceOption) { console.log("💾 [ProductCard] Ajout au panier:", { id, name, category: normalizedCategory, quantity: selectedGrams, price: priceOption.price, }); addToCart({ product_id: id, name_product: name, price: priceOption.price, quantity: selectedGrams, category: normalizedCategory, image: image, }); setShowSuccess(true); setTimeout(() => { setShowSuccess(false); setShowQuantitySelect(false); setSelectedQuantity(null); }, 1500); } }; return (
{name} {/* ✨ Icône caméra cliquable */} {hasVideo && !isOutOfStock && videoUrl && ( )} {/* ✨ Bouton "Détails" */} {isOutOfStock && (
SOLD OUT
)}

{name}

{price > 0 ? `${price.toFixed(2)} €` : "Prix non disponible"}

e.stopPropagation()} > {showSuccess ? (
✓ Ajouté !
) : ( <> {showQuantitySelect && prices && prices.length > 0 && ( )} )}
{/* ✨ Modal vidéo */} {showVideo && videoUrl && (
e.stopPropagation()} >
)}
); } export default ProductCard;