"use client"; import { Stamp } from "lucide-react"; import { useRouter } from "next/navigation"; import { useState, useTransition } from "react"; import { Button } from "@/components/ui/button"; import { Modal } from "@/components/ui/modal"; import { Input } from "@/components/ui/input"; import { cn } from "@/lib/utils/cn"; import { addLoyaltyStampAction } from "../actions/loyalty-actions"; export function AddStampButton({ customerId, appointmentId, className, size = "sm", }: { customerId: string; appointmentId?: string; className?: string; size?: "sm" | "md" | "lg"; }) { const router = useRouter(); const [open, setOpen] = useState(false); const [amount, setAmount] = useState("1"); const [reason, setReason] = useState(""); const [error, setError] = useState(null); const [result, setResult] = useState<{ rewardCreated?: boolean } | null>(null); const [isPending, startTransition] = useTransition(); const close = () => { const shouldRefresh = !!result; setOpen(false); setResult(null); setError(null); setAmount("1"); setReason(""); if (shouldRefresh) router.refresh(); }; const submit = () => { const parsed = Number(amount); if (!Number.isInteger(parsed) || parsed <= 0) { setError("Enter a positive number of stamps."); return; } setError(null); startTransition(async () => { const res = await addLoyaltyStampAction({ customerId, amount: parsed, appointmentId, reason: reason || undefined, }); if (!res.success) { setError(res.error ?? "Couldn't add a stamp."); return; } setResult({ rewardCreated: res.rewardCreated }); }); }; return ( <> {result ? (

The stamp has been recorded on this customer's loyalty account.

{result.rewardCreated && (

They've now reached the stamp threshold — a reward is available.

)}
) : (
setAmount(e.target.value)} /> setReason(e.target.value)} /> {error && (

{error}

)}
)}
); }