"use client"; import { useState } from "react"; import { Minus, Plus, ShoppingBag } from "lucide-react"; import { customerPortalUrl } from "@/lib/config"; export function AddToCart({ variantId, maxQuantity }: { variantId: string; maxQuantity?: number | null }) { const [quantity, setQuantity] = useState(1); const [status, setStatus] = useState<"idle" | "loading" | "ok" | "error">("idle"); const cap = maxQuantity && maxQuantity > 0 ? maxQuantity : 99; const change = (delta: number) => { setQuantity((current) => Math.min(cap, Math.max(1, current + delta))); }; const add = async () => { const token = window.localStorage.getItem("accessToken") || window.localStorage.getItem("meshkeeToken") || window.localStorage.getItem("token"); if (!token) { window.location.href = customerPortalUrl; return; } setStatus("loading"); try { const response = await fetch("/api/cart/items", { method: "POST", headers: { Authorization: `Bearer ${token}`, "Content-Type": "application/json", }, body: JSON.stringify({ storeItemVariantId: variantId, quantity }), }); if (response.status === 401) { window.location.href = customerPortalUrl; return; } setStatus(response.ok ? "ok" : "error"); } catch { setStatus("error"); } }; return (
به سبد اضافه شد.
: null} {status === "error" ?افزودن به سبد انجام نشد.
: null}