Found by the manual e2e of the production build: - paste guests preview counted an array, so it read "[object Object]" and never offered Save guests; - New event and Add user errors now clear when the field is fixed; - "Send to all unsent" says when nothing was unsent; - the check-in lookup shows an error on an empty submit and carries the typed email or mobile into the walk-in form; - invitation emails sent over Pub/Sub now save their recipient's link, so the Emails page shows the recipient. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
346 lines
14 KiB
TypeScript
346 lines
14 KiB
TypeScript
"use client";
|
|
|
|
import { useCallback, useEffect, useState } from "react";
|
|
import { Badge } from "@/app/components/ui/badge";
|
|
import { Button } from "@/app/components/ui/button";
|
|
import { Card, CardContent, CardHeader, CardTitle } from "@/app/components/ui/card";
|
|
import { Input } from "@/app/components/ui/input";
|
|
import { Label } from "@/app/components/ui/label";
|
|
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/app/components/ui/select";
|
|
import { ErrorState } from "@/app/components/ui/states";
|
|
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/app/components/ui/table";
|
|
import { useAdminAuth } from "@/app/lib/AdminAuthContext";
|
|
import { ApiError } from "@/app/lib/clientApi";
|
|
import {
|
|
addGuests,
|
|
getAttendance,
|
|
listGuests,
|
|
removeGuest,
|
|
sendInvites,
|
|
type AddGuestsResult,
|
|
type Attendance,
|
|
type EventGuest,
|
|
type GuestInput,
|
|
} from "@/app/lib/services/adminGuests";
|
|
import type { AdminEventMember } from "@/app/lib/types";
|
|
import { useT } from "@/app/lib/i18n/MessagesProvider";
|
|
|
|
type Field = "name" | "mobile" | "email" | "companyName";
|
|
const FIELDS: Field[] = ["name", "mobile", "email", "companyName"];
|
|
const EMPTY: GuestInput = { name: "", mobile: "", email: "", companyName: "" };
|
|
// Radix Select rejects "" as an item value, so "None" is this sentinel, mapped to no salesperson.
|
|
const NONE = "none";
|
|
|
|
// One guest per line: name, mobile, email, company. ponytail: plain comma split, no quoted commas.
|
|
const splitLines = (text: string) =>
|
|
text
|
|
.split("\n")
|
|
.map((l) => l.trim())
|
|
.filter(Boolean);
|
|
// ponytail: paste has no salesperson column; the backend accepts one per guest, add a column here if sales asks.
|
|
const parseLines = (text: string): GuestInput[] =>
|
|
splitLines(text).map((l) => {
|
|
const [name = "", mobile = "", email = "", companyName = ""] = l.split(",").map((c) => c.trim());
|
|
return { name, mobile, email, companyName };
|
|
});
|
|
|
|
export function GuestsCard({ eventUuid, team = [] }: { eventUuid: string; team?: AdminEventMember[] }) {
|
|
const t = useT();
|
|
const { hasModule } = useAdminAuth();
|
|
const canManage = hasModule("BO_EVENT_GUESTS");
|
|
const [guests, setGuests] = useState<EventGuest[] | null>(null);
|
|
const [att, setAtt] = useState<Attendance | null>(null);
|
|
const [loadError, setLoadError] = useState(false);
|
|
const [error, setError] = useState<string | null>(null);
|
|
const [notice, setNotice] = useState<string | null>(null);
|
|
const [form, setForm] = useState<GuestInput>(EMPTY);
|
|
const [fieldErrors, setFieldErrors] = useState<Partial<Record<Field, string>>>({});
|
|
const [paste, setPaste] = useState("");
|
|
const [salesperson, setSalesperson] = useState(NONE);
|
|
const [preview, setPreview] = useState<(AddGuestsResult & { saved: boolean }) | null>(null);
|
|
const [busy, setBusy] = useState(false);
|
|
|
|
const load = useCallback(async () => {
|
|
try {
|
|
setGuests(await listGuests(eventUuid));
|
|
setLoadError(false);
|
|
} catch {
|
|
setLoadError(true);
|
|
}
|
|
// ponytail: the numbers strip is secondary; a failed fetch leaves dashes.
|
|
getAttendance(eventUuid).then(setAtt).catch(() => {});
|
|
}, [eventUuid]);
|
|
|
|
useEffect(() => {
|
|
// eslint-disable-next-line react-hooks/set-state-in-effect -- load() fetches from the API; its setState runs after the await.
|
|
load();
|
|
}, [load]);
|
|
|
|
async function run(action: () => Promise<void>, failed: string) {
|
|
setError(null);
|
|
setNotice(null);
|
|
setBusy(true);
|
|
try {
|
|
await action();
|
|
} catch (err) {
|
|
const api = err instanceof ApiError ? err : null;
|
|
const code = (api?.data as { code?: string } | undefined)?.code;
|
|
// A 4xx is the server explaining what is wrong; anything else gets the generic text.
|
|
const known = api && api.status >= 400 && api.status < 500 && api.message ? api.message : failed;
|
|
setError(code === "GUEST_REGISTERED" ? t("GuestRegisteredRefused") : known);
|
|
} finally {
|
|
setBusy(false);
|
|
}
|
|
}
|
|
|
|
const addOne = (e: React.FormEvent) => {
|
|
e.preventDefault();
|
|
return run(async () => {
|
|
setFieldErrors({});
|
|
const res = await addGuests(eventUuid, [salesperson === NONE ? form : { ...form, salespersonUserId: Number(salesperson) }], false);
|
|
if (res.rejected.length) {
|
|
const next: Partial<Record<Field, string>> = {};
|
|
for (const r of res.rejected) {
|
|
if (r.field && (FIELDS as string[]).includes(r.field)) next[r.field as Field] = r.message;
|
|
else setError(r.message);
|
|
}
|
|
setFieldErrors(next);
|
|
return;
|
|
}
|
|
setForm(EMPTY);
|
|
setSalesperson(NONE);
|
|
await load();
|
|
}, t("GuestAddFailed"));
|
|
};
|
|
|
|
const doPaste = (dryRun: boolean) =>
|
|
run(async () => {
|
|
const res = await addGuests(eventUuid, parseLines(paste), dryRun);
|
|
if (dryRun) {
|
|
setPreview({ ...res, saved: false });
|
|
return;
|
|
}
|
|
// Keep only the rejected lines, so a second save cannot resend the saved ones.
|
|
const lines = splitLines(paste);
|
|
const rejected = res.rejected.map((r, i) => ({ ...r, index: i }));
|
|
setPaste(res.rejected.map((r) => lines[r.index]).join("\n"));
|
|
setPreview({ added: res.added, rejected, saved: true });
|
|
await load();
|
|
}, t("GuestAddFailed"));
|
|
|
|
const send = (target: { guestIds: number[] } | { allUnsent: true }) =>
|
|
run(async () => {
|
|
const res = await sendInvites(eventUuid, target);
|
|
setNotice(res.sent + res.skippedNoEmail === 0 ? t("GuestNoUnsent") : t("GuestSendResult", { sent: res.sent, skipped: res.skippedNoEmail }));
|
|
await load();
|
|
}, t("GuestSendFailed"));
|
|
|
|
const remove = (id: number) =>
|
|
run(async () => {
|
|
await removeGuest(eventUuid, id);
|
|
await load();
|
|
}, t("GuestRemoveFailed"));
|
|
|
|
async function copyLink(token: string) {
|
|
setError(null);
|
|
setNotice(null);
|
|
try {
|
|
await navigator.clipboard.writeText(`${window.location.origin}/i/${token}`);
|
|
setNotice(t("QrCopied"));
|
|
} catch {
|
|
setError(t("CopyFailed"));
|
|
}
|
|
}
|
|
|
|
const status = (g: EventGuest) =>
|
|
g.attended ? (
|
|
<Badge variant="positive">{t("GuestAttended")}</Badge>
|
|
) : g.registeredAt ? (
|
|
<Badge variant="teal">{t("GuestRegistered")}</Badge>
|
|
) : (
|
|
<Badge variant="neutral">{t("GuestInvited")}</Badge>
|
|
);
|
|
|
|
const numbers: [string, number | null | undefined][] = [
|
|
[t("GuestInvited"), att?.invited],
|
|
[t("EvtInvitationCap"), att?.cap],
|
|
[t("GuestRegistered"), att?.registered],
|
|
[t("GuestAttended"), att?.attended],
|
|
[t("GuestWalkIns"), att?.walkIns],
|
|
[t("GuestTotal"), att?.total],
|
|
[t("EvtCapacity"), att?.capacity],
|
|
];
|
|
|
|
return (
|
|
<Card>
|
|
<CardHeader>
|
|
<CardTitle>{t("GuestsTitle")}</CardTitle>
|
|
</CardHeader>
|
|
<CardContent className="space-y-4">
|
|
<ul aria-label={t("GuestNumbers")} className="grid grid-cols-2 gap-3 sm:grid-cols-4 lg:grid-cols-7">
|
|
{numbers.map(([label, value]) => (
|
|
<li key={label} className="rounded-md border p-2">
|
|
<span className="block text-xs text-muted-foreground">{label}</span>
|
|
<span className="text-lg font-semibold">{value ?? "-"}</span>
|
|
</li>
|
|
))}
|
|
</ul>
|
|
|
|
{error && <ErrorState message={error} />}
|
|
{loadError && <ErrorState message={t("GuestsLoadFailed")} onRetry={load} />}
|
|
{notice && (
|
|
<p role="status" className="text-sm">
|
|
{notice}
|
|
</p>
|
|
)}
|
|
|
|
{canManage && (
|
|
<>
|
|
<p className="text-sm text-muted-foreground">{t("GuestsNote")}</p>
|
|
<form onSubmit={addOne} className="grid gap-3 sm:grid-cols-2 lg:grid-cols-5 lg:items-start">
|
|
{FIELDS.map((f) => {
|
|
const label = t({ name: "GuestName", mobile: "GuestMobile", email: "GuestEmail", companyName: "GuestCompany" }[f]);
|
|
return (
|
|
<div key={f} className="space-y-1.5">
|
|
<Label htmlFor={`guest-${f}`}>{label}</Label>
|
|
<Input
|
|
id={`guest-${f}`}
|
|
type="text"
|
|
value={form[f]}
|
|
onChange={(e) => setForm({ ...form, [f]: e.target.value })}
|
|
aria-invalid={!!fieldErrors[f]}
|
|
/>
|
|
{fieldErrors[f] && (
|
|
<p role="alert" className="text-sm text-destructive">
|
|
{fieldErrors[f]}
|
|
</p>
|
|
)}
|
|
</div>
|
|
);
|
|
})}
|
|
{team.length > 0 && (
|
|
<div className="space-y-1.5">
|
|
<Label htmlFor="guest-salesperson">{t("LeadSalesperson")}</Label>
|
|
<Select value={salesperson} onValueChange={setSalesperson}>
|
|
<SelectTrigger id="guest-salesperson" aria-label={t("LeadSalesperson")}>
|
|
<SelectValue />
|
|
</SelectTrigger>
|
|
<SelectContent>
|
|
<SelectItem value={NONE}>{t("LeadNone")}</SelectItem>
|
|
{team.map((m) => (
|
|
<SelectItem key={m.id} value={String(m.id)}>
|
|
{m.name ?? m.username ?? m.id}
|
|
</SelectItem>
|
|
))}
|
|
</SelectContent>
|
|
</Select>
|
|
</div>
|
|
)}
|
|
<Button type="submit" disabled={busy} className="lg:mt-6">
|
|
{t("GuestAdd")}
|
|
</Button>
|
|
</form>
|
|
|
|
<div className="space-y-1.5">
|
|
<Label htmlFor="guest-paste">{t("GuestPaste")}</Label>
|
|
<textarea
|
|
id="guest-paste"
|
|
rows={4}
|
|
value={paste}
|
|
onChange={(e) => {
|
|
setPaste(e.target.value);
|
|
setPreview(null);
|
|
}}
|
|
className="w-full rounded-md border border-input bg-background px-3 py-2 text-sm"
|
|
/>
|
|
<p className="text-xs text-muted-foreground">{t("GuestPasteHelp")}</p>
|
|
{preview && (
|
|
<div className="space-y-1 text-sm">
|
|
<p>
|
|
{preview.saved ? t("GuestAddedCount", { count: preview.added }) : t(preview.added === 1 ? "GuestWillAddOne" : "GuestWillAddMany", { count: preview.added })}
|
|
</p>
|
|
{preview.rejected.map((r) => (
|
|
<p key={r.index} className="text-destructive">
|
|
{t("GuestLineError", { line: r.index + 1, reason: r.message })}
|
|
</p>
|
|
))}
|
|
</div>
|
|
)}
|
|
<div className="flex gap-2">
|
|
<Button type="button" variant="outline" size="sm" disabled={busy || !paste.trim()} onClick={() => doPaste(true)}>
|
|
{t("GuestPreview")}
|
|
</Button>
|
|
{preview && !preview.saved && preview.added > 0 && (
|
|
<Button type="button" size="sm" disabled={busy} onClick={() => doPaste(false)}>
|
|
{t("GuestSave")}
|
|
</Button>
|
|
)}
|
|
</div>
|
|
</div>
|
|
|
|
<Button type="button" variant="outline" size="sm" disabled={busy} onClick={() => send({ allUnsent: true })}>
|
|
{t("GuestSendAll")}
|
|
</Button>
|
|
</>
|
|
)}
|
|
|
|
{guests && (
|
|
<Table>
|
|
<TableHeader>
|
|
<TableRow>
|
|
<TableHead>{t("Name")}</TableHead>
|
|
<TableHead>{t("LeadMobile")}</TableHead>
|
|
<TableHead>{t("Email")}</TableHead>
|
|
<TableHead>{t("Company")}</TableHead>
|
|
<TableHead>{t("Status")}</TableHead>
|
|
<TableHead>{t("GuestInviteSent")}</TableHead>
|
|
<TableHead>{t("LeadSalesperson")}</TableHead>
|
|
{canManage && <TableHead />}
|
|
</TableRow>
|
|
</TableHeader>
|
|
<TableBody>
|
|
{guests.map((g) => (
|
|
<TableRow key={g.id}>
|
|
<TableCell>{g.name}</TableCell>
|
|
<TableCell>{g.mobileE164}</TableCell>
|
|
<TableCell>{g.email}</TableCell>
|
|
<TableCell>{g.companyName}</TableCell>
|
|
<TableCell>{status(g)}</TableCell>
|
|
<TableCell>{g.inviteSentCount}</TableCell>
|
|
<TableCell>{g.salespersonName}</TableCell>
|
|
{canManage && (
|
|
<TableCell>
|
|
<div className="flex flex-wrap gap-1">
|
|
{g.email && (
|
|
<Button variant="outline" size="sm" disabled={busy} aria-label={t("GuestSendTo", { name: g.name })} onClick={() => send({ guestIds: [g.id] })}>
|
|
{t("GuestSend")}
|
|
</Button>
|
|
)}
|
|
{g.token && (
|
|
<Button
|
|
variant="outline"
|
|
size="sm"
|
|
aria-label={t("GuestCopyLinkFor", { name: g.name })}
|
|
onClick={() => copyLink(g.token!)}
|
|
>
|
|
{t("QrCopy")}
|
|
</Button>
|
|
)}
|
|
{!g.registeredAt && !g.attended && (
|
|
<Button variant="outline" size="sm" disabled={busy} aria-label={t("GuestRemoveNamed", { name: g.name })} onClick={() => remove(g.id)}>
|
|
{t("GuestRemove")}
|
|
</Button>
|
|
)}
|
|
</div>
|
|
</TableCell>
|
|
)}
|
|
</TableRow>
|
|
))}
|
|
</TableBody>
|
|
</Table>
|
|
)}
|
|
</CardContent>
|
|
</Card>
|
|
);
|
|
}
|