Regions: featured cards in the region list and on the region's page (migration 072)
A region owns featured cards like the front page does, set in its editor (shown when Kind is region). They show in its row of the region list when opened, and in a Featured section on its page. featured_cards takes an 'organization' owner kind; the table is rebuilt, and a region's cards go when it does. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
07be48d770
commit
2977168af5
9 changed files with 181 additions and 17 deletions
|
|
@ -1,4 +1,4 @@
|
|||
import { text, int, real, bool, enumeration, placeColumns, timelineExtension, linksChild, blocksChild } from "./columns.js";
|
||||
import { text, int, real, bool, enumeration, placeColumns, timelineExtension, linksChild, blocksChild, featuredCardsChild } from "./columns.js";
|
||||
import { tagsChild } from "./tags.js";
|
||||
|
||||
/* ── Organizations ───────────────────────────────────────────── */
|
||||
|
|
@ -86,6 +86,9 @@ export const organizations = {
|
|||
text("phone"),
|
||||
],
|
||||
},
|
||||
// A region's own featured cards (072), in its row of the region
|
||||
// list and on its page.
|
||||
{ ...featuredCardsChild("organization"), when: { column: "kind", value: "region" } },
|
||||
{
|
||||
key: "region_areas",
|
||||
table: "region_areas",
|
||||
|
|
|
|||
111
server/src/migrations/072_region_featured_cards.sql
Normal file
111
server/src/migrations/072_region_featured_cards.sql
Normal file
|
|
@ -0,0 +1,111 @@
|
|||
-- ═══════════════════════════════════════════════════════════════
|
||||
-- 072 REGION FEATURED CARDS
|
||||
--
|
||||
-- A region gets featured cards of its own, the same cards the front
|
||||
-- page and the Community blog own, shown in its row of the region
|
||||
-- list and on its page. featured_cards takes a third owner kind,
|
||||
-- 'organization'. The CHECK can't change in place, so the table is
|
||||
-- rebuilt, then its index and triggers are made again.
|
||||
--
|
||||
-- An organization's own cards go when it does
|
||||
-- (organizations_owned_featured_cleanup), and an insert has to name
|
||||
-- an organization that exists (featured_cards_owner_exists).
|
||||
-- ═══════════════════════════════════════════════════════════════
|
||||
|
||||
CREATE TABLE featured_cards_new (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
owner_kind TEXT NOT NULL CHECK (owner_kind IN ('front_page', 'community_page', 'organization')),
|
||||
owner_id TEXT NOT NULL,
|
||||
sort_order INTEGER NOT NULL DEFAULT 0,
|
||||
|
||||
kind TEXT NOT NULL DEFAULT 'link'
|
||||
CHECK (kind IN ('record', 'link', 'video')),
|
||||
|
||||
ref_kind TEXT CHECK (ref_kind IN ('event', 'organization', 'person', 'team', 'award')),
|
||||
ref_id TEXT,
|
||||
url TEXT,
|
||||
|
||||
eyebrow TEXT,
|
||||
title TEXT,
|
||||
blurb TEXT,
|
||||
image TEXT,
|
||||
|
||||
CHECK ((ref_kind IS NULL) = (ref_id IS NULL)),
|
||||
CONSTRAINT "A record card needs a record to point at."
|
||||
CHECK (kind != 'record' OR ref_kind IS NOT NULL),
|
||||
CONSTRAINT "A link or video card needs a URL."
|
||||
CHECK (kind = 'record' OR url IS NOT NULL)
|
||||
) STRICT;
|
||||
|
||||
INSERT INTO featured_cards_new
|
||||
(id, owner_kind, owner_id, sort_order, kind, ref_kind, ref_id, url, eyebrow, title, blurb, image)
|
||||
SELECT id, owner_kind, owner_id, sort_order, kind, ref_kind, ref_id, url, eyebrow, title, blurb, image
|
||||
FROM featured_cards;
|
||||
|
||||
-- The cleanup triggers on events, people, teams, awards and
|
||||
-- organizations name featured_cards; legacy mode leaves them alone
|
||||
-- through the rename.
|
||||
PRAGMA legacy_alter_table = ON;
|
||||
DROP TABLE featured_cards;
|
||||
ALTER TABLE featured_cards_new RENAME TO featured_cards;
|
||||
PRAGMA legacy_alter_table = OFF;
|
||||
|
||||
CREATE INDEX featured_cards_owner_idx ON featured_cards (owner_kind, owner_id, sort_order);
|
||||
|
||||
CREATE TRIGGER organizations_owned_featured_cleanup
|
||||
AFTER DELETE ON organizations
|
||||
BEGIN
|
||||
DELETE FROM featured_cards WHERE owner_kind = 'organization' AND owner_id = old.id;
|
||||
END;
|
||||
|
||||
CREATE TRIGGER featured_cards_owner_exists
|
||||
BEFORE INSERT ON featured_cards
|
||||
WHEN new.owner_kind = 'organization'
|
||||
BEGIN
|
||||
SELECT RAISE(ABORT, 'featured_cards: no such organization')
|
||||
WHERE NOT EXISTS (SELECT 1 FROM organizations WHERE id = new.owner_id);
|
||||
END;
|
||||
|
||||
CREATE TRIGGER featured_cards_ref_exists
|
||||
BEFORE INSERT ON featured_cards
|
||||
BEGIN
|
||||
SELECT CASE
|
||||
WHEN new.ref_kind = 'event'
|
||||
AND NOT EXISTS (SELECT 1 FROM events WHERE id = new.ref_id)
|
||||
THEN RAISE(ABORT, 'featured_cards: no such event')
|
||||
WHEN new.ref_kind = 'organization'
|
||||
AND NOT EXISTS (SELECT 1 FROM organizations WHERE id = new.ref_id)
|
||||
THEN RAISE(ABORT, 'featured_cards: no such organization')
|
||||
WHEN new.ref_kind = 'person'
|
||||
AND NOT EXISTS (SELECT 1 FROM people WHERE id = new.ref_id)
|
||||
THEN RAISE(ABORT, 'featured_cards: no such person')
|
||||
WHEN new.ref_kind = 'team'
|
||||
AND NOT EXISTS (SELECT 1 FROM teams WHERE id = new.ref_id)
|
||||
THEN RAISE(ABORT, 'featured_cards: no such team')
|
||||
WHEN new.ref_kind = 'award'
|
||||
AND NOT EXISTS (SELECT 1 FROM awards WHERE id = new.ref_id)
|
||||
THEN RAISE(ABORT, 'featured_cards: no such award')
|
||||
END;
|
||||
END;
|
||||
|
||||
CREATE TRIGGER featured_cards_ref_exists_update
|
||||
BEFORE UPDATE OF ref_kind, ref_id ON featured_cards
|
||||
BEGIN
|
||||
SELECT CASE
|
||||
WHEN new.ref_kind = 'event'
|
||||
AND NOT EXISTS (SELECT 1 FROM events WHERE id = new.ref_id)
|
||||
THEN RAISE(ABORT, 'featured_cards: no such event')
|
||||
WHEN new.ref_kind = 'organization'
|
||||
AND NOT EXISTS (SELECT 1 FROM organizations WHERE id = new.ref_id)
|
||||
THEN RAISE(ABORT, 'featured_cards: no such organization')
|
||||
WHEN new.ref_kind = 'person'
|
||||
AND NOT EXISTS (SELECT 1 FROM people WHERE id = new.ref_id)
|
||||
THEN RAISE(ABORT, 'featured_cards: no such person')
|
||||
WHEN new.ref_kind = 'team'
|
||||
AND NOT EXISTS (SELECT 1 FROM teams WHERE id = new.ref_id)
|
||||
THEN RAISE(ABORT, 'featured_cards: no such team')
|
||||
WHEN new.ref_kind = 'award'
|
||||
AND NOT EXISTS (SELECT 1 FROM awards WHERE id = new.ref_id)
|
||||
THEN RAISE(ABORT, 'featured_cards: no such award')
|
||||
END;
|
||||
END;
|
||||
|
|
@ -56,6 +56,7 @@ import {
|
|||
asBool,
|
||||
json,
|
||||
loadBlocks,
|
||||
loadFeaturedCardsFor,
|
||||
loadLinks,
|
||||
loadOwned,
|
||||
marks,
|
||||
|
|
@ -286,6 +287,9 @@ function attachRegionDetails(db, orgs) {
|
|||
)
|
||||
.all(...ids);
|
||||
|
||||
// Its own featured cards (072), for its row and its page.
|
||||
const featuredBy = loadFeaturedCardsFor(db, "organization", ids);
|
||||
|
||||
const byId = Object.fromEntries(rows.map((r) => [r.id, r]));
|
||||
const areasBy = new Map();
|
||||
const childrenBy = new Map();
|
||||
|
|
@ -321,6 +325,7 @@ function attachRegionDetails(db, orgs) {
|
|||
map_note: byId[org.id]?.map_note ?? null,
|
||||
areas: areasBy.get(org.id) ?? [],
|
||||
groups: childrenBy.get(org.id) ?? [],
|
||||
featured: featuredBy.get(org.id) ?? [],
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -226,7 +226,7 @@ export function shapeSeries(row) {
|
|||
}
|
||||
|
||||
/* ── Featured cards ────────────────────────────────────────────
|
||||
One owner's cards (the front page's, today), in order, with
|
||||
One owner's cards (the front page's, a region's), in order, with
|
||||
each record card filled in from its record. Shared so the
|
||||
Community blog's cards will come out the same shape.
|
||||
|
||||
|
|
@ -285,13 +285,22 @@ const FEATURED_REFS = {
|
|||
};
|
||||
|
||||
export function loadFeaturedCards(db, ownerKind, ownerId) {
|
||||
return loadFeaturedCardsFor(db, ownerKind, [ownerId]).get(ownerId) ?? [];
|
||||
}
|
||||
|
||||
/* Several owners' cards at once (every region in the region list),
|
||||
as owner id → cards, in the same number of queries as one owner. */
|
||||
export function loadFeaturedCardsFor(db, ownerKind, ownerIds) {
|
||||
const byOwner = new Map();
|
||||
if (ownerIds.length === 0) return byOwner;
|
||||
|
||||
const rows = db
|
||||
.prepare(
|
||||
`SELECT * FROM featured_cards
|
||||
WHERE owner_kind = ? AND owner_id = ?
|
||||
ORDER BY sort_order, id`,
|
||||
WHERE owner_kind = ? AND owner_id IN (${placeholders(ownerIds.length)})
|
||||
ORDER BY owner_id, sort_order, id`,
|
||||
)
|
||||
.all(ownerKind, ownerId);
|
||||
.all(ownerKind, ...ownerIds);
|
||||
|
||||
// kind → id → record row
|
||||
const records = new Map();
|
||||
|
|
@ -306,8 +315,9 @@ export function loadFeaturedCards(db, ownerKind, ownerId) {
|
|||
|
||||
const own = (row) => (row.image ? { file: row.image, kind: "featured" } : null);
|
||||
|
||||
const cards = [];
|
||||
for (const row of rows) {
|
||||
let cards = byOwner.get(row.owner_id);
|
||||
if (!cards) byOwner.set(row.owner_id, (cards = []));
|
||||
if (row.kind === "record") {
|
||||
const record = records.get(row.ref_kind)?.get(row.ref_id);
|
||||
if (!record) continue; // unpublished, or its organization is
|
||||
|
|
@ -332,7 +342,7 @@ export function loadFeaturedCards(db, ownerKind, ownerId) {
|
|||
});
|
||||
}
|
||||
}
|
||||
return cards;
|
||||
return byOwner;
|
||||
}
|
||||
|
||||
export { asBool };
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue