// Types and pure helpers behind the form editor (add-cms-forms). export type FieldType = "TEXT" | "TEXTAREA" | "EMAIL" | "PHONE" | "SELECT" | "CHECKBOX"; export const FIELD_TYPES: FieldType[] = ["TEXT", "TEXTAREA", "EMAIL", "PHONE", "SELECT", "CHECKBOX"]; export const FIELD_LABELS: Record = { TEXT: "FrmFieldTypeText", TEXTAREA: "FrmFieldTypeTextarea", EMAIL: "Email", PHONE: "Phone", SELECT: "FrmFieldTypeSelect", CHECKBOX: "FrmFieldTypeCheckbox", }; export interface CmsFormField { key: string; label: string; type: FieldType; required: boolean; options?: string[]; } /** A field as the editor holds it: options as one-per-line text. */ export interface CmsFormFieldDraft { key: string; label: string; type: FieldType; required: boolean; optionsText: string; } export interface CmsFormDraft { id?: number; name: string; submitLabel: string; successMessage: string; notifyEmailsText: string; status: "Y" | "N"; fields: CmsFormFieldDraft[]; } export interface CmsFormView { id: number; name: string; submitLabel: string; successMessage: string; notifyEmails: string[]; status: "Y" | "N"; fields: CmsFormField[]; updatedAt?: string | null; } export interface CmsFormListItem { id: number; name: string; status: "Y" | "N"; fieldCount: number; submissionCount: number; updatedAt?: string | null; } export interface CmsFormAnswer { key: string; label: string; type: FieldType; value: string; } export interface CmsFormSubmission { id: number; formId: number; pageSlug: string | null; answers: CmsFormAnswer[]; createdAt: string; } export interface SubmissionsPage { content: CmsFormSubmission[]; totalElements: number; } export interface CmsFormBody { id?: number; name: string; submitLabel: string; successMessage: string; notifyEmails: string[]; status: "Y" | "N"; fields: CmsFormField[]; } /** A stable field key the server accepts (^[a-z0-9]{1,16}$); never changes once made. */ function newKey(): string { return crypto.randomUUID().replace(/-/g, "").slice(0, 8); } export function newField(type: FieldType = "TEXT", label = ""): CmsFormFieldDraft { return { key: newKey(), label, type, required: false, optionsText: "" }; } export function emptyForm(): CmsFormDraft { return { id: undefined, name: "", submitLabel: "Send", successMessage: "Thanks! We'll be in touch soon.", notifyEmailsText: "", status: "Y", fields: [{ ...newField("TEXT", "Name"), required: true }], }; } export function draftFromView(view: CmsFormView): CmsFormDraft { return { id: view.id, name: view.name, submitLabel: view.submitLabel, successMessage: view.successMessage, notifyEmailsText: (view.notifyEmails ?? []).join(", "), status: view.status, fields: view.fields.map((f) => ({ key: f.key, label: f.label, type: f.type, required: f.required, optionsText: (f.options ?? []).join("\n"), })), }; } export function moveField(fields: T[], index: number, delta: number): T[] { const target = index + delta; if (target < 0 || target >= fields.length) return fields; const next = [...fields]; [next[index], next[target]] = [next[target], next[index]]; return next; } function lines(text: string, separator: RegExp): string[] { return text.split(separator).map((s) => s.trim()).filter(Boolean); } export function formBody(draft: CmsFormDraft): CmsFormBody { return { id: draft.id, name: draft.name.trim(), submitLabel: draft.submitLabel.trim(), successMessage: draft.successMessage.trim(), notifyEmails: lines(draft.notifyEmailsText, /[,\n]/), status: draft.status, fields: draft.fields.map((f) => { const field: CmsFormField = { key: f.key, label: f.label.trim(), type: f.type, required: f.required }; if (f.type === "SELECT") field.options = lines(f.optionsText, /\n/); return field; }), }; }