/* ═══════════════════════════════════════════════════════════════ REQUEST FORM Posts to /api/requests (server/src/routes/requests.js), as a form so files can go with it. What can be asked for is REQUEST_TYPES; ?type=chapter in the address picks one to start with, which is how the site's "Request" buttons open it. Files go to Google Drive on the server; when Drive isn't set up (REQUEST_FILES) the file field isn't shown. The limits here match the server's, so nobody waits on an upload that will be refused. ═══════════════════════════════════════════════════════════════ */ import { useState } from "react"; import { useSearchParams } from "react-router-dom"; import { ApiError, upload } from "../../lib/api.ts"; import { accentText, BRAND } from "../../lib/color.ts"; import { REQUEST_FILES } from "../../lib/site.ts"; import { REQUEST_TYPES, type RequestAction } from "../../data/requestTypes.ts"; import { FieldError, OptionalTag, TilePicker, errorFieldClass, fieldClass } from "./FeedbackForm.tsx"; const MUTED = "var(--color-muted)"; const MAX_CHARS = 5000; const MIN_MESSAGE = 10; // matches the server const MAX_FILES = 3; const MAX_FILE_MB = 10; const ACCEPT = ".docx,.pdf,.jpg,.jpeg,.png"; const ACTION_LABEL: Record = { new: "Something new", update: "An update to what's there", }; const PLACEHOLDER: Record = { blog: "A few lines about the post: what it's about, and anything we should know. Attach the post as a Word document.", event: "When and where, who it's for, how to register, and who to contact.", }; export default function RequestForm() { const [params] = useSearchParams(); const start = REQUEST_TYPES.find((t) => t.id === params.get("type")) ?? null; const [category, setCategory] = useState(start?.id ?? null); const [action, setAction] = useState(start?.actions.length === 1 ? start.actions[0] : null); const [subject, setSubject] = useState(""); const [message, setMessage] = useState(""); const [files, setFiles] = useState([]); const [name, setName] = useState(""); const [email, setEmail] = useState(""); const [website, setWebsite] = useState(""); // honeypot const [status, setStatus] = useState<"idle" | "sending" | "sent">("idle"); const [formError, setFormError] = useState(null); const [fieldErrors, setFieldErrors] = useState>({}); const type = REQUEST_TYPES.find((t) => t.id === category); const sending = status === "sending"; const fileError = files.length > MAX_FILES ? `${MAX_FILES} files at most.` : files.find((f) => f.size > MAX_FILE_MB * 1024 * 1024) ? `Files can be ${MAX_FILE_MB} MB each at most.` : null; const ready = Boolean(type && action) && subject.trim() !== "" && message.trim().length >= MIN_MESSAGE && email.trim() !== "" && !fileError; function pick(id: string) { const next = REQUEST_TYPES.find((t) => t.id === id); setCategory(id); // One choice picks itself; otherwise keep what was chosen if it still applies. setAction((prev) => (next?.actions.length === 1 ? next.actions[0] : prev && next?.actions.includes(prev) ? prev : null)); } async function handleSubmit(event) { event.preventDefault(); if (!ready || sending || !category || !action) return; setStatus("sending"); setFormError(null); setFieldErrors({}); const form = new FormData(); form.set("category", category); form.set("action", action); form.set("subject", subject.trim()); form.set("message", message.trim()); form.set("name", name.trim()); form.set("email", email.trim()); form.set("website", website); for (const file of files) form.append("files", file); try { await upload("/requests", form); setStatus("sent"); } catch (error) { setStatus("idle"); if (error instanceof ApiError) { setFieldErrors(error.fields ?? {}); setFormError(error.fields ? "Have another look at the highlighted fields." : error.message); } else { setFormError("Couldn't reach the server. Try again in a moment."); } } } function reset() { setSubject(""); setMessage(""); setFiles([]); setStatus("idle"); setFormError(null); setFieldErrors({}); } if (status === "sent") { return (

Thanks, we've got it

We'll look it over and follow up at {email}, usually within a week.

); } return (
{fieldErrors.category} {type && type.actions.length > 1 && (
New, or an update?
{type.actions.map((a) => ( ))}
{fieldErrors.action}
)} {type?.id === "region" && (

Regions are set by Unity, so this is for updating one that's on the site.

)} {type && ( <>
setSubject(e.target.value)} aria-invalid={Boolean(fieldErrors.subject)} aria-describedby={fieldErrors.subject ? "request-subject-error" : undefined} className={`mt-3 ${fieldClass} ${fieldErrors.subject ? errorFieldClass : ""}`} /> {fieldErrors.subject}