// src/components/product/ProductInfo.tsx 'use client'; import { useState } from 'react'; 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 { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select'; import { Separator } from '@/components/ui/separator'; import { ShoppingBag, Heart, Share2, Truck, Shield, RotateCcw, Star, Plus, Minus } from 'lucide-react'; import { cn } from '@/lib/utils'; interface ProductInfoProps { product: WooCommerceProduct; } interface ColorOption { name: string; value: string; hex: string; } interface SizeOption { name: string; value: string; available: boolean; } export default function ProductInfo({ product }: ProductInfoProps): JSX.Element { const { addToCart, isInCart, getItemQuantity } = useCart(); const [selectedColor, setSelectedColor] = useState(''); const [selectedSize, setSelectedSize] = useState(''); const [quantity, setQuantity] = useState(1); const [isAddingToCart, setIsAddingToCart] = useState(false); const [isWishlisted, setIsWishlisted] = useState(false); const discountPercentage = getDiscountPercentage(product); const productInCart = isInCart(product.id); const cartQuantity = getItemQuantity(product.id); // Extraire les couleurs des attributs du produit const colorAttribute = product.attributes?.find(attr => attr.name.toLowerCase().includes('couleur') || attr.name.toLowerCase().includes('color') ); const sizeAttribute = product.attributes?.find(attr => attr.name.toLowerCase().includes('taille') || attr.name.toLowerCase().includes('size') ); const colorOptions: ColorOption[] = colorAttribute?.options.map(color => ({ name: color, value: color.toLowerCase(), hex: getColorHex(color), })) || []; const sizeOptions: SizeOption[] = sizeAttribute?.options.map(size => ({ name: size, value: size.toLowerCase(), available: true, // Vous pouvez ajouter la logique de disponibilité ici })) || []; 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: product.images[0]?.src || '/placeholder-product.jpg', }, quantity); // Simulation d'une petite attente pour l'UX await new Promise(resolve => setTimeout(resolve, 500)); } finally { setIsAddingToCart(false); } }; const increaseQuantity = (): void => { setQuantity(prev => prev + 1); }; const decreaseQuantity = (): void => { setQuantity(prev => Math.max(1, prev - 1)); }; const toggleWishlist = (): void => { setIsWishlisted(!isWishlisted); }; const shareProduct = (): void => { if (navigator.share) { navigator.share({ title: product.name, text: product.short_description, url: window.location.href, }); } else { // Fallback: copier l'URL dans le presse-papiers navigator.clipboard.writeText(window.location.href); } }; return (
{/* Product Header */}
{/* Categories */} {product.categories.length > 0 && (
{product.categories.map((category) => ( {category.name} ))}
)} {/* Title */}

{product.name}

{/* Ratings */} {product.rating_count > 0 && (
{[...Array(5)].map((_, i) => ( ))}
{product.average_rating} ({product.rating_count} avis)
)} {/* Price */}
{isOnSale(product) ? ( <> {formatPrice(product.sale_price)} {formatPrice(product.regular_price)} {discountPercentage > 0 && ( -{discountPercentage}% )} ) : ( {formatPrice(product.price)} )}
{/* Short Description */} {product.short_description && (
)}
{/* Product Options */}
{/* Colors */} {colorOptions.length > 0 && (
{colorOptions.map((color) => (
)} {/* Sizes */} {sizeOptions.length > 0 && (
)} {/* Quantity */}
{quantity}
{cartQuantity > 0 && ( {cartQuantity} déjà dans le panier )}
{/* Action Buttons */}
{/* Product Features */}
Livraison gratuite à partir de 50.000 MRU
Garantie qualité - Retour sous 30 jours
Échange gratuit en magasin
); } // Helper function pour les couleurs function getColorHex(colorName: string): string { const colorMap: { [key: string]: string } = { 'noir': '#000000', 'blanc': '#ffffff', 'rouge': '#dc2626', 'bleu': '#2563eb', 'vert': '#059669', 'jaune': '#d97706', 'violet': '#7c3aed', 'rose': '#ec4899', 'gris': '#6b7280', 'marron': '#92400e', 'beige': '#d6d3d1', 'doré': '#f59e0b', 'argenté': '#9ca3af', }; return colorMap[colorName.toLowerCase()] || '#9ca3af'; } function Label({ children, className = '' }: { children: React.ReactNode; className?: string }): JSX.Element { return ( ); }