"use client"; import { useState, useEffect } from "react"; import { Star, ChevronLeft, Check } from "lucide-react"; import { Button } from "@/components/ui/button"; import { useLanguage } from "@/contexts/LanguageContext"; import { useCart } from "@/contexts/CartContext"; import { getProduct, listProducts } from "@/lib/api/products"; import type { Product } from "@/data/products"; import ProductCard from "@/components/ProductCard"; import Link from "next/link"; import { useParams } from "next/navigation"; import { toast } from "sonner"; export default function ProductDetail() { const { id } = useParams(); const { t } = useLanguage(); const { addItem } = useCart(); const [product, setProduct] = useState(null); const [similar, setSimilar] = useState([]); const [loading, setLoading] = useState(true); const [selectedImage, setSelectedImage] = useState(0); const [selectedColor, setSelectedColor] = useState(""); const [selectedLength, setSelectedLength] = useState(""); useEffect(() => { if (!id) return; setLoading(true); getProduct(id as string) .then((p) => { setProduct(p); setSelectedImage(0); setSelectedColor(p.colors[0] ?? ""); setSelectedLength(p.lengths[0] ?? ""); return listProducts({ category: p.category, exclude: p.id, per_page: 4 }); }) .then((res) => setSimilar(res.data)) .catch((e) => console.error("[produit] fetch failed:", e)) .finally(() => setLoading(false)); }, [id]); if (loading) { return (
); } if (!product) { return (

{t("product.not_found")}

); } const handleAddToCart = () => { const color = selectedColor || product.colors[0]; const length = selectedLength || product.lengths[0]; addItem(product, color, length); toast.success(`${t("product.added_to_cart")} — ${product.name} — ${color}, ${length}`); }; return (
{t("nav.shop")}
{product.name}
{product.images.length > 1 && (
{product.images.map((img, i) => ( ))}
)}

{product.name}

{Array.from({ length: 5 }).map((_, i) => ( ))}
({product.reviewCount} {t("product.reviews")})
{product.price} € {product.originalPrice && ( {product.originalPrice} € )}

{product.description}

{t("product.color")}

{product.colors.map((color) => ( ))}

{t("product.length")}

{product.lengths.map((length) => ( ))}
{product.features.length > 0 && (

{t("product.features")}

    {product.features.map((feature) => (
  • {feature}
  • ))}
)}
{similar.length > 0 && (

{t("product.similar")}

{similar.map((p) => ( ))}
)}
); }