Archived
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Ls1vMdg2GYqbnKZmPaX1ot
131 lines
6.6 KiB
TypeScript
131 lines
6.6 KiB
TypeScript
"use client";
|
|
|
|
import React, { useState } from "react";
|
|
import { ListPlus, X } from "lucide-react";
|
|
import { ApiError } from "@/lib/api";
|
|
import { useAuth } from "@/hooks/useAuth";
|
|
import type { WaitlistLine } from "@/lib/waitlist";
|
|
import { ModalOverlay } from "@/components/shared/ModalOverlay";
|
|
import { apiV2Data } from "@/lib/apiV2";
|
|
|
|
export type { WaitlistLine };
|
|
|
|
const inputCls = "w-full border rounded-lg px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-brand-400";
|
|
|
|
/**
|
|
* "Join waitlist" for sold-out options. Opens a small form: which option, how many,
|
|
* and (for guests on events that allow it) name and contact details.
|
|
*/
|
|
export function JoinWaitlistButton({ eventId, lines, guestsAllowed, initialLine, compact = false }: {
|
|
eventId: string;
|
|
lines: WaitlistLine[];
|
|
guestsAllowed: boolean;
|
|
initialLine?: WaitlistLine;
|
|
compact?: boolean;
|
|
}) {
|
|
const { token, user } = useAuth();
|
|
const [open, setOpen] = useState(false);
|
|
const [lineKey, setLineKey] = useState(() => keyOf(initialLine || lines[0]));
|
|
const [qty, setQty] = useState(1);
|
|
const [guest, setGuest] = useState({ name: "", email: "", phone: "" });
|
|
const [busy, setBusy] = useState(false);
|
|
const [error, setError] = useState<string | null>(null);
|
|
const [done, setDone] = useState<string | null>(null);
|
|
|
|
if (lines.length === 0) return null;
|
|
const loggedIn = !!(token && user);
|
|
const line = lines.find(l => keyOf(l) === lineKey) || lines[0];
|
|
|
|
const submit = async (e: React.FormEvent) => {
|
|
e.preventDefault();
|
|
setBusy(true);
|
|
setError(null);
|
|
try {
|
|
const res = await (apiV2Data("post", "/waitlist", {
|
|
authToken: token,
|
|
body: {
|
|
eventId,
|
|
eventOptionId: line.eventOptionId,
|
|
...(line.variantId ? { variantId: line.variantId } : {}),
|
|
...(line.sessionId ? { sessionId: line.sessionId } : {}),
|
|
quantity: qty,
|
|
...(!loggedIn ? { guestName: guest.name.trim(), guestEmail: guest.email.trim(), guestPhone: guest.phone.trim() || undefined } : {}),
|
|
},
|
|
}) as Promise<{ message: string }>);
|
|
setDone(res.message);
|
|
} catch (err) {
|
|
setError(err instanceof ApiError ? err.message : "Couldn't join the waitlist. Please try again.");
|
|
} finally {
|
|
setBusy(false);
|
|
}
|
|
};
|
|
|
|
return (
|
|
<>
|
|
<button
|
|
type="button"
|
|
onClick={() => { setOpen(true); setDone(null); setError(null); }}
|
|
className={compact
|
|
? "inline-flex items-center gap-1 text-xs font-medium text-brand-700 border border-brand-200 bg-brand-50 rounded px-2 py-0.5 hover:bg-brand-100"
|
|
: "w-full inline-flex items-center justify-center gap-2 bg-white text-brand-700 border border-brand-300 px-4 py-2.5 rounded-lg hover:bg-brand-50 font-medium"}
|
|
>
|
|
<ListPlus className={compact ? "w-3.5 h-3.5" : "w-4 h-4"} /> Join waitlist
|
|
</button>
|
|
|
|
{open && (
|
|
<ModalOverlay labelledBy="waitlist-title" onClose={() => setOpen(false)} closeOnBackdrop className="fixed inset-0 z-50 bg-black/40 flex items-center justify-center p-4">
|
|
<form onSubmit={submit} className="bg-white rounded-xl shadow-xl w-full max-w-md p-5 space-y-4">
|
|
<div className="flex items-center justify-between">
|
|
<h2 id="waitlist-title" className="font-semibold">Join the waitlist</h2>
|
|
<button type="button" onClick={() => setOpen(false)} aria-label="Close" className="p-1 rounded hover:bg-gray-100"><X className="w-5 h-5 text-gray-500" /></button>
|
|
</div>
|
|
|
|
{done ? (
|
|
<>
|
|
<p className="text-sm text-green-800 bg-green-50 border border-green-200 rounded-lg p-3">{done}</p>
|
|
<button type="button" onClick={() => setOpen(false)} className="w-full px-4 py-2 rounded-lg bg-brand-600 text-white text-sm font-medium hover:bg-brand-700">Done</button>
|
|
</>
|
|
) : !loggedIn && !guestsAllowed ? (
|
|
<p className="text-sm text-gray-700">
|
|
<a href={`/login?redirect=${encodeURIComponent(typeof window !== "undefined" ? window.location.pathname : "/events")}`} className="text-brand-600 font-medium underline">Log in</a> to join the waitlist for this event.
|
|
</p>
|
|
) : (
|
|
<>
|
|
<p className="text-sm text-gray-600">If a place opens up, we'll email or WhatsApp you a link to claim it. We hold it for you for a limited time.</p>
|
|
{lines.length > 1 && (
|
|
<div>
|
|
<label htmlFor="waitlist-ticket" className="block text-sm font-medium text-gray-700 mb-1">Ticket</label>
|
|
<select id="waitlist-ticket" className={inputCls} value={lineKey} onChange={e => setLineKey(e.target.value)}>
|
|
{lines.map(l => <option key={keyOf(l)} value={keyOf(l)}>{l.label}</option>)}
|
|
</select>
|
|
</div>
|
|
)}
|
|
{lines.length === 1 && <p className="text-sm"><span className="text-gray-500">Ticket:</span> {line.label}</p>}
|
|
<div>
|
|
<label htmlFor="waitlist-qty" className="block text-sm font-medium text-gray-700 mb-1">How many?</label>
|
|
<input id="waitlist-qty" type="number" min={1} max={20} className={`${inputCls} w-24`} value={qty} onChange={e => setQty(Math.max(1, Math.min(20, parseInt(e.target.value, 10) || 1)))} />
|
|
</div>
|
|
{!loggedIn && (
|
|
<div className="space-y-2">
|
|
<input className={inputCls} placeholder="Full name *" value={guest.name} onChange={e => setGuest({ ...guest, name: e.target.value })} required aria-label="Full name" />
|
|
<input type="email" className={inputCls} placeholder="Email *" value={guest.email} onChange={e => setGuest({ ...guest, email: e.target.value })} required aria-label="Email" />
|
|
<input type="tel" className={inputCls} placeholder="Phone (optional)" value={guest.phone} onChange={e => setGuest({ ...guest, phone: e.target.value })} aria-label="Phone" />
|
|
</div>
|
|
)}
|
|
{error && <p role="alert" className="text-sm text-red-600">{error}</p>}
|
|
<button type="submit" disabled={busy} className="w-full px-4 py-2 rounded-lg bg-brand-600 text-white text-sm font-medium hover:bg-brand-700 disabled:opacity-50">
|
|
{busy ? "Joining…" : "Join waitlist"}
|
|
</button>
|
|
</>
|
|
)}
|
|
</form>
|
|
</ModalOverlay>
|
|
)}
|
|
</>
|
|
);
|
|
}
|
|
|
|
function keyOf(l?: WaitlistLine) {
|
|
return l ? `${l.eventOptionId}::${l.variantId || ""}::${l.sessionId || ""}` : "";
|
|
}
|