Archived
Events can take a name (and optional email/phone) for each main-ticket place. Each named person gets their own quantity-1 ticket and attendee form; unnamed places share one ticket. Tickets go to the payer or to each person, attendees matching one existing account are linked to it, and the payer can edit names and send form links from My Events. Ticket generation is now planned per line (utils/attendees.js), free registrations use the same generator, and guest registration matches accounts by phone too, refusing conflicting email/phone pairs. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
131 lines
5.8 KiB
TypeScript
131 lines
5.8 KiB
TypeScript
"use client";
|
|
|
|
import React from "react";
|
|
import { Users } from "lucide-react";
|
|
|
|
export type AttendeeOption = {
|
|
id: string;
|
|
name: string;
|
|
isMainTicket?: boolean;
|
|
variants?: { id: string; name: string }[] | null;
|
|
};
|
|
|
|
export type AttendeeEntry = { name: string; email: string; phone: string };
|
|
export type AttendeeEntries = Record<string, AttendeeEntry>;
|
|
export type TicketDelivery = "payer" | "each";
|
|
|
|
export type AttendeePayload = { eventOptionId: string; variantId?: string | null; name: string; email?: string; phone?: string };
|
|
|
|
type Slot = { key: string; eventOptionId: string; variantId: string | null; label: string };
|
|
|
|
/**
|
|
* One slot per main-ticket place in the basket. `qtyFor(optionId, variantId)` reads the
|
|
* caller's quantities (each page keys its quantity map differently).
|
|
*/
|
|
function slotsFor(options: AttendeeOption[], qtyFor: (optionId: string, variantId: string | null) => number): Slot[] {
|
|
const slots: Slot[] = [];
|
|
for (const o of options) {
|
|
if (!o.isMainTicket) continue;
|
|
const variants = o.variants || [];
|
|
const lines = variants.length > 0 ? variants.map(v => ({ variantId: v.id, label: `${o.name} (${v.name})` })) : [{ variantId: null, label: o.name }];
|
|
for (const line of lines) {
|
|
const qty = qtyFor(o.id, line.variantId);
|
|
for (let i = 0; i < qty; i++) {
|
|
slots.push({ key: `${o.id}::${line.variantId || ""}#${i}`, eventOptionId: o.id, variantId: line.variantId, label: `${line.label} ${i + 1}` });
|
|
}
|
|
}
|
|
}
|
|
return slots;
|
|
}
|
|
|
|
/** The named places as the API expects them (unnamed places are left out). */
|
|
export function attendeesPayload(options: AttendeeOption[], qtyFor: (optionId: string, variantId: string | null) => number, entries: AttendeeEntries): AttendeePayload[] {
|
|
return slotsFor(options, qtyFor)
|
|
.map(s => ({ s, e: entries[s.key] }))
|
|
.filter(({ e }) => e && e.name.trim())
|
|
.map(({ s, e }) => ({
|
|
eventOptionId: s.eventOptionId,
|
|
...(s.variantId ? { variantId: s.variantId } : {}),
|
|
name: e.name.trim(),
|
|
...(e.email.trim() ? { email: e.email.trim() } : {}),
|
|
...(e.phone.trim() ? { phone: e.phone.trim() } : {}),
|
|
}));
|
|
}
|
|
|
|
/**
|
|
* "Registering for other people?" — a name (and optional email/phone) for each main
|
|
* ticket place, plus who gets the tickets. Named places get their own ticket; any left
|
|
* blank share one ticket as usual.
|
|
*/
|
|
export function AttendeeNamesEditor({
|
|
options,
|
|
qtyFor,
|
|
enabled,
|
|
onEnabledChange,
|
|
entries,
|
|
onEntriesChange,
|
|
delivery,
|
|
onDeliveryChange,
|
|
payerLabel = "me",
|
|
}: {
|
|
options: AttendeeOption[];
|
|
qtyFor: (optionId: string, variantId: string | null) => number;
|
|
enabled: boolean;
|
|
onEnabledChange: (v: boolean) => void;
|
|
entries: AttendeeEntries;
|
|
onEntriesChange: (v: AttendeeEntries) => void;
|
|
delivery: TicketDelivery;
|
|
onDeliveryChange: (v: TicketDelivery) => void;
|
|
payerLabel?: string;
|
|
}) {
|
|
const slots = slotsFor(options, qtyFor);
|
|
const set = (key: string, field: keyof AttendeeEntry, value: string) => {
|
|
const prev = entries[key] || { name: "", email: "", phone: "" };
|
|
onEntriesChange({ ...entries, [key]: { ...prev, [field]: value } });
|
|
};
|
|
const inputCls = "w-full border rounded-lg px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-brand-400";
|
|
|
|
return (
|
|
<div className="border rounded-xl p-4 bg-white shadow-sm">
|
|
<label className="flex items-center gap-2 text-sm font-medium text-gray-800 cursor-pointer">
|
|
<input type="checkbox" checked={enabled} onChange={e => onEnabledChange(e.target.checked)} />
|
|
<Users className="w-4 h-4 text-brand-600" />
|
|
Registering for other people?
|
|
</label>
|
|
{enabled && (
|
|
<div className="mt-3 space-y-3">
|
|
{slots.length === 0 ? (
|
|
<p className="text-sm text-gray-500">Choose how many tickets first, then add a name for each person.</p>
|
|
) : (
|
|
<>
|
|
<p className="text-xs text-gray-500">Each named person gets their own ticket and their own attendee form. Leave a name blank to keep that ticket with the others.</p>
|
|
{slots.map(s => {
|
|
const e = entries[s.key] || { name: "", email: "", phone: "" };
|
|
return (
|
|
<div key={s.key} className="border rounded-lg p-3">
|
|
<div className="text-xs font-semibold text-gray-600 mb-2">{s.label}</div>
|
|
<div className="grid grid-cols-1 sm:grid-cols-3 gap-2">
|
|
<input className={inputCls} placeholder="Full name" value={e.name} onChange={ev => set(s.key, "name", ev.target.value)} aria-label={`${s.label} name`} />
|
|
<input type="email" className={inputCls} placeholder="Email (optional)" value={e.email} onChange={ev => set(s.key, "email", ev.target.value)} aria-label={`${s.label} email`} />
|
|
<input type="tel" className={inputCls} placeholder="Phone (optional)" value={e.phone} onChange={ev => set(s.key, "phone", ev.target.value)} aria-label={`${s.label} phone`} />
|
|
</div>
|
|
</div>
|
|
);
|
|
})}
|
|
<fieldset className="pt-1">
|
|
<legend className="text-sm font-medium text-gray-700 mb-1">Send the tickets</legend>
|
|
<label className="flex items-center gap-2 text-sm text-gray-700">
|
|
<input type="radio" name="ticket-delivery" checked={delivery === "payer"} onChange={() => onDeliveryChange("payer")} /> All to {payerLabel}
|
|
</label>
|
|
<label className="flex items-center gap-2 text-sm text-gray-700">
|
|
<input type="radio" name="ticket-delivery" checked={delivery === "each"} onChange={() => onDeliveryChange("each")} /> Each person their own (by their email or WhatsApp; anyone without contact details goes to {payerLabel})
|
|
</label>
|
|
</fieldset>
|
|
</>
|
|
)}
|
|
</div>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|