"use client"; import { useState, useEffect } from "react"; interface CartItem { id: string; product_slug: string; product_name: string; variant: string | null; quantity: number; unit_price: number; subtotal: number; } interface Cart { id: string; items: CartItem[]; item_count: number; subtotal: number; } export default function CartPage() { const [cart, setCart] = useState(null); const [loading, setLoading] = useState(true); const [checkingOut, setCheckingOut] = useState(false); useEffect(() => { const cartId = localStorage.getItem("cart_id"); if (cartId) { fetch( `${process.env.NEXT_PUBLIC_API_URL || "http://localhost:8000/api"}/cart/${cartId}` ) .then((res) => (res.ok ? res.json() : null)) .then(setCart) .finally(() => setLoading(false)); } else { setLoading(false); } }, []); const handleCheckout = async () => { if (!cart) return; setCheckingOut(true); try { const res = await fetch( `${process.env.NEXT_PUBLIC_API_URL || "http://localhost:8000/api"}/checkout/session`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ cart_id: cart.id, success_url: `${window.location.origin}/checkout/success`, cancel_url: `${window.location.origin}/cart`, }), } ); if (res.ok) { const { checkout_url } = await res.json(); window.location.href = checkout_url; } } catch (error) { console.error("Checkout error:", error); } finally { setCheckingOut(false); } }; if (loading) { return (

Loading cart...

); } if (!cart || cart.items.length === 0) { return (

Your Cart

Your cart is empty.

Continue Shopping
); } return (

Your Cart

{cart.items.map((item) => (

{item.product_name}

{item.variant && (

Variant: {item.variant}

)}

Qty: {item.quantity}

${item.subtotal.toFixed(2)}

))}

Order Summary

Subtotal ${cart.subtotal.toFixed(2)}
Shipping Calculated at checkout
Total ${cart.subtotal.toFixed(2)}
); }