Files
crosscode-platform/apps/website/frontend/src/admin/fields.tsx
T
joshuaandClaude Opus 5.5 84c5b0c08e feat(website): the admin, with drafts, preview, publish and restore
Admin API and screens: dashboard and setup checklist, template and
style, pages and blocks, ministries, leadership, media library with
the storage limit, publish and history. Pending changes with who and
when, discard one, one Publish, restore any version (each version
keeps the working copy's rows, never sent to visitors). Preview on
the site's own addresses with a bar (widths, back, publish); shared
preview links for 7 days or until revoked. Roles checked, same-origin
changes only. Publish refreshes the public pages at once
(WEBSITE_CONTENT_CACHE_SECONDS). E2E on a production build: the
admin's flows and axe on every screen.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 12:30:29 +02:00

115 lines
7.4 KiB
TypeScript

"use client";
/**
* The forms for each homepage section and block, from one description per type (the backend's
* zod schemas in src/site/sections.ts check what's saved; field errors come back by path).
*/
import { ImagePicker } from "./media";
import { Btn, Field, inputClass } from "./ui";
type Spec =
| { key: string; kind: "text" | "textarea"; label: string; hint?: string; max?: number }
| { key: string; kind: "number"; label: string; min: number; max: number }
| { key: string; kind: "image"; label: string }
| { key: string; kind: "images"; label: string; max: number }
| { key: string; kind: "button"; label: string }
| { key: string; kind: "group"; label: string; fields: Spec[] };
const heading: Spec = { key: "heading", kind: "text", label: "Heading", max: 120 };
const button: Spec = { key: "button", kind: "button", label: "Button" };
export const SECTION_FIELDS: Record<string, { name: string; fields: Spec[] }> = {
hero: { name: "Top of the page (hero)", fields: [
{ key: "headline", kind: "text", label: "Headline", max: 120 },
{ key: "subline", kind: "textarea", label: "Under the headline", max: 300 },
{ key: "images", kind: "images", label: "Photos (the first for Photo and Split; up to three for Collage)", max: 3 },
button,
] },
serviceTimes: { name: "Service times and location", fields: [heading, { key: "note", kind: "textarea", label: "A note for visitors", hint: "The times and address come from your church profile.", max: 300 }] },
welcome: { name: "Welcome", fields: [
heading,
{ key: "mission", kind: "text", label: "Mission (one line)", max: 200 },
{ key: "body", kind: "textarea", label: "Introduction", hint: "Leave an empty line between paragraphs.", max: 4000 },
{ key: "imageId", kind: "image", label: "Photo (Image and text layout)" },
{ key: "pastor", kind: "group", label: "Pastor's photo and quote (With pastor layout)", fields: [
{ key: "name", kind: "text", label: "Name", max: 120 }, { key: "role", kind: "text", label: "Role", max: 80 },
{ key: "quote", kind: "textarea", label: "Quote", max: 400 }, { key: "photoId", kind: "image", label: "Photo" },
] },
] },
ministries: { name: "Ministries", fields: [heading, { key: "intro", kind: "textarea", label: "Introduction", max: 400 }] },
team: { name: "Team", fields: [heading, { key: "intro", kind: "textarea", label: "Introduction", max: 400 }] },
events: { name: "Upcoming events", fields: [heading, { key: "count", kind: "number", label: "How many to show", min: 2, max: 12 }] },
latestMessage: { name: "Latest message", fields: [heading] },
invitation: { name: "Invitation", fields: [heading, { key: "text", kind: "textarea", label: "Text", max: 400 }, button, { key: "imageId", kind: "image", label: "Background photo (Image background layout)" }] },
};
export const BLOCK_FIELDS: Record<string, { name: string; fields: Spec[] }> = {
text: { name: "Text", fields: [heading, { key: "body", kind: "textarea", label: "Text", hint: "Leave an empty line between paragraphs.", max: 20000 }] },
"image-text": { name: "Image and text", fields: [heading, { key: "body", kind: "textarea", label: "Text", max: 4000 }, { key: "imageId", kind: "image", label: "Photo" }, button] },
quote: { name: "Quote", fields: [{ key: "quote", kind: "textarea", label: "Quote", max: 600 }, { key: "by", kind: "text", label: "Who said it", max: 120 }] },
cta: { name: "Call to action", fields: [heading, { key: "text", kind: "textarea", label: "Text", max: 400 }, button] },
};
type Data = Record<string, unknown>;
export function DataForm({ fields, value, onChange, errors, prefix = "" }: { fields: Spec[]; value: Data; onChange: (v: Data) => void; errors: Record<string, string>; prefix?: string }) {
const set = (k: string, v: unknown) => onChange({ ...value, [k]: v });
return (
<div className="space-y-4">
{fields.map((f) => {
const err = errors[`${prefix}${f.key}`];
const v = value[f.key];
switch (f.kind) {
case "text":
return <Field key={f.key} label={f.label} hint={f.hint} error={err}><input className={inputClass} value={(v as string) ?? ""} maxLength={f.max} onChange={(e) => set(f.key, e.target.value || null)} /></Field>;
case "textarea":
return <Field key={f.key} label={f.label} hint={f.hint} error={err}><textarea className={inputClass} rows={f.key === "body" ? 6 : 3} value={(v as string) ?? ""} maxLength={f.max} onChange={(e) => set(f.key, e.target.value || (f.key === "body" ? "" : null))} /></Field>;
case "number":
return <Field key={f.key} label={f.label} error={err}><input type="number" className={`${inputClass} w-24`} min={f.min} max={f.max} value={(v as number) ?? f.min} onChange={(e) => set(f.key, Number(e.target.value))} /></Field>;
case "image":
return <div key={f.key}><ImagePicker label={f.label} value={(v as string) ?? null} onChange={(id) => set(f.key, id)} />{err && <p className="text-xs text-red-700">{err}</p>}</div>;
case "images": {
const list = Array.isArray(v) ? (v as string[]) : [];
return (
<div key={f.key} className="space-y-2">
<p className="text-sm font-medium text-gray-800">{f.label}</p>
{list.map((id, i) => <ImagePicker key={i} label={`Photo ${i + 1}`} value={id} onChange={(n) => set(f.key, n ? list.map((x, j) => (j === i ? n : x)) : list.filter((_, j) => j !== i))} />)}
{list.length < f.max && <ImagePicker label={`Photo ${list.length + 1}`} value={null} onChange={(n) => n && set(f.key, [...list, n])} />}
</div>
);
}
case "button": {
const b = v as { label: string; href: string } | null;
return (
<fieldset key={f.key} className="rounded border border-gray-200 p-3">
<legend className="px-1 text-sm font-medium text-gray-800">{f.label}</legend>
{b ? (
<div className="grid gap-3 sm:grid-cols-2">
<Field label="Says" error={errors[`${prefix}${f.key}.label`]}><input className={inputClass} value={b.label} maxLength={60} onChange={(e) => set(f.key, { ...b, label: e.target.value })} /></Field>
<Field label="Goes to" hint="A page here (/visit) or https://…" error={errors[`${prefix}${f.key}.href`]}><input className={inputClass} value={b.href} onChange={(e) => set(f.key, { ...b, href: e.target.value })} /></Field>
<Btn kind="link" onClick={() => set(f.key, null)}>No button</Btn>
</div>
) : <Btn onClick={() => set(f.key, { label: "Plan your visit", href: "/visit" })}>Add a button</Btn>}
</fieldset>
);
}
case "group": {
const g = v as Data | null;
return (
<fieldset key={f.key} className="rounded border border-gray-200 p-3">
<legend className="px-1 text-sm font-medium text-gray-800">{f.label}</legend>
{g ? (
<>
<DataForm fields={f.fields} value={g} onChange={(n) => set(f.key, n)} errors={errors} prefix={`${prefix}${f.key}.`} />
<Btn kind="link" className="mt-3" onClick={() => set(f.key, null)}>Remove</Btn>
</>
) : <Btn onClick={() => set(f.key, { name: "", role: null, quote: null, photoId: null })}>Add</Btn>}
</fieldset>
);
}
}
})}
</div>
);
}