"use client"; import { ExtraFieldInput, isExtraFieldType, ChoicesEditor } from "@/components/shared/FormFieldExtras"; import React, { useCallback, useEffect, useMemo, useRef, useState } from "react"; import { useAuth } from "@/hooks/useAuth"; import { useRouter } from "next/navigation"; import { fetchAllUsers, fetchManagedEvents, fetchEvent } from "@/lib/api"; import { useDismissingState } from "@/hooks/useDismissingState"; import { FileText } from "lucide-react"; import { errorMessage } from "@/lib/utils"; import type { Event, EventForm, FormResponse, Registration, User } from "@/types"; import { BackLink } from "@/components/shared/BackLink"; import { EventPicker } from "@/components/shared/EventPicker"; import { apiV2, apiV2Data } from "@/lib/apiV2"; 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 }[] }; // The form as GET /api/events/:id returns it, mapped into the editor's shape. const toFormDef = (f: EventForm | null | undefined): EventFormDef => f && Array.isArray(f.fields) ? { isRequired: !!f.isRequired, fields: f.fields.map((x) => ({ id: x.id, type: x.type, label: x.label, isRequired: !!x.isRequired, order: x.order, helpText: x.helpText || '', options: x.options || undefined })) } : { isRequired: false, fields: [] }; // /api/forms/responses returns { items } (older builds returned a bare array). const responseItems = (res: { items?: FormResponse[] } | FormResponse[] | null | undefined): FormResponse[] => Array.isArray(res) ? res : (Array.isArray(res?.items) ? res.items : []); function FormBuilder({ value, onChange }: { value: EventFormDef; onChange: (v: EventFormDef) => void }) { const fields = value.fields || []; const addField = (type: FormFieldType = 'text') => { onChange({ ...value, fields: [...fields, { type, label: '', isRequired: false, helpText: '' }] }); }; const updateField = (idx: number, patch: Partial) => { const copy = fields.slice(); copy[idx] = { ...copy[idx], ...patch }; onChange({ ...value, fields: copy }); }; const removeField = (idx: number) => { const copy = fields.slice(); copy.splice(idx, 1); onChange({ ...value, fields: copy }); }; const moveField = (idx: number, dir: -1 | 1) => { const copy = fields.slice(); const target = idx + dir; if (target < 0 || target >= copy.length) return; [copy[idx], copy[target]] = [copy[target], copy[idx]]; onChange({ ...value, fields: copy }); }; const typeLabel = (type: FormFieldType) => { const labels: Record = { text: 'Text', numeric: 'Number', date: 'Date', yes_no: 'Yes/No', statement: 'Statement', paragraph: 'Heading', rating: 'Rating', multiple_choice: 'Multiple choice' }; return labels[type] || type; }; return (
Registration Form Fields
{fields.length === 0 ? (
No fields yet — add questions below.
) : (
    {fields.map((f, idx) => (
  • {typeLabel(f.type)} #{idx + 1}
    {f.type !== 'statement' && f.type !== 'paragraph' && ( )}
    updateField(idx, { label: e.target.value })} /> {f.type === 'multiple_choice' && updateField(idx, { options: opts })} />} {f.type === 'paragraph' ? (