"use client"; import React, { useMemo, useState } from "react"; import { Users, Send } from "lucide-react"; import { apiFetch } from "@/lib/api"; type Attendee = { id: string; eventOptionId: string; variantId: string | null; name: string; email: string | null; phone: string | null; ticket?: { id: string; isUsed: boolean } | null }; type Row = { id?: string; eventOptionId: string; variantId: string | null; label: string; name: string; email: string; phone: string; locked: boolean }; /** * Named attendees on a registration (group tickets): shows who each main-ticket place * is for, and lets the payer (or staff) name places, change details, choose where the * tickets go, and send an attendee the link to their own form. */ export function AttendeesPanel({ registration, token, canEdit, hasForm, onSaved, onMessage }: { registration: any; token: string; canEdit: boolean; hasForm: boolean; onSaved: () => void; onMessage: (msg: string) => void; }) { const attendees: Attendee[] = registration.attendees || []; const [editing, setEditing] = useState(false); const [rows, setRows] = useState([]); const [delivery, setDelivery] = useState<"payer" | "each">(registration.ticketDelivery || "payer"); const [busy, setBusy] = useState(false); const [error, setError] = useState(null); // One row per main-ticket place: named ones first (in order), then blanks. const buildRows = useMemo(() => () => { const out: Row[] = []; for (const ro of registration.registrationOptions || []) { if (!ro.eventOption?.isMainTicket) continue; const label = `${ro.eventOption?.name || "Ticket"}${ro.variant?.name ? ` (${ro.variant.name})` : ""}`; const named = attendees.filter(a => a.eventOptionId === ro.eventOptionId && (a.variantId || null) === (ro.variantId || null)); for (let i = 0; i < (ro.quantity || 0); i++) { const a = named[i]; out.push({ id: a?.id, eventOptionId: ro.eventOptionId, variantId: ro.variantId || null, label: `${label} ${i + 1}`, name: a?.name || "", email: a?.email || "", phone: a?.phone || "", locked: !!a?.ticket?.isUsed, }); } } return out; }, [registration, attendees]); if (!registration.event?.allowNamedAttendees && attendees.length === 0) return null; const startEdit = () => { setRows(buildRows()); setDelivery(registration.ticketDelivery || "payer"); setError(null); setEditing(true); }; const update = (i: number, field: "name" | "email" | "phone", value: string) => setRows(prev => prev.map((r, j) => (j === i ? { ...r, [field]: value } : r))); const save = async () => { setBusy(true); setError(null); try { await apiFetch(`/api/registrations/${encodeURIComponent(registration.id)}/attendees`, { method: "PUT", authToken: token, body: { ticketDelivery: delivery, attendees: rows.filter(r => r.name.trim()).map(r => ({ ...(r.id ? { id: r.id } : {}), eventOptionId: r.eventOptionId, ...(r.variantId ? { variantId: r.variantId } : {}), name: r.name.trim(), email: r.email.trim() || undefined, phone: r.phone.trim() || undefined, })), }, }); setEditing(false); onMessage("Attendees saved."); onSaved(); } catch (e: any) { setError(e?.message || "Couldn't save attendees"); } finally { setBusy(false); } }; const sendLink = async (a: Attendee) => { try { const res = await apiFetch<{ message: string }>(`/api/registrations/${encodeURIComponent(registration.id)}/attendees/${encodeURIComponent(a.id)}/send-form-link`, { method: "POST", authToken: token }); onMessage(res.message); } catch (e: any) { setError(e?.message || "Couldn't send the form link"); } }; const answered = new Set((registration.formResponses || []).map((r: any) => r.attendeeId).filter(Boolean)); return (
People {canEdit && !editing && ( )}
{!editing ? ( attendees.length === 0 ? (

No names yet — add a name for each person to give them their own ticket.

) : ( <>
    {attendees.map(a => (
  • {a.name} {(a.email || a.phone) && · {[a.email, a.phone].filter(Boolean).join(" · ")}} {hasForm && {answered.has(a.id) ? "form done" : "form to do"}} {canEdit && hasForm && !answered.has(a.id) && (a.email || a.phone) && ( )}
  • ))}

Tickets go {registration.ticketDelivery === "each" ? "to each person (or you, if they have no contact details)" : "to you"}.

) ) : (
{rows.map((r, i) => (
{r.label}{r.locked && — checked in, can't be changed}
update(i, "name", e.target.value)} aria-label={`${r.label} name`} /> update(i, "email", e.target.value)} aria-label={`${r.label} email`} /> update(i, "phone", e.target.value)} aria-label={`${r.label} phone`} />
))}
{error &&

{error}

}
)} {!editing && error &&

{error}

}
); }