"use client"; import React, { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from "react"; import { createPortal } from "react-dom"; import { useAuth } from "@/hooks/useAuth"; import { useRouter } from "next/navigation"; import { apiFetch, API_BASE } from "@/lib/api"; import { ApiImage } from "@/components/shared/ApiImage"; import { useDismissingState } from "@/hooks/useDismissingState"; import { useSiteSettings } from "@/contexts/SiteSettingsContext"; import { useMyCampuses } from "@/hooks/useCampuses"; import { CampusBadge, CampusFilter, matchesCampus } from "@/components/shared/CampusFilter"; import { SavedFilters } from "@/components/shared/SavedFilters"; import { Calendar, Copy } from "lucide-react"; import { ChoicesEditor } from "@/components/shared/FormFieldExtras"; import { errorMessage } from "@/lib/utils"; import type { Event, EventAttachment, EventFormField, EventOption, EventSeries, Paginated, Section } from "@/types"; import { BackLink } from "@/components/shared/BackLink"; import { SessionsEditor, AttendanceSettings, keyed, sortSessions, sessionsProblem, defaultCertificate, type KeyedSession, type AttendanceDraft, } from "@/components/events/ScheduleEditor"; import { toSessionDraft, scheduleSummary } from "@/lib/sessions"; import { fetchApiFile, openBlob } from "@/lib/export"; // ─── helpers ──────────────────────────────────────────────────────────────── const cleanChoices = (opts: unknown): string[] => (Array.isArray(opts) ? opts.map(o => String(o).trim()).filter(Boolean) : []); function toLocalDT(input: string | Date | null | undefined): string { if (!input) return ""; const d = new Date(input); if (isNaN(d.getTime())) return ""; const y = d.getFullYear(); const m = String(d.getMonth() + 1).padStart(2, "0"); const day = String(d.getDate()).padStart(2, "0"); const hh = String(d.getHours()).padStart(2, "0"); const mm = String(d.getMinutes()).padStart(2, "0"); return `${y}-${m}-${day}T${hh}:${mm}`; } const DT_MIN = "2000-01-01T00:00"; const DT_MAX = "2099-12-31T23:59"; function DTInput({ label, value, onChange, hint, required }: { label: string; value: string; onChange: (v: string) => void; hint?: string; required?: boolean }) { return (
onChange(e.target.value)} /> {hint &&

{hint}

}
); } // ─── types ─────────────────────────────────────────────────────────────────── type FormFieldType = "yes_no" | "text" | "date" | "numeric" | "statement" | "paragraph" | "rating" | "multiple_choice"; type EventFormDef = { isRequired: boolean; fields: { id?: string; type: FormFieldType; label: string; isRequired?: boolean; order?: number; helpText?: string | null; options?: unknown }[] }; type FieldDef = EventFormDef["fields"][number]; // What PUT /api/events/options/:id takes as earlyBirdTiers (variantId null = option-level). type TierPayload = { id?: string; deadline: string; price: number; stockLimit: number; order: number; variantId: string | null }; // GET /api/feedback/event/:id — the event's post-event feedback form, if it has one. type FeedbackFormResponse = { fields?: EventFormField[]; sendAt?: string | null; audience?: string; anonymous?: boolean; intro?: string | null; sentAt: string | null; invited: number; responses: number; }; const toFieldDefs = (fields: EventFormField[] | undefined): FieldDef[] => (fields || []).map((x) => ({ id: x.id, type: x.type, label: x.label, isRequired: !!x.isRequired, helpText: x.helpText || "", options: x.options || undefined })); type Tier = { id?: string; deadline: string; price: string; stockLimit: string; order?: number }; type Variant = { id?: string; name: string; price: string; // empty → use option base price stockLimit: string; staffPrice?: string; // empty → use the option's staff price earlyBirdTiers: Tier[]; }; type OptionDraft = { id?: string; name: string; price: string; isMainTicket: boolean; stockLimit: string; staffPrice?: string; // empty → no staff price earlyBirdTiers: Tier[]; // option-level (used only when no variants) variants: Variant[]; sessionMode?: "all" | "pick"; // "pick sessions" events only }; type SectionDraft = { name: string; optionIndices: number[]; }; // ─── sub-components ───────────────────────────────────────────────────────── function UploadImageButton({ onUploaded, label = "Upload image" }: { onUploaded: (url: string) => void; label?: string }) { const { token } = useAuth(); const [uploading, setUploading] = useState(false); const ref = useRef(null); return (
{ const file = e.target.files?.[0]; if (!file || !token) return; setUploading(true); try { const fd = new FormData(); fd.append("image", file); const res = await apiFetch<{ url?: string; path?: string }>("/api/uploads/event-image", { method: "POST", body: fd, authToken: token }); const uploaded = res?.url || res?.path; if (uploaded) onUploaded(uploaded); } finally { setUploading(false); if (ref.current) ref.current.value = ""; } }} />
); } function FormBuilder({ value, onChange, title = "Registration Form Fields", gateLabel = "Required before tickets generate" }: { value: EventFormDef; onChange: (v: EventFormDef) => void; title?: string; /** Label of the "form must be filled in" switch; null hides it (feedback forms). */ gateLabel?: string | null; }) { const fields = value.fields || []; const add = (type: FormFieldType) => onChange({ ...value, fields: [...fields, { type, label: "", isRequired: false, helpText: "" }] }); const upd = (idx: number, patch: Partial) => { const c = fields.slice(); c[idx] = { ...c[idx], ...patch }; onChange({ ...value, fields: c }); }; const rem = (idx: number) => { const c = fields.slice(); c.splice(idx, 1); onChange({ ...value, fields: c }); }; const mov = (idx: number, dir: -1 | 1) => { const c = fields.slice(); const t = idx + dir; if (t < 0 || t >= c.length) return; [c[idx], c[t]] = [c[t], c[idx]]; onChange({ ...value, fields: c }); }; const typeLabel = (t: FormFieldType) => ({ text: "Text", numeric: "Number", date: "Date", yes_no: "Yes/No", statement: "Statement", paragraph: "Heading", rating: "Rating", multiple_choice: "Multiple choice" }[t] || t); return (
{title}
{gateLabel && ( )}
{fields.length === 0 ?

No fields yet.

: (
    {fields.map((f, idx) => (
  • {typeLabel(f.type)} #{idx + 1}
    {f.type !== "statement" && f.type !== "paragraph" && ( )}
    upd(idx, { label: e.target.value })} /> {f.type === "multiple_choice" && upd(idx, { options: opts })} />} {f.type === "paragraph" ?