Archived
Replace `catch (e: any)` with `catch (e)` and read the message through a new errorMessage() helper. ApiError.data gets a real type, and useDismissingState's setter keeps one identity across renders. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
65 lines
2.6 KiB
TypeScript
65 lines
2.6 KiB
TypeScript
"use client";
|
||
|
||
import React, { useCallback, useEffect, useState } from "react";
|
||
import { ListChecks } from "lucide-react";
|
||
import { apiFetch } from "@/lib/api";
|
||
import { errorMessage } from "@/lib/utils";
|
||
|
||
type Entry = {
|
||
id: string;
|
||
status: "waiting" | "offered";
|
||
quantity: number;
|
||
offeredQuantity: number | null;
|
||
offerExpiresAt: string | null;
|
||
event: { id: string; title: string; startDate: string };
|
||
eventOption: { name: string };
|
||
variant: { name: string } | null;
|
||
};
|
||
|
||
/** My Events: the waitlists I'm on, with Leave (and a note when a place is on offer). */
|
||
export function MyWaitlists({ token, onMessage }: { token: string; onMessage: (m: string) => void }) {
|
||
const [entries, setEntries] = useState<Entry[]>([]);
|
||
const load = useCallback(() => {
|
||
apiFetch<Entry[]>("/api/waitlist/mine", { authToken: token }).then(setEntries).catch(() => setEntries([]));
|
||
}, [token]);
|
||
useEffect(() => { load(); }, [load]);
|
||
|
||
if (entries.length === 0) return null;
|
||
|
||
const leave = async (id: string) => {
|
||
try {
|
||
const res = await apiFetch<{ message: string }>(`/api/waitlist/${encodeURIComponent(id)}`, { method: "DELETE", authToken: token });
|
||
onMessage(res.message);
|
||
load();
|
||
} catch (e) {
|
||
onMessage(errorMessage(e) || "Couldn't leave the waitlist");
|
||
}
|
||
};
|
||
|
||
return (
|
||
<section className="border rounded-xl p-4 bg-white shadow-sm">
|
||
<div className="flex items-center gap-2 font-semibold text-gray-900 mb-2">
|
||
<ListChecks className="w-4 h-4 text-brand-600" /> Waitlists
|
||
</div>
|
||
<ul className="divide-y">
|
||
{entries.map(e => (
|
||
<li key={e.id} className="py-2 flex items-center justify-between gap-3 text-sm">
|
||
<div>
|
||
<div className="font-medium">{e.event.title}</div>
|
||
<div className="text-xs text-gray-500">{e.quantity} × {e.eventOption.name}{e.variant ? ` (${e.variant.name})` : ""}</div>
|
||
{e.status === "offered" ? (
|
||
<div className="text-xs text-green-700 mt-0.5">
|
||
{e.offeredQuantity} place(s) held for you until {e.offerExpiresAt ? new Date(e.offerExpiresAt).toLocaleString() : ""} — use the link we sent you to claim.
|
||
</div>
|
||
) : (
|
||
<div className="text-xs text-gray-500 mt-0.5">Waiting — we'll email or WhatsApp you if a place opens up.</div>
|
||
)}
|
||
</div>
|
||
<button onClick={() => leave(e.id)} className="px-2.5 py-1 text-xs rounded border hover:bg-gray-50 shrink-0">Leave</button>
|
||
</li>
|
||
))}
|
||
</ul>
|
||
</section>
|
||
);
|
||
}
|