"use client"; import { useState, type FormEvent } from "react"; import { Button } from "@/app/components/ui/button"; import { Checkbox } from "@/app/components/ui/checkbox"; import { Input } from "@/app/components/ui/input"; import { Label } from "@/app/components/ui/label"; import { ApiError } from "@/app/lib/clientApi"; import type { RegistrationBody, RegistrationInfo } from "@/app/lib/services/publicRegistration"; import { useT } from "@/app/lib/i18n/MessagesProvider"; const EMPTY: RegistrationBody = { name: "", mobile: "", email: "", companyName: "", jobTitle: "", interestIds: [], consent: false, website: "" }; // Fields the form renders; a server error for any other field goes to the form-level alert. const FIELDS = ["name", "mobile", "email", "companyName", "jobTitle", "consent"]; const ROW = "flex min-h-11 w-full cursor-pointer items-center gap-3 rounded-md border p-3"; const CONSENT_ROW = "flex min-h-11 w-full cursor-pointer items-start gap-3 rounded-md border p-3 leading-normal"; const BOX = "size-6"; export function apiData(err: unknown): { status?: number; code?: string; error?: string; field?: string } { if (!(err instanceof ApiError)) return {}; const d = (err.data ?? {}) as { code?: string; error?: string; details?: { field?: string } }; return { status: err.status, code: d.code, error: d.error, field: d.details?.field }; } /** Message for a failed visitor call: too-many wording, the server's text for 400/409, else the generic failure. */ export function failMessage(t: (k: string) => string, err: unknown, tooMany = "RegTooMany"): string { const { status, code, error } = apiData(err); if (code === "TOO_MANY") return t(tooMany); return (status === 400 || status === 409) && error ? error : t("RegFailed"); } interface Props { /** i18n key for the too-many message (default RegTooMany, the /r wording). */ tooManyKey?: string; info: RegistrationInfo; /** Prefilled values (a personal invite); the form is blank without it. */ initial?: Partial; /** Sends the form; reject with the ApiError to have the form show it. */ submit: (body: RegistrationBody) => Promise; onDone: () => void; /** The code or invitation was switched off: the page shows its own message. */ onInactive: () => void; } // The visitor registration form shared by /r, /i and /c: fields, bilingual privacy notice and consent. // The event header comes from the VisitorCard around it. export function RegistrationForm({ tooManyKey, info, initial, submit, onDone, onInactive }: Props) { const t = useT(); const [form, setForm] = useState({ ...EMPTY, ...initial }); const [errors, setErrors] = useState>({}); const [formError, setFormError] = useState(null); const [saving, setSaving] = useState(false); const set = (patch: Partial) => setForm((f) => ({ ...f, ...patch })); async function onSubmit(e: FormEvent) { e.preventDefault(); const errs: Record = {}; if (!form.name.trim()) errs.name = t("RegNameRequired"); if (!form.mobile.trim() && !form.email.trim()) errs.mobile = t("RegContactRequired"); setErrors(errs); setFormError(null); if (Object.keys(errs).length) return; setSaving(true); try { await submit({ ...form, name: form.name.trim(), mobile: form.mobile.trim(), email: form.email.trim(), companyName: form.companyName.trim(), jobTitle: form.jobTitle.trim(), }); onDone(); } catch (err) { const { status, code, error, field } = apiData(err); if (code === "CODE_INACTIVE" || code === "INVITE_INACTIVE") onInactive(); else if (code === "EVENT_FULL") setFormError(t("RegFull")); else if (status === 400 && error && field && FIELDS.includes(field)) setErrors({ [field]: error }); else setFormError(failMessage(t, err, tooManyKey)); setSaving(false); } } const field = (id: "name" | "mobile" | "email" | "companyName" | "jobTitle", label: string, type = "text", hint?: string) => { const describedBy = [hint && `reg-${id}-hint`, errors[id] && `reg-${id}-error`].filter(Boolean).join(" ") || undefined; return (
set({ [id]: e.target.value })} disabled={saving} aria-invalid={!!errors[id]} aria-describedby={describedBy} /> {hint &&

{hint}

} {errors[id] &&

{errors[id]}

}
); }; return (
{formError &&

{formError}

} {field("name", t("RegName"))} {field("mobile", t("RegMobile"), "tel", t("RegContactHint"))} {field("email", t("RegEmail"), "email")} {field("companyName", t("RegCompany"))} {field("jobTitle", t("RegJobTitle"))} {info.interests.length > 0 && (
{t("RegInterests")} {info.interests.map((i) => ( ))}
)}

{t("RegNoticeEn")}

{info.notice.textEn}

{t("RegNoticeMs")}

{info.notice.textMs}

{errors.consent && } {/* Honeypot: people never see or reach it; bots fill it and the server drops the submission. */}
); }