import { useLocation } from "wouter"; import { useAuth } from "@/hooks/use-auth"; import { useCart } from "@/hooks/use-cart"; import Header from "@/components/layout/header"; import { Button } from "@/components/ui/button"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { Separator } from "@/components/ui/separator"; import { ShoppingCart, Plus, Minus, Trash2, ArrowLeft } from "lucide-react"; import { useToast } from "@/hooks/use-toast"; import { useState } from "react"; export default function Cart() { const [, setLocation] = useLocation(); const { user } = useAuth(); const [searchQuery, setSearchQuery] = useState(""); const { items, itemCount, subtotal, tax, total, updateQuantity, removeItem } = useCart(); const { toast } = useToast(); // Redirect if not authenticated if (!user) { setLocation('/auth'); return null; } const handleUpdateQuantity = async (id: string, newQuantity: number) => { if (newQuantity < 1) return; try { await updateQuantity(id, newQuantity); } catch (error) { toast({ variant: "destructive", title: "Error", description: "Failed to update quantity.", }); } }; const handleRemoveItem = async (id: string) => { try { await removeItem(id); toast({ title: "Item removed", description: "Item has been removed from your cart.", }); } catch (error) { toast({ variant: "destructive", title: "Error", description: "Failed to remove item.", }); } }; const handleCheckout = () => { setLocation('/checkout'); }; return (
{/* Back Button */}
{/* Header */}

Shopping Cart

{itemCount > 0 ? `${itemCount} item${itemCount > 1 ? 's' : ''} in your cart` : 'Your cart is empty'}

{/* Cart Content */}
{/* Cart Items */}
Cart Items {items.length === 0 ? (

Your cart is empty

Add some items to get started!

) : (
{items.map((item) => (
{/* Product Image */}
{item.product?.images && item.product.images.length > 0 ? ( {item.product.title} ) : (
No Image
)}
{/* Product Details */}

{item.product?.title || 'Unknown Product'}

Sold by {item.product?.store?.name || item.product?.seller?.username || 'Unknown Seller'}

₹{item.product ? parseFloat(item.product.price).toFixed(2) : '0.00'}

{/* Quantity Controls and Remove */}
{/* Quantity Controls */}
{item.quantity}
{/* Remove Button */}
))}
)}
{/* Order Summary */} {items.length > 0 && (
Order Summary
Subtotal ({itemCount} items): ₹{subtotal.toFixed(2)}
Shipping: Free
Tax: ₹{tax.toFixed(2)}
Total: ₹{total.toFixed(2)}
)}
); }