import { useEffect, useState } from 'react'; import { useParams } from 'react-router-dom'; import { supabase } from '@/integrations/supabase/client'; import { Layout } from '@/components/Layout'; import { Card, CardContent } from '@/components/ui/card'; import { Badge } from '@/components/ui/badge'; import { Button } from '@/components/ui/button'; import { useCart } from '@/contexts/CartContext'; import { toast } from '@/hooks/use-toast'; import { Skeleton } from '@/components/ui/skeleton'; interface Product { id: string; title: string; slug: string; type: string; description: string; content: string; price: number; sale_price: number | null; } export default function ProductDetail() { const { slug } = useParams<{ slug: string }>(); const [product, setProduct] = useState(null); const [loading, setLoading] = useState(true); const { addItem, items } = useCart(); useEffect(() => { if (slug) fetchProduct(); }, [slug]); const fetchProduct = async () => { const { data, error } = await supabase .from('products') .select('*') .eq('slug', slug) .eq('is_active', true) .maybeSingle(); if (error || !data) { toast({ title: 'Error', description: 'Product not found', variant: 'destructive' }); } else { setProduct(data); } setLoading(false); }; const handleAddToCart = () => { if (!product) return; addItem({ id: product.id, title: product.title, price: product.price, sale_price: product.sale_price, type: product.type, }); toast({ title: 'Added to cart', description: `${product.title} has been added to your cart` }); }; const isInCart = product ? items.some(item => item.id === product.id) : false; if (loading) { return (
); } if (!product) { return (

Product not found

); } return (

{product.title}

{product.type}
{product.sale_price ? (
${product.sale_price} ${product.price}
) : ( ${product.price} )}

{product.description}

No content available

' }} />
); }