// src/components/product/ProductCard.tsx 'use client'; import { useState } from 'react'; import Link from 'next/link'; import Image from 'next/image'; import { WooCommerceProduct } from '@/types/woocommerce'; import { useCartActions } from '@/hooks/useCartSync';import { formatPrice, isOnSale, getDiscountPercentage } from '@/lib/woocommerce'; import { Button } from '@/components/ui/button'; import { Badge } from '@/components/ui/badge'; import { Eye, ShoppingBag, Heart } from 'lucide-react'; import { cn } from '@/lib/utils'; interface ProductCardProps { product: WooCommerceProduct; className?: string; showQuickView?: boolean; } interface ColorOption { name: string; hex: string; } export default function ProductCard({ product, className = '', showQuickView = true }: ProductCardProps): JSX.Element { const { addToCart, isInCart } = useCart(); const [isHovered, setIsHovered] = useState(false); const [selectedColor, setSelectedColor] = useState(null); const [isAddingToCart, setIsAddingToCart] = useState(false); const primaryImage = product.images?.[0]?.src || '/placeholder-product.jpg'; const secondaryImage = product.images?.[1]?.src || primaryImage; const discountPercentage = getDiscountPercentage(product); // Extraire les couleurs des attributs du produit const colorAttribute = product.attributes?.find(attr => attr.name.toLowerCase().includes('couleur') || attr.name.toLowerCase().includes('color') ); const colorOptions: ColorOption[] = colorAttribute?.options.map(color => ({ name: color, hex: getColorHex(color), // Fonction helper pour obtenir le hex })) || []; const handleAddToCart = async (): Promise => { if (isAddingToCart) return; setIsAddingToCart(true); try { addToCart({ id: product.id, name: product.name, price: product.sale_price || product.regular_price, image: primaryImage, }); // Simulation d'une petite attente pour l'UX await new Promise(resolve => setTimeout(resolve, 300)); } finally { setIsAddingToCart(false); } }; return (
setIsHovered(true)} onMouseLeave={() => setIsHovered(false)} > {/* Image Container */}
{product.name} {/* Badges */}
{isOnSale(product) && ( -{discountPercentage}% )} {product.featured && ( Nouvelle Collection )}
{/* Quick Actions Overlay */} {showQuickView && (
)} {/* Wishlist Button */}
{/* Product Info */}

{product.name}

{/* Prix */}
{isOnSale(product) ? ( <> {formatPrice(product.sale_price)} {formatPrice(product.regular_price)} ) : ( {formatPrice(product.price)} )}
{/* Couleurs disponibles */} {colorOptions.length > 0 && (
{colorOptions.slice(0, 4).map((color, index) => (
)} {/* Add to Cart Button */}
); } // Fonction helper pour obtenir la couleur hex à partir du nom function getColorHex(colorName: string): string { const colorMap: { [key: string]: string } = { 'noir': '#000000', 'black': '#000000', 'blanc': '#ffffff', 'white': '#ffffff', 'rouge': '#e74c3c', 'red': '#e74c3c', 'bleu': '#3498db', 'blue': '#3498db', 'vert': '#27ae60', 'green': '#27ae60', 'jaune': '#f1c40f', 'yellow': '#f1c40f', 'orange': '#e67e22', 'violet': '#8e44ad', 'purple': '#8e44ad', 'rose': '#e91e63', 'pink': '#e91e63', 'gris': '#95a5a6', 'gray': '#95a5a6', 'grey': '#95a5a6', 'marron': '#8B4513', 'brown': '#8B4513', 'beige': '#F5F5DC', 'doré': '#FFD700', 'gold': '#FFD700', 'argenté': '#C0C0C0', 'silver': '#C0C0C0', }; const normalizedName = colorName.toLowerCase().trim(); return colorMap[normalizedName] || '#9CA3AF'; // Couleur par défaut }