"use client"; import { useEffect, useState } from "react"; import { useSession } from "next-auth/react"; import DesktopHeader from "../../components/DesktopHeader"; import MobileHeader from "../../components/MobileHeader"; import Footer from "../../components/Footer"; type CartItem = { id: string; name: string; unitAmount: number; currency: string; quantity: number; }; type Cart = { id: string; items: CartItem[]; }; export default function CartPage() { const [slug, setSlug] = useState("storeshifted"); const [cart, setCart] = useState(null); const [loading, setLoading] = useState(false); const [message, setMessage] = useState(""); const { data: session, status } = useSession(); const isAuthenticated = status === "authenticated"; const [addressTo, setAddressTo] = useState({ name: "", street1: "", city: "", state: "", zip: "", country: "US", phone: "", }); useEffect(() => { const stored = window.localStorage.getItem("connectStoreSlug"); if (stored) setSlug(stored); }, []); async function loadCart(currentSlug: string) { setLoading(true); setMessage(""); try { const res = await fetch(`/api/cart/get?slug=${currentSlug}`); const data = await res.json(); if (!res.ok) throw new Error(data.error || "Failed to load cart"); setCart(data.cart); } catch (err: any) { setMessage(err.message || "Failed to load cart."); } finally { setLoading(false); } } useEffect(() => { loadCart(slug); }, [slug]); async function updateQty(itemId: string, quantity: number) { setLoading(true); try { const res = await fetch("/api/cart/update", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ itemId, quantity }), }); const data = await res.json(); if (!res.ok) throw new Error(data.error || "Failed to update cart"); await loadCart(slug); } catch (err: any) { setMessage(err.message || "Failed to update cart."); } finally { setLoading(false); } } async function removeItem(itemId: string) { setLoading(true); try { const res = await fetch("/api/cart/remove", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ itemId }), }); const data = await res.json(); if (!res.ok) throw new Error(data.error || "Failed to remove item"); await loadCart(slug); } catch (err: any) { setMessage(err.message || "Failed to remove item."); } finally { setLoading(false); } } async function checkout() { setLoading(true); setMessage(""); try { const res = await fetch("/api/cart/checkout", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ slug, shippingAddress: addressTo, }), }); const data = await res.json(); if (!res.ok) throw new Error(data.error || "Checkout failed"); window.location.href = data.url; } catch (err: any) { setMessage(err.message || "Checkout failed."); } finally { setLoading(false); } } const total = cart?.items.reduce((sum, item) => sum + item.unitAmount * item.quantity, 0) ?? 0; return (

Cart

{isAuthenticated ? (

Signed in as {session!.user.email}

) : (

Sign in to save your cart across devices. Sign in

)} {message ?
{message}
: null}
{cart?.items.map((item) => (
{item.name}
{(item.unitAmount / 100).toFixed(2)} {item.currency.toUpperCase()}
updateQty(item.id, Number(e.target.value))} />
))} {!cart?.items.length ? (
Your cart is empty.
) : null}

Shipping

Ship To

setAddressTo({ ...addressTo, name: e.target.value })} placeholder="Name" /> setAddressTo({ ...addressTo, street1: e.target.value }) } placeholder="Street" /> setAddressTo({ ...addressTo, city: e.target.value })} placeholder="City" /> setAddressTo({ ...addressTo, state: e.target.value }) } placeholder="State/Province" /> setAddressTo({ ...addressTo, zip: e.target.value })} placeholder="Postal Code" /> setAddressTo({ ...addressTo, country: e.target.value }) } placeholder="Country" />
Total: {(total / 100).toFixed(2)} USD
); }