NGU-Web/src/data/requestTypes.ts
Zaldimmar af7e99895a Request form: short field titles with a help line, and why to send a file
Detail fields can carry a help line under the title (RequestField
help). The blog post's card field is now Short description, saying
where it shows; other long titles became short ones with the detail
in the help. The Files note explains that a blog post sent as a
.docx can be imported straight onto the site.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-30 20:33:20 -05:00

241 lines
9.4 KiB
TypeScript

/* ═══════════════════════════════════════════════════════════════
REQUEST TYPES
What the Request form (/request) can ask for, and whether each can
be new, an update, or both. The ids and actions must match
CATEGORIES in server/src/routes/requests.js (and the CHECK in
046_content_requests.sql); keep them in step.
A link can open the form on one: requestLink("group") is
/request?type=group. People, teams and awards have no buttons on
the site, but are here to pick.
`fields` are the details the form asks for on top of the name and
message, stored together as JSON on the request (feedback.details,
049). Only here: the server keeps whatever keys come, so adding a
field is a change to this file alone. Each field's `to` says where
it goes when Forms → Content requests opens the record's editor
from the request (EntityEdit's fromRequest):
{ path } a field in the editor, eg. "locality"
{ link, label } a row in the record's links
{ block: "card" } a paragraph of the record's description
(none) shown in the admin only, eg. a region's
name, which the admin picks by hand
`entity` is the admin record it becomes, `preset` the values a new
one starts with (a group is an organization of kind group), and
`heading` the editor field the request's name fills.
═══════════════════════════════════════════════════════════════ */
export type RequestAction = "new" | "update";
export type RequestField = {
key: string;
/** A short title for the field. */
label: string;
/** A line under the title: what it's for, or where it shows. */
help?: string;
input?: "text" | "date" | "url" | "email" | "textarea";
placeholder?: string;
to?: { path: string } | { link: "website" | "email" | "action"; label: string } | { block: "card" };
};
export type RequestType = {
id: string;
label: string;
hint: string;
actions: RequestAction[];
// What the name field asks, for a new one and for an update.
subject: string;
entity: string;
heading: string;
preset?: Record<string, string>;
fields: RequestField[];
};
const CITY: RequestField = { key: "city", label: "City", to: { path: "locality" } };
const STATE: RequestField = { key: "state", label: "State or province", placeholder: "eg. MO", to: { path: "state_code" } };
const COUNTRY: RequestField = { key: "country", label: "Country", placeholder: "eg. US", to: { path: "country" } };
const WEBSITE: RequestField = { key: "website", label: "Website", input: "url", placeholder: "https://", to: { link: "website", label: "Website" } };
const EMAIL: RequestField = { key: "email", label: "Contact email", input: "email", to: { link: "email", label: "Email" } };
const DESCRIPTION: RequestField = { key: "description", label: "Short description", help: "A line or two shown on its card in lists and at the top of its page.", input: "textarea", to: { block: "card" } };
export const REQUEST_TYPES: RequestType[] = [
{
id: "blog",
label: "Blog post",
hint: "A story or article for the community blog",
actions: ["new"],
subject: "Its title",
entity: "blog_posts",
heading: "title",
fields: [
{ key: "author", label: "Author", help: "Their name as it should appear on the post.", to: { path: "guest_author" } },
{ key: "excerpt", label: "Short description", help: "A line or two shown on the post's preview card, eg. in the Community blog.", input: "textarea", to: { path: "excerpt" } },
],
},
{
id: "event",
label: "Event",
hint: "A retreat, gathering or calendar event",
actions: ["new", "update"],
subject: "The event's name",
entity: "events",
heading: "title",
fields: [
{ key: "starts_on", label: "Starts", input: "date", to: { path: "starts_on" } },
{ key: "ends_on", label: "Ends", input: "date", to: { path: "ends_on" } },
{ key: "venue", label: "Venue", placeholder: "eg. Unity Village", to: { path: "venue" } },
CITY,
STATE,
{ key: "register", label: "Registration link", input: "url", placeholder: "https://", to: { link: "action", label: "Register" } },
DESCRIPTION,
],
},
{
id: "group",
label: "Group",
hint: "A local NGU group",
actions: ["new", "update"],
subject: "The group's name",
entity: "organizations",
heading: "name",
preset: { kind: "group" },
fields: [
{ key: "region", label: "Region", help: "The Unity region it's part of, if you know it.", placeholder: "eg. Great Lakes" },
CITY,
STATE,
COUNTRY,
{ key: "meets", label: "Meeting time", placeholder: "eg. 2nd Sundays, 6:00pm", to: { path: "group.meets" } },
WEBSITE,
EMAIL,
DESCRIPTION,
],
},
{
id: "region",
label: "Region",
hint: "Details for a Unity region",
actions: ["update"],
subject: "Which region",
entity: "organizations",
heading: "name",
preset: { kind: "region" },
fields: [WEBSITE, EMAIL, DESCRIPTION],
},
{
id: "partner",
label: "Partner",
hint: "A partner organization",
actions: ["new", "update"],
subject: "The organization's name",
entity: "organizations",
heading: "name",
preset: { kind: "partner" },
fields: [CITY, STATE, COUNTRY, WEBSITE, EMAIL, DESCRIPTION],
},
{
id: "team",
label: "Team",
hint: "A team or committee",
actions: ["new", "update"],
subject: "The team's name",
entity: "teams",
heading: "name",
fields: [
{ key: "organization", label: "Organization", help: "The organization the team belongs to.", placeholder: "eg. NGU national" },
{ key: "tagline", label: "Tagline", help: "What the team does, in a line.", to: { path: "tagline" } },
EMAIL,
DESCRIPTION,
],
},
{
id: "award",
label: "Award",
hint: "An award and who received it",
actions: ["new", "update"],
subject: "The award's name",
entity: "awards",
heading: "name",
fields: [
{ key: "organization", label: "Given by", help: "The organization that gives the award.", placeholder: "eg. NGU national" },
{ key: "description", label: "What it's for", input: "textarea", to: { path: "description" } },
{ key: "recipients", label: "Recipients", help: "Who received it, and in which year.", input: "textarea" },
],
},
{
id: "person",
label: "Person",
hint: "Someone's profile",
actions: ["new", "update"],
subject: "Their name",
entity: "people",
heading: "display_name",
fields: [
{ key: "pronouns", label: "Pronouns", placeholder: "eg. she/her", to: { path: "pronouns" } },
{ key: "tagline", label: "Role or title", help: "Shown under their name, eg. Retreat Team Lead.", to: { path: "tagline" } },
CITY,
STATE,
{ key: "email", label: "Public email", input: "email", to: { path: "public_email" } },
WEBSITE,
{ key: "bio", label: "Short bio", input: "textarea", to: { path: "bio" } },
],
},
];
export const requestLink = (type: string) => `/request?type=${type}`;
const BY_ID = new Map(REQUEST_TYPES.map((t) => [t.id, t]));
export const requestType = (id: string) => BY_ID.get(id);
export const requestTypeLabel = (id: string) => BY_ID.get(id)?.label ?? id;
/* ── Into the admin editor ─────────────────────────────────────── */
/* What EntityEdit applies from a request (location.state.fromRequest):
`set` fields by path, `add` child rows (links, a description
paragraph). A new record gets all of it; an existing one lists each
beside its current value to use or skip. */
export type FromRequest = {
request: number;
set: { path: string; label: string; value: string }[];
add: { child: string; label: string; row: Record<string, unknown> }[];
};
export function fromRequest(
row: { id: number; category: string; request_action: string; subject?: string | null; details?: Record<string, string> | null },
): FromRequest | null {
const type = requestType(row.category);
if (!type) return null;
const details = row.details ?? {};
const set: FromRequest["set"] = [];
const add: FromRequest["add"] = [];
if (row.request_action === "new") {
for (const [path, value] of Object.entries(type.preset ?? {})) set.push({ path, label: path, value });
if (row.subject) set.push({ path: type.heading, label: "Name", value: row.subject });
}
for (const field of type.fields) {
const value = details[field.key]?.trim();
if (!value || !field.to) continue;
if ("path" in field.to) set.push({ path: field.to.path, label: field.label, value });
else if ("link" in field.to) {
add.push({
child: "links",
label: `${field.label}: ${value}`,
row: {
kind: field.to.link,
label: field.to.label,
url: field.to.link === "email" && !value.startsWith("mailto:") ? `mailto:${value}` : value,
is_primary: 0,
},
});
} else {
add.push({
child: "content_blocks",
label: `${field.label} (a paragraph)`,
row: { slot: field.to.block, type: "paragraph", text: value, items: [] },
});
}
}
return { request: row.id, set, add };
}