Page editors: Retreats, Community, Resources, Organization and History built from sections
Each page is now a list of sections edited in Admin → Pages: a title (30 characters at most, for the side nav), URL ID, blurb and accent, its own content blocks, and an optional preset (Community blog, groups map, region list, partner cards, events, timeline, NGU's details). Migration 056 seeds every page as the code drew it and moves the Organization editor's blocks and volunteering into its sections. The Community blog's settings move into the Community editor. The header, footer and side nav read each page's sections from the API. The admin engine reads and writes nested collections to any depth. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
4a1d6fd6e9
commit
9daf7b1a4f
29 changed files with 972 additions and 938 deletions
|
|
@ -236,15 +236,13 @@ function readChildren(db, child, ownerId) {
|
|||
)
|
||||
.all(...params);
|
||||
|
||||
// Nested collections, to any depth: a page's sections, each
|
||||
// section's blocks, each block's items. The id goes as text: a JS
|
||||
// number binds as REAL, which a TEXT owner column (a section's
|
||||
// blocks) holds as '13.0'; text suits an INTEGER owner just as well.
|
||||
for (const child2 of child.children ?? []) {
|
||||
for (const row of rows) {
|
||||
row[child2.key] = db
|
||||
.prepare(
|
||||
`SELECT * FROM ${child2.table}
|
||||
WHERE ${child2.owner.column} = ?
|
||||
ORDER BY ${child2.order}`,
|
||||
)
|
||||
.all(row.id);
|
||||
row[child2.key] = readChildren(db, child2, String(row.id));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -378,6 +376,14 @@ export function updateRow(db, entity, rawId, payload) {
|
|||
`UPDATE ${entity.table} SET ${sets.join(", ")}
|
||||
WHERE ${entity.idColumn} = ?`,
|
||||
).run(...Object.values(values), id);
|
||||
} else if (entity.concurrency) {
|
||||
// A row that is only its collections (a site page and its
|
||||
// sections) still marks the save, or the next stale save
|
||||
// would pass the check above.
|
||||
db.prepare(
|
||||
`UPDATE ${entity.table} SET ${entity.concurrency} = datetime('now')
|
||||
WHERE ${entity.idColumn} = ?`,
|
||||
).run(id);
|
||||
}
|
||||
|
||||
writeExtensions(db, entity, id, payload, merged);
|
||||
|
|
@ -516,80 +522,63 @@ function writeChildren(db, entity, id, payload, parentValues) {
|
|||
if (payload[child.key] === undefined) continue; // not sent, not touched
|
||||
|
||||
deleteChildren(db, child, id);
|
||||
writeRows(db, child, id, payload[child.key], `${child.key}.`, parentValues);
|
||||
}
|
||||
}
|
||||
|
||||
const incoming = Array.isArray(payload[child.key]) ? payload[child.key] : [];
|
||||
/* One collection's rows under one owner, then each row's own nested
|
||||
collections under it, to any depth. Only the top level is deleted
|
||||
first (writeChildren): a nested row goes with its owner, by a
|
||||
foreign key's cascade or, for a polymorphic owner like a section's
|
||||
blocks, by the owner table's cleanup trigger. */
|
||||
function writeRows(db, child, ownerId, incomingRaw, prefix, parentValues) {
|
||||
const incoming = Array.isArray(incomingRaw) ? incomingRaw : [];
|
||||
|
||||
// index is the row's place in what the client sent, so a
|
||||
// validation error still points at the row the user is looking
|
||||
// at. position counts only the rows that survive, so dropping a
|
||||
// blank in the middle doesn't leave a gap in sort_order.
|
||||
const rows = incoming
|
||||
.map((raw, index) => ({ raw, index }))
|
||||
.filter(({ raw }) => child.allowBlank || !isBlankChildRow(child, raw));
|
||||
// index is the row's place in what the client sent, so a
|
||||
// validation error still points at the row the user is looking
|
||||
// at. position counts only the rows that survive, so dropping a
|
||||
// blank in the middle doesn't leave a gap in sort_order.
|
||||
const rows = incoming
|
||||
.map((raw, index) => ({ raw, index }))
|
||||
.filter(({ raw }) => child.allowBlank || !isBlankChildRow(child, raw));
|
||||
|
||||
rows.forEach(({ raw, index }, position) => {
|
||||
const { values, errors } = coerceRow(child.columns, raw, {
|
||||
prefix: `${child.key}.${index}.`,
|
||||
});
|
||||
if (Object.keys(errors).length) {
|
||||
throw new HttpError(422, "Validation failed", errors);
|
||||
}
|
||||
rows.forEach(({ raw, index }, position) => {
|
||||
const rowPrefix = `${prefix}${index}.`;
|
||||
const { values, errors } = coerceRow(child.columns, raw, { prefix: rowPrefix });
|
||||
if (Object.keys(errors).length) {
|
||||
throw new HttpError(422, "Validation failed", errors);
|
||||
}
|
||||
|
||||
const names = [child.owner.column, ...Object.keys(values)];
|
||||
const params = [id, ...Object.values(values)];
|
||||
const names = [child.owner.column, ...Object.keys(values)];
|
||||
const params = [ownerId, ...Object.values(values)];
|
||||
|
||||
if (child.owner.kindColumn) {
|
||||
names.push(child.owner.kindColumn);
|
||||
params.push(child.owner.kindValue);
|
||||
}
|
||||
for (const [column, from] of Object.entries(child.owner.inherit ?? {})) {
|
||||
if (names.includes(column)) continue;
|
||||
names.push(column);
|
||||
params.push(parentValues[from] ?? id);
|
||||
}
|
||||
if (child.order === "sort_order" && !names.includes("sort_order")) {
|
||||
names.push("sort_order");
|
||||
params.push(position);
|
||||
}
|
||||
if (child.owner.kindColumn) {
|
||||
names.push(child.owner.kindColumn);
|
||||
params.push(child.owner.kindValue);
|
||||
}
|
||||
for (const [column, from] of Object.entries(child.owner.inherit ?? {})) {
|
||||
if (names.includes(column)) continue;
|
||||
names.push(column);
|
||||
params.push(parentValues[from] ?? ownerId);
|
||||
}
|
||||
if (child.order === "sort_order" && !names.includes("sort_order")) {
|
||||
names.push("sort_order");
|
||||
params.push(position);
|
||||
}
|
||||
|
||||
const result = keyedDbErrors(`${child.key}.${index}.`, () =>
|
||||
db
|
||||
const result = keyedDbErrors(rowPrefix, () =>
|
||||
db
|
||||
.prepare(
|
||||
`INSERT INTO ${child.table} (${names.join(", ")})
|
||||
VALUES (${names.map(() => "?").join(", ")})`,
|
||||
)
|
||||
.run(...params),
|
||||
);
|
||||
);
|
||||
|
||||
for (const child2 of child.children ?? []) {
|
||||
const incomingNested = Array.isArray(raw[child2.key]) ? raw[child2.key] : [];
|
||||
const nested = incomingNested
|
||||
.map((rawItem, i) => ({ rawItem, i }))
|
||||
.filter(({ rawItem }) => child2.allowBlank || !isBlankChildRow(child2, rawItem));
|
||||
|
||||
nested.forEach(({ rawItem, i }, nestedPosition) => {
|
||||
const item = coerceRow(child2.columns, rawItem, {
|
||||
prefix: `${child.key}.${index}.${child2.key}.${i}.`,
|
||||
});
|
||||
if (Object.keys(item.errors).length) {
|
||||
throw new HttpError(422, "Validation failed", item.errors);
|
||||
}
|
||||
|
||||
const itemNames = [child2.owner.column, ...Object.keys(item.values)];
|
||||
const itemParams = [result.lastInsertRowid, ...Object.values(item.values)];
|
||||
if (!itemNames.includes("sort_order")) {
|
||||
itemNames.push("sort_order");
|
||||
itemParams.push(nestedPosition);
|
||||
}
|
||||
|
||||
db.prepare(
|
||||
`INSERT INTO ${child2.table} (${itemNames.join(", ")})
|
||||
VALUES (${itemNames.map(() => "?").join(", ")})`,
|
||||
).run(...itemParams);
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
for (const child2 of child.children ?? []) {
|
||||
writeRows(db, child2, String(result.lastInsertRowid), raw[child2.key], `${rowPrefix}${child2.key}.`, values);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function deleteChildren(db, child, ownerId) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue