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 (
{itemCount > 0 ? `${itemCount} item${itemCount > 1 ? 's' : ''} in your cart` : 'Your cart is empty'}
Add some items to get started!
Sold by {item.product?.store?.name || item.product?.seller?.username || 'Unknown Seller'}
₹{item.product ? parseFloat(item.product.price).toFixed(2) : '0.00'}