import { useLocation } from "wouter";
import { useAuth } from "@/hooks/use-auth";
import { useCart } from "@/hooks/use-cart";
import Header from "@/components/layout/header";
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, isLoading } = useAuth();
const [searchQuery, setSearchQuery] = useState("");
const { items, itemCount, subtotal, tax, total, updateQuantity, removeItem } = useCart();
const { toast } = useToast();
// Show loading while authentication is being verified
if (isLoading) {
return (
);
}
// Redirect if not authenticated after loading completes
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 */}
{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 ? (

) : (
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 && (
Subtotal ({itemCount} items):
₹{subtotal.toFixed(2)}
Shipping:
Free
Tax:
₹{tax.toFixed(2)}
Total:
₹{total.toFixed(2)}
)}
);
}