"use client"; import React, { useEffect, useRef, useState } from "react"; import { TicketPercent, X } from "lucide-react"; import { apiFetch, ApiError } from "@/lib/api"; export type BasketItem = { eventOptionId: string; variantId?: string | null; quantity: number }; export type AppliedCoupon = { code: string; summary: string; discount: number; subtotal: number; total: number; }; /** * "Have a code?" field. Checks the code against the current basket with * POST /api/coupons/validate and reports the result through `onChange` — the parent * sends `applied.code` as `couponCode` when it registers. Re-checks automatically when * the basket changes, since the discount (or whether the code applies at all) can change. * * Staff checking on someone's behalf pass `forUserId` (the attendee), or `null` for a * walk-in with no account yet. */ export function CouponCodeField({ eventId, basket, token, forUserId, applied, onChange, compact = false, }: { eventId: string; basket: BasketItem[]; token?: string | null; forUserId?: string | null; applied: AppliedCoupon | null; onChange: (applied: AppliedCoupon | null) => void; compact?: boolean; }) { const [open, setOpen] = useState(!!applied); const [code, setCode] = useState(applied?.code || ""); const [checking, setChecking] = useState(false); const [error, setError] = useState(null); const items = basket.filter(b => b.quantity > 0); const basketKey = JSON.stringify(items); const lastKey = useRef(basketKey); const check = async (value: string) => { const trimmed = value.trim(); if (!trimmed) return; if (items.length === 0) { setError("Choose your tickets first."); return; } setChecking(true); setError(null); try { const body: Record = { code: trimmed, eventId, options: items }; if (forUserId !== undefined) body.forUserId = forUserId; const res = await apiFetch("/api/coupons/validate", { method: "POST", authToken: token || undefined, body }); onChange({ code: res.code, summary: res.summary, discount: res.discount, subtotal: res.subtotal, total: res.total }); } catch (e) { onChange(null); setError(e instanceof ApiError ? e.message : "Couldn't check that code. Please try again."); } finally { setChecking(false); } }; // Basket changed after a code was applied: check it again for the new items. useEffect(() => { if (lastKey.current === basketKey) return; lastKey.current = basketKey; if (applied) check(applied.code); // eslint-disable-next-line react-hooks/exhaustive-deps }, [basketKey]); // Staff switched attendee: restricted codes depend on who it's for. useEffect(() => { if (applied) check(applied.code); // eslint-disable-next-line react-hooks/exhaustive-deps }, [forUserId]); const remove = () => { onChange(null); setCode(""); setError(null); }; if (!open) { return ( ); } if (applied) { return (
{applied.code} — {applied.summary} {applied.discount > 0 && (−R {applied.discount.toFixed(2)})}
); } return (
{!compact && }
{ setCode(e.target.value.toUpperCase()); setError(null); }} onKeyDown={e => { if (e.key === "Enter") { e.preventDefault(); check(code); } }} placeholder="Enter code" className="flex-1 min-w-0 border rounded-lg px-3 py-2 text-sm font-mono uppercase focus:outline-none focus:ring-2 focus:ring-brand-400" aria-label="Discount code" />
{error &&

{error}

}
); }