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!

); } return (
{/* Header */}

Shopping Cart

{/* Cart Items */}
{cart.items.map((item: CartItem) => (
{/* Product Image */} {elements.product_images && (
{item.image ? ( {item.name} ) : (
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, parseInt(e.target.value) || 1) } className="w-16 text-center border rounded py-1" min="1" />
{/* Item Total & Remove */}

{formatPrice(item.price * item.quantity)}

))}
{/* Cart Summary */}

Cart Summary

{/* Coupon Field */} {elements.coupon_field && (
)} {/* Shipping Calculator */} {elements.shipping_calculator && (

Calculate Shipping

)}
Subtotal {formatPrice(total)}
Shipping Calculated at checkout
Total {formatPrice(total)}
{elements.continue_shopping_button && ( )}
{/* Clear Cart Confirmation Dialog */} Clear Cart? Are you sure you want to remove all items from your cart? This action cannot be undone.
); }