'use client' import { useState } from 'react' import { Button } from '@/components/ui/button' import { ShoppingCart, CheckCircle, Loader2 } from 'lucide-react' import { useCart } from '@/lib/cart/context' import { toast } from 'sonner' import type { Product } from '@/lib/products/types' interface AddToCartButtonProps { product: Product } export function AddToCartButton({ product }: AddToCartButtonProps) { const { addItem } = useCart() const [isAdding, setIsAdding] = useState(false) const [isAdded, setIsAdded] = useState(false) const handleAddToCart = () => { if (isAdding || isAdded) return setIsAdding(true) setIsAdded(false) try { addItem({ id: product.id, name: product.name, price: product.price, priceId: product.priceId!, image: product.images?.[0] || '/placeholder.svg', quantity: 1, }) // Show success feedback setIsAdded(true) toast.success(`${product.name} added to cart!`) // Reset states after showing feedback setTimeout(() => { setIsAdding(false) setIsAdded(false) }, 2000) } catch (error) { console.error('Error adding to cart:', error) toast.error('Failed to add item to cart') setIsAdding(false) } } if (!product.priceId) { return ( Product unavailable ) } return ( {isAdding ? ( ) : isAdded ? ( ) : ( )} {isAdding ? 'Adding...' : isAdded ? 'Added!' : 'Add to Cart'} ) }