import React, { useState } from 'react'; import { Link, useNavigate } from 'react-router-dom'; import { useCartStore, type CartItem } from '@/lib/cart/store'; import { useCartSettings } from '@/hooks/useAppearanceSettings'; import { Button } from '@/components/ui/button'; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from '@/components/ui/dialog'; import Container from '@/components/Layout/Container'; import { formatPrice } from '@/lib/currency'; import { Trash2, Plus, Minus, ShoppingBag, ArrowLeft } from 'lucide-react'; import { toast } from 'sonner'; export default function Cart() { const navigate = useNavigate(); const { cart, removeItem, updateQuantity, clearCart } = useCartStore(); const { layout, elements } = useCartSettings(); const [showClearDialog, setShowClearDialog] = useState(false); // Calculate total from items const total = cart.items.reduce((sum, item) => sum + (item.price * item.quantity), 0); const handleUpdateQuantity = (key: string, newQuantity: number) => { if (newQuantity < 1) { handleRemoveItem(key); return; } updateQuantity(key, newQuantity); }; const handleRemoveItem = (key: string) => { removeItem(key); toast.success('Item removed from cart'); }; const handleClearCart = () => { clearCart(); setShowClearDialog(false); toast.success('Cart cleared'); }; if (cart.items.length === 0) { return ( Your cart is empty Add some products to get started! navigate('/shop')}> Continue Shopping ); } return ( {/* Header */} Shopping Cart setShowClearDialog(true)}> Clear Cart {/* Cart Items */} {cart.items.map((item: CartItem) => ( {/* Product Image */} {elements.product_images && ( {item.image ? ( ) : ( No Image )} )} {/* Product Info */} {item.name} {/* Variation Attributes */} {item.attributes && Object.keys(item.attributes).length > 0 && ( {Object.entries(item.attributes).map(([key, value]) => { // Format attribute name: capitalize first letter const formattedKey = key.charAt(0).toUpperCase() + key.slice(1); return ( {formattedKey}: {value} ); })} )} {formatPrice(item.price)} {/* Quantity Controls */} handleUpdateQuantity(item.key, item.quantity - 1)} className="font-[inherit] p-1 hover:bg-gray-100 rounded" > handleUpdateQuantity(item.key, parseInt(e.target.value) || 1) } className="w-16 text-center border rounded py-1" min="1" /> handleUpdateQuantity(item.key, item.quantity + 1)} className="font-[inherit] p-1 hover:bg-gray-100 rounded" > {/* Item Total & Remove */} handleRemoveItem(item.key)} className="font-[inherit] text-red-600 hover:text-red-700 p-2" > {formatPrice(item.price * item.quantity)} ))} {/* Cart Summary */} Cart Summary {/* Coupon Field */} {elements.coupon_field && ( Coupon Code Apply )} {/* Shipping Calculator */} {elements.shipping_calculator && ( Calculate Shipping Calculate )} Subtotal {formatPrice(total)} Shipping Calculated at checkout Total {formatPrice(total)} navigate('/checkout')} size="lg" className="w-full mb-3" > Proceed to Checkout {elements.continue_shopping_button && ( navigate('/shop')} variant="outline" className="w-full" > Continue Shopping )} {/* Clear Cart Confirmation Dialog */} Clear Cart? Are you sure you want to remove all items from your cart? This action cannot be undone. setShowClearDialog(false)}> Cancel Clear Cart ); }
Add some products to get started!
{formatPrice(item.price)}
{formatPrice(item.price * item.quantity)}