import { useState, useEffect } from 'react' import { useParams, Link } from 'react-router-dom' import Navbar from '../components/Navbar' import Footer from '../components/Footer' import ProductCard from '../components/ProductCard' import { API_BASE, getCategoryImage, formatPrice, type Category, type Product } from '../types' import { useCartStore } from '../store/cartStore' export default function ProductDetailPage() { const { id } = useParams<{ id: string }>() const [categories, setCategories] = useState([]) const [product, setProduct] = useState(null) const [related, setRelated] = useState([]) const [selectedVariant, setSelectedVariant] = useState(0) const [loading, setLoading] = useState(true) const [error, setError] = useState(null) const { addToCart } = useCartStore() useEffect(() => { fetch(`${API_BASE}/categories`) .then((r) => r.json()) .then(setCategories) .catch(console.error) }, []) useEffect(() => { if (!id) return setLoading(true) setError(null) setSelectedVariant(0) fetch(`${API_BASE}/products/${id}`) .then(async (r) => { if (!r.ok) throw new Error('Product not found') return r.json() }) .then(async (data: Product) => { setProduct(data) // Fetch related (same category, different product) const relRes = await fetch(`${API_BASE}/products?categoryId=${data.category.id}`) const relJson = await relRes.json() const relData: Product[] = relJson.data || [] setRelated(relData.filter((p) => p.id !== data.id).slice(0, 6)) }) .catch((err) => setError(err.message)) .finally(() => setLoading(false)) }, [id]) if (loading) { return (
) } if (error || !product) { return (

⚠️

{error ?? 'Product not found'}

Back to Shop
) } const currentVariant = product.variants[selectedVariant] const handleAddToCart = () => { if (!product || !currentVariant) return addToCart({ productId: product.id, variantId: currentVariant.id, quantity: 1, price: currentVariant.price || product.price || 0, productName: product.scent.name, variantSize: currentVariant.size, productImage: product.images?.[0]?.url || getCategoryImage(product.category.name) }) } return (
{/* Breadcrumb */}
Home / Shop / {product.category.name} / {product.scent.name}
{/* Product Detail */}
{/* Image */}
{product.scent.name} { (e.target as HTMLImageElement).style.display = 'none' }} />
{product.category.name}
{/* Info */}

{product.category.name}

{product.scent.name}

Premium Collection

{product.description && (

{product.description}

)} {/* Price */}
{formatPrice(currentVariant?.price)} {currentVariant && ( / {currentVariant.size} )}
{/* Variant Selector */}

Select Size

{product.variants.map((v, i) => ( ))}
{/* Actions */}
{/* Meta */}
Scent {product.scent.name}
Category {product.category.name}
{/* Full Details Section */} {product.details && (

Product Details

)} {/* Related Products */} {related.length > 0 && (

More {product.category.name}

Related Products

View All →
{related.map((p) => ( ))}
)}
) }