Let a content request carry the record's details, and open its editor
The Request form asks each type for its main details (a chapter's city, meeting time, website and email; an event's dates, venue and registration link...), stored as one JSON column (migration 049). The server only checks the shape; the fields are src/data/requestTypes.ts. Forms → Content requests shows them, and for a new record opens the New editor filled in (URL ID from the name, links and a description paragraph added); for an update, picks the record (closest name first) and opens it with each requested value beside the current one to use or skip. Nothing is saved until Save. Also centres the image and file pickers, which Tailwind's reset had pinned to the top left. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
7876e060e7
commit
4b249c9e52
10 changed files with 529 additions and 22 deletions
|
|
@ -24,7 +24,7 @@ import { del, get, patch, post, ApiError } from "../../lib/api.ts";
|
|||
import { isUnauthorized, useAuth } from "../../lib/auth.tsx";
|
||||
import { canWrite as roleCanWrite, canDelete } from "../../lib/roles.ts";
|
||||
import { feedbackTypeLabel } from "../../data/feedbackTypes.ts";
|
||||
import { requestTypeLabel } from "../../data/requestTypes.ts";
|
||||
import { fromRequest, requestType, requestTypeLabel } from "../../data/requestTypes.ts";
|
||||
|
||||
const STATUSES = ["new", "read", "actioned", "archived", "spam"];
|
||||
|
||||
|
|
@ -104,6 +104,111 @@ function RequestFiles({ row, canWrite }) {
|
|||
);
|
||||
}
|
||||
|
||||
/* A content request's details (049), labelled from requestTypes.ts;
|
||||
keys it no longer knows are shown as they are. */
|
||||
function RequestDetails({ row }) {
|
||||
const entries = Object.entries(row.details ?? {});
|
||||
if (entries.length === 0) return null;
|
||||
const labels = new Map((requestType(row.category)?.fields ?? []).map((f) => [f.key, f.label]));
|
||||
return (
|
||||
<dl className="mt-4 grid gap-x-6 gap-y-2 text-sm sm:grid-cols-[max-content_1fr]">
|
||||
{entries.map(([key, value]) => (
|
||||
<div key={key} className="contents">
|
||||
<dt className="font-medium text-muted">{labels.get(key) ?? key.replace(/_/g, " ")}</dt>
|
||||
<dd className="whitespace-pre-wrap text-body">{value as string}</dd>
|
||||
</div>
|
||||
))}
|
||||
</dl>
|
||||
);
|
||||
}
|
||||
|
||||
/* The record a request is about, in its editor: a new one filled in
|
||||
from the request, or, for an update, the existing one (picked here,
|
||||
the closest name first) with the requested values beside its own.
|
||||
Nothing is saved until Save in the editor. */
|
||||
function RequestRecord({ row }) {
|
||||
const navigate = useNavigate();
|
||||
const type = requestType(row.category);
|
||||
const [candidates, setCandidates] = useState<any[] | null>(null);
|
||||
const [picked, setPicked] = useState("");
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
if (!type) return null;
|
||||
|
||||
const state = { fromRequest: fromRequest(row) };
|
||||
const button =
|
||||
"rounded-full border border-link px-4 py-1.5 text-sm font-semibold text-link transition-colors hover:bg-tint disabled:opacity-60";
|
||||
|
||||
if (row.request_action === "new") {
|
||||
return (
|
||||
<div className="mt-4">
|
||||
<button type="button" className={button} onClick={() => navigate(`/admin/${type.entity}/new`, { state })}>
|
||||
Create {type.label.toLowerCase()} from this
|
||||
</button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
async function find() {
|
||||
if (!type) return;
|
||||
setError(null);
|
||||
try {
|
||||
const params = new URLSearchParams({ limit: "500", ...(type.preset ?? {}) });
|
||||
const { rows } = await get(`/admin/${type.entity}?${params}`, { ttl: 0 });
|
||||
const wanted = String(row.subject ?? "").toLowerCase().trim();
|
||||
const name = (r) => String(r[type.heading] ?? r.id);
|
||||
// The closest name first: exact, then containing, then the rest.
|
||||
const score = (r) => {
|
||||
const n = name(r).toLowerCase();
|
||||
return n === wanted ? 0 : wanted && (n.includes(wanted) || wanted.includes(n)) ? 1 : 2;
|
||||
};
|
||||
const sorted = [...rows].sort((a, b) => score(a) - score(b) || name(a).localeCompare(name(b)));
|
||||
setCandidates(sorted.map((r) => ({ id: r.id, name: name(r) })));
|
||||
setPicked(sorted[0]?.id ?? "");
|
||||
} catch (err) {
|
||||
setError(err instanceof ApiError ? err.message : "Couldn't load the list.");
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="mt-4 flex flex-wrap items-center gap-3">
|
||||
{candidates === null ? (
|
||||
<button type="button" className={button} onClick={find}>
|
||||
Find the {type.label.toLowerCase()} to update
|
||||
</button>
|
||||
) : candidates.length === 0 ? (
|
||||
<span className="text-sm text-muted">No {type.label.toLowerCase()} records yet.</span>
|
||||
) : (
|
||||
<>
|
||||
<label htmlFor={`record-${row.id}`} className="sr-only">
|
||||
Which {type.label.toLowerCase()}
|
||||
</label>
|
||||
<select
|
||||
id={`record-${row.id}`}
|
||||
value={picked}
|
||||
onChange={(e) => setPicked(e.target.value)}
|
||||
className="rounded-lg border border-muted/25 bg-surface px-3 py-1.5 text-sm text-body outline-none focus:border-brand focus:ring-2 focus:ring-brand/25"
|
||||
>
|
||||
{candidates.map((c) => (
|
||||
<option key={c.id} value={c.id}>
|
||||
{c.name}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
<button
|
||||
type="button"
|
||||
className={button}
|
||||
disabled={!picked}
|
||||
onClick={() => navigate(`/admin/${type.entity}/${encodeURIComponent(picked)}`, { state })}
|
||||
>
|
||||
Open it with the requested changes
|
||||
</button>
|
||||
</>
|
||||
)}
|
||||
{error && <span className="text-sm text-danger">{error}</span>}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function locationOf(row) {
|
||||
if (!row.page_path) return "Not page-specific";
|
||||
return row.section_id ? `${row.page_path} #${row.section_id}` : row.page_path;
|
||||
|
|
@ -170,7 +275,9 @@ function FeedbackCard({ row, onChange, onRemove, canWrite, canRemove }) {
|
|||
|
||||
{row.subject && <h2 className="mt-4 text-lg font-semibold text-body">{row.subject}</h2>}
|
||||
<p className={`${row.subject ? "mt-2" : "mt-4"} whitespace-pre-wrap text-body`}>{row.message}</p>
|
||||
{row.feedback_type === "content" && <RequestDetails row={row} />}
|
||||
{row.files?.length > 0 && <RequestFiles row={row} canWrite={canWrite} />}
|
||||
{row.feedback_type === "content" && canWrite && <RequestRecord row={row} />}
|
||||
|
||||
<p className="mt-4 text-sm text-muted">
|
||||
{row.name || row.email ? (
|
||||
|
|
|
|||
|
|
@ -88,6 +88,10 @@ export default function EntityEdit() {
|
|||
useWideAdminPage();
|
||||
|
||||
const isNew = id === "new";
|
||||
// From Forms → Content requests (requestTypes.ts, fromRequest): a
|
||||
// new record starts filled in with it; an existing one shows it in
|
||||
// RequestedChanges to use value by value.
|
||||
const fromRequest = (useLocation().state as any)?.fromRequest ?? null;
|
||||
// Editor, unless the entity asks for more (site settings:
|
||||
// superadmin), matching the server's writeRole.
|
||||
const canWrite = atLeast(user, manifest?.writeRole ?? "editor");
|
||||
|
|
@ -172,8 +176,18 @@ export default function EntityEdit() {
|
|||
// sending "" would be an explicit value rather than an absence.
|
||||
const blank = autoId ? {} : { id: "" };
|
||||
for (const child of manifest.children ?? []) blank[child.key] = [];
|
||||
setForm(blank);
|
||||
// The baseline stays the empty form, so a filled-in one counts
|
||||
// as unsaved until Create.
|
||||
baseline.current = JSON.stringify(blank);
|
||||
let start: any = blank;
|
||||
if (fromRequest) {
|
||||
for (const item of fromRequest.set) start = setPath(start, item.path, item.value);
|
||||
for (const item of fromRequest.add) start = { ...start, [item.child]: [...(start[item.child] ?? []), item.row] };
|
||||
if (!autoId && headingPath) {
|
||||
start = { ...start, id: `${prefixOf(start)}${slugify(getPath(start, headingPath) ?? "")}` };
|
||||
}
|
||||
}
|
||||
setForm(start);
|
||||
} else {
|
||||
const data = await get(`/admin/${manifest.key}/${id}`, { ttl: 0 });
|
||||
setForm(data.row);
|
||||
|
|
@ -189,7 +203,8 @@ export default function EntityEdit() {
|
|||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
}, [manifest, id, isNew, autoId, navigate]);
|
||||
// prefixOf and headingPath follow the manifest, already a dependency.
|
||||
}, [manifest, id, isNew, autoId, navigate, fromRequest]);
|
||||
|
||||
useEffect(() => {
|
||||
load();
|
||||
|
|
@ -368,6 +383,15 @@ export default function EntityEdit() {
|
|||
</h1>
|
||||
|
||||
<ImportedNote />
|
||||
{fromRequest && (
|
||||
<RequestedChanges
|
||||
request={fromRequest}
|
||||
isNew={isNew}
|
||||
form={form}
|
||||
onUse={change}
|
||||
onAdd={(child, row) => setForm((prev) => ({ ...prev, [child]: [...(prev[child] ?? []), row] }))}
|
||||
/>
|
||||
)}
|
||||
|
||||
{/* Straight in the page, not wrapped: a sticky element only
|
||||
travels within its parent, so its parent has to be tall. */}
|
||||
|
|
@ -551,3 +575,74 @@ function ImportedNote() {
|
|||
</p>
|
||||
);
|
||||
}
|
||||
|
||||
/* From a content request (Forms → Content requests). A new record
|
||||
is already filled in: this says so. An existing one lists each
|
||||
requested value beside the current one, to use or leave; nothing
|
||||
changes until it's used, and nothing is saved until Save. */
|
||||
function RequestedChanges({ request, isNew, form, onUse, onAdd }: any) {
|
||||
const [done, setDone] = useState<Set<string>>(() => new Set());
|
||||
const mark = (key: string) => setDone((prev) => new Set(prev).add(key));
|
||||
const button = "rounded-full border border-link px-3 py-0.5 text-xs font-semibold text-link transition-colors hover:bg-tint";
|
||||
|
||||
if (isNew) {
|
||||
return (
|
||||
<p role="status" className="mt-4 rounded-xl border border-brand/20 bg-tint px-4 py-3 text-sm text-body">
|
||||
Filled in from content request #{request.request}. Check it, fill in the rest, then Create. It isn't saved yet.
|
||||
</p>
|
||||
);
|
||||
}
|
||||
|
||||
const items = [
|
||||
...request.set.map((item) => ({ key: `set:${item.path}`, ...item, current: getPath(form, item.path) })),
|
||||
...request.add.map((item, i) => ({ key: `add:${i}`, ...item })),
|
||||
];
|
||||
if (items.length === 0) {
|
||||
return (
|
||||
<p className="mt-4 rounded-xl border border-brand/20 bg-tint px-4 py-3 text-sm text-body">
|
||||
Content request #{request.request} has no details to copy; its message is in Forms → Content requests.
|
||||
</p>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="mt-4 rounded-xl border border-brand/20 bg-tint px-4 py-3 text-sm text-body">
|
||||
<p className="font-semibold">Requested in content request #{request.request}</p>
|
||||
<p className="mt-0.5 text-xs text-muted">Use what should change, then Save changes.</p>
|
||||
<ul className="mt-3 space-y-2">
|
||||
{items.map((item) => (
|
||||
<li key={item.key} className="flex flex-wrap items-baseline gap-x-3 gap-y-1">
|
||||
<span className="font-medium">{item.label}</span>
|
||||
{"path" in item ? (
|
||||
<>
|
||||
<span className="text-muted">
|
||||
now {item.current ? <q>{String(item.current)}</q> : "blank"}, requested <q>{item.value}</q>
|
||||
</span>
|
||||
{done.has(item.key) || String(item.current ?? "") === item.value ? (
|
||||
<span className="text-xs text-muted">in the form</span>
|
||||
) : (
|
||||
<button type="button" className={button} onClick={() => {
|
||||
onUse(item.path, item.value);
|
||||
mark(item.key);
|
||||
}}>
|
||||
Use this
|
||||
</button>
|
||||
)}
|
||||
</>
|
||||
) : done.has(item.key) ? (
|
||||
<span className="text-xs text-muted">added below</span>
|
||||
) : (
|
||||
<button type="button" className={button} onClick={() => {
|
||||
onAdd(item.child, item.row);
|
||||
mark(item.key);
|
||||
}}>
|
||||
Add it
|
||||
</button>
|
||||
)}
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue