Planning Center: compare a People list with the site's people and apply what an admin picks
Migration 085 adds people.pco_id and Site settings' list. Data -> Planning Center fetches the list, suggests matches (linked, same email, same name), shows each field that would change (Planning Center wins), and applies the chosen links, updates and new unpublished people in one transaction. Needs PCO_APP_ID and PCO_SECRET. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
a10ff6d75f
commit
e8aab4d7ec
12 changed files with 607 additions and 7 deletions
|
|
@ -81,6 +81,7 @@ const AdminVideoFrames = lazyPage(() => import("./pages/admin/AdminVideoFrames.t
|
|||
const AdminNewsletter = lazyPage(() => import("./pages/admin/AdminNewsletter.tsx"))
|
||||
const AdminProfiles = lazyPage(() => import("./pages/admin/AdminProfiles.tsx"))
|
||||
const AdminConvert = lazyPage(() => import("./pages/admin/AdminConvert.tsx"))
|
||||
const AdminPco = lazyPage(() => import("./pages/admin/AdminPco.tsx"))
|
||||
const EntityList = lazyPage(() => import("./pages/admin/EntityList.tsx"))
|
||||
const EntityEdit = lazyPage(() => import("./pages/admin/EntityEdit.tsx"))
|
||||
|
||||
|
|
@ -145,6 +146,7 @@ export default function App() {
|
|||
<Route element={<RequireRole role="admin" />}>
|
||||
<Route path="clips" element={<AdminClips />} />
|
||||
<Route path="profiles" element={<AdminProfiles />} />
|
||||
<Route path="pco" element={<AdminPco />} />
|
||||
</Route>
|
||||
<Route path=":entity" element={<EntityList />} />
|
||||
<Route path=":entity/:id" element={<EntityEdit />} />
|
||||
|
|
|
|||
|
|
@ -159,6 +159,16 @@ export const siteSettings = {
|
|||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
legend: "Planning Center",
|
||||
fields: [
|
||||
{
|
||||
path: "pco_list_id",
|
||||
label: "People list",
|
||||
help: "The Planning Center list Data → Planning Center brings in: its number, from the list's address in Planning Center People, eg. 1234567. Needs PCO_APP_ID and PCO_SECRET on the server (docs/planning-center.md).",
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
legend: "Animated words",
|
||||
note: WORD_HELP + " Works in the hero's headline, section titles, headings and paragraphs. These are the defaults; a hero or a block can set its own. Visitors who ask for less motion see the first word, still.",
|
||||
|
|
|
|||
240
src/pages/admin/AdminPco.tsx
Normal file
240
src/pages/admin/AdminPco.tsx
Normal file
|
|
@ -0,0 +1,240 @@
|
|||
/* ═══════════════════════════════════════════════════════════════
|
||||
ADMIN: PLANNING CENTER (Data → Planning Center, admins and up)
|
||||
|
||||
One Planning Center People list into the site's people, by hand
|
||||
(server/src/planningCenter.js). Compare fetches the list and shows
|
||||
each member: the site person they're linked to, or the one
|
||||
suggested (same email, else same name), and the fields that would
|
||||
change, Planning Center's value winning. For each, choose: link
|
||||
(and update), another person, a new unpublished person, or skip.
|
||||
Nothing is written until Apply.
|
||||
═══════════════════════════════════════════════════════════════ */
|
||||
|
||||
import { useEffect, useMemo, useState } from "react";
|
||||
import { Link } from "react-router-dom";
|
||||
|
||||
import { ApiError, get, post } from "../../lib/api.ts";
|
||||
|
||||
const MUTED = "var(--color-muted)";
|
||||
const button =
|
||||
"btn border border-brand px-4 py-2 text-sm text-link transition-colors hover:bg-tint disabled:cursor-default disabled:opacity-50";
|
||||
const primary =
|
||||
"btn bg-action px-5 py-2.5 text-white transition-colors hover:bg-action-hover disabled:bg-muted/25";
|
||||
const input =
|
||||
"border border-muted/25 bg-surface px-2 py-1.5 text-sm text-body outline-none focus:border-brand focus:ring-2 focus:ring-brand/25";
|
||||
|
||||
type Member = { id: string; first: string; last: string; name: string; email: string | null; phone: string | null };
|
||||
type Row = {
|
||||
pco: Member;
|
||||
match: { person_id: string; name: string; how: "linked" | "email" | "name" } | null;
|
||||
changes: { field: string; label: string; site: string | null; pco: string }[];
|
||||
};
|
||||
/* What the admin picked for a member: a person id, "new", or "" (skip). */
|
||||
type Choice = { target: string; update: boolean };
|
||||
|
||||
const HOW: Record<string, string> = {
|
||||
linked: "Linked",
|
||||
email: "Suggested: same email",
|
||||
name: "Suggested: same name",
|
||||
};
|
||||
|
||||
const message = (err: unknown) => (err instanceof ApiError ? err.message : "Couldn't reach the server.");
|
||||
|
||||
export default function AdminPco() {
|
||||
const [status, setStatus] = useState<{ ready: boolean; list_id: string | null } | null>(null);
|
||||
const [rows, setRows] = useState<Row[] | null>(null);
|
||||
const [people, setPeople] = useState<{ id: string; label: string }[]>([]);
|
||||
const [choices, setChoices] = useState<Record<string, Choice>>({});
|
||||
const [busy, setBusy] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [done, setDone] = useState<string | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
get("/admin/pco", { ttl: 0 }).then(setStatus).catch((err) => setError(message(err)));
|
||||
get("/admin/options?only=people", { ttl: 0 })
|
||||
.then((body) => setPeople(body.options.people))
|
||||
.catch(() => setPeople([]));
|
||||
}, []);
|
||||
|
||||
async function compare() {
|
||||
setBusy(true);
|
||||
setError(null);
|
||||
setDone(null);
|
||||
try {
|
||||
const body = await post("/admin/pco/compare", {});
|
||||
setRows(body.rows);
|
||||
// Linked or suggested: that person, updated; else skip.
|
||||
setChoices(
|
||||
Object.fromEntries(
|
||||
body.rows.map((row: Row) => [row.pco.id, { target: row.match?.person_id ?? "", update: Boolean(row.match) }]),
|
||||
),
|
||||
);
|
||||
} catch (err) {
|
||||
setError(message(err));
|
||||
} finally {
|
||||
setBusy(false);
|
||||
}
|
||||
}
|
||||
|
||||
async function applyAll() {
|
||||
if (!rows) return;
|
||||
setBusy(true);
|
||||
setError(null);
|
||||
try {
|
||||
const decisions = rows
|
||||
.map((row) => ({ row, choice: choices[row.pco.id] }))
|
||||
.filter(({ choice }) => choice?.target)
|
||||
.map(({ row, choice }) =>
|
||||
choice.target === "new"
|
||||
? { pco_id: row.pco.id, action: "create" }
|
||||
: { pco_id: row.pco.id, action: "link", person_id: choice.target, update: choice.update },
|
||||
);
|
||||
const body = await post("/admin/pco/apply", { decisions });
|
||||
setDone(`Done: ${body.counts.linked} linked, ${body.counts.updated} updated, ${body.counts.created} new (unpublished).`);
|
||||
await compare();
|
||||
} catch (err) {
|
||||
setError(message(err));
|
||||
} finally {
|
||||
setBusy(false);
|
||||
}
|
||||
}
|
||||
|
||||
const counts = useMemo(() => {
|
||||
const picked = Object.values(choices);
|
||||
return {
|
||||
link: picked.filter((c) => c.target && c.target !== "new").length,
|
||||
create: picked.filter((c) => c.target === "new").length,
|
||||
};
|
||||
}, [choices]);
|
||||
|
||||
const setChoice = (id: string, next: Partial<Choice>) =>
|
||||
setChoices((current) => ({ ...current, [id]: { ...current[id], ...next } }));
|
||||
|
||||
return (
|
||||
<div>
|
||||
<div className="flex flex-wrap items-baseline gap-3">
|
||||
<h1 className="text-2xl font-bold text-link">Planning Center</h1>
|
||||
<p className="text-sm" style={{ color: MUTED }}>
|
||||
Bring a Planning Center People list into the site's people. Planning Center wins; you review each person first.
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<section className="mt-6 bg-raised p-5 shadow-md">
|
||||
{!status ? (
|
||||
<p className="text-sm" style={{ color: MUTED }}>Checking…</p>
|
||||
) : !status.ready ? (
|
||||
<p className="text-sm text-body">
|
||||
Not set up on the server: it needs PCO_APP_ID and PCO_SECRET (docs/planning-center.md).
|
||||
</p>
|
||||
) : !status.list_id ? (
|
||||
<p className="text-sm text-body">
|
||||
Pick the list in <Link to="/admin/site_settings" className="text-link underline">Site settings → Planning Center</Link> (superadmins).
|
||||
</p>
|
||||
) : (
|
||||
<div className="flex flex-wrap items-center gap-3">
|
||||
<button type="button" className={primary} disabled={busy} onClick={compare}>
|
||||
{busy && !rows ? "Fetching…" : rows ? "Compare again" : "Compare with Planning Center"}
|
||||
</button>
|
||||
<span className="text-sm" style={{ color: MUTED }}>
|
||||
List {status.list_id}. Name, and email and phone into the private fields.
|
||||
</span>
|
||||
</div>
|
||||
)}
|
||||
{error && <p className="mt-3 text-sm text-danger">{error}</p>}
|
||||
{done && <p className="mt-3 text-sm text-body">{done}</p>}
|
||||
</section>
|
||||
|
||||
{rows && (
|
||||
<>
|
||||
<div className="mt-6 flex flex-wrap items-center gap-3">
|
||||
<button type="button" className={primary} disabled={busy || counts.link + counts.create === 0} onClick={applyAll}>
|
||||
{busy ? "Applying…" : `Apply: ${counts.link} linked, ${counts.create} new`}
|
||||
</button>
|
||||
<span className="text-sm" style={{ color: MUTED }}>
|
||||
{rows.length} on the list. Skipped ones aren't touched.
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<ul className="mt-4 space-y-3">
|
||||
{rows.map((row) => {
|
||||
const choice = choices[row.pco.id] ?? { target: "", update: false };
|
||||
const sameAsMatch = choice.target && choice.target === row.match?.person_id;
|
||||
return (
|
||||
<li key={row.pco.id} className="bg-raised p-4 shadow-sm">
|
||||
<div className="flex flex-wrap items-start justify-between gap-3">
|
||||
<div>
|
||||
<p className="font-semibold text-body">{row.pco.name || "(no name)"}</p>
|
||||
<p className="text-xs" style={{ color: MUTED }}>
|
||||
{[row.pco.email, row.pco.phone].filter(Boolean).join(" · ") || "No email or phone"}
|
||||
</p>
|
||||
<p className="mt-1 text-xs text-body">
|
||||
{row.match ? `${HOW[row.match.how]}: ${row.match.name}` : "No match on the site"}
|
||||
</p>
|
||||
</div>
|
||||
<div className="flex flex-wrap items-center gap-2">
|
||||
<label className="sr-only" htmlFor={`pco-${row.pco.id}`}>
|
||||
What to do with {row.pco.name}
|
||||
</label>
|
||||
<select
|
||||
id={`pco-${row.pco.id}`}
|
||||
value={choice.target}
|
||||
onChange={(e) => setChoice(row.pco.id, { target: e.target.value, update: e.target.value !== "new" })}
|
||||
className={input}
|
||||
>
|
||||
<option value="">Skip</option>
|
||||
<option value="new">Make a new person (unpublished)</option>
|
||||
{row.match && <option value={row.match.person_id}>Link to {row.match.name}</option>}
|
||||
<optgroup label="Link to someone else">
|
||||
{people
|
||||
.filter((p) => p.id !== row.match?.person_id)
|
||||
.map((p) => (
|
||||
<option key={p.id} value={p.id}>
|
||||
{p.label}
|
||||
</option>
|
||||
))}
|
||||
</optgroup>
|
||||
</select>
|
||||
{choice.target && choice.target !== "new" && (
|
||||
<label className="flex items-center gap-2 text-sm text-body">
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={choice.update}
|
||||
onChange={(e) => setChoice(row.pco.id, { update: e.target.checked })}
|
||||
/>
|
||||
Update from Planning Center
|
||||
</label>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
{sameAsMatch && row.changes.length > 0 && (
|
||||
<table className="mt-3 w-full text-left text-sm">
|
||||
<thead className="text-xs uppercase tracking-wider" style={{ color: MUTED }}>
|
||||
<tr>
|
||||
<th className="py-1 pr-4 font-medium">Field</th>
|
||||
<th className="py-1 pr-4 font-medium">On the site</th>
|
||||
<th className="py-1 font-medium">Planning Center</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{row.changes.map((change) => (
|
||||
<tr key={change.field} className="border-t border-muted/10">
|
||||
<td className="py-1 pr-4 text-body">{change.label}</td>
|
||||
<td className="py-1 pr-4" style={{ color: MUTED }}>{change.site ?? "-"}</td>
|
||||
<td className="py-1 text-body">{change.pco}</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
)}
|
||||
{sameAsMatch && row.changes.length === 0 && (
|
||||
<p className="mt-2 text-xs" style={{ color: MUTED }}>Already the same.</p>
|
||||
)}
|
||||
</li>
|
||||
);
|
||||
})}
|
||||
</ul>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
|
@ -101,6 +101,12 @@ export const DATA_NAV = {
|
|||
blurb: "Invite people to make a profile account, approve their claims to a People record, and link their Discord.",
|
||||
minRole: "admin",
|
||||
},
|
||||
{
|
||||
to: "/admin/pco",
|
||||
label: "Planning Center",
|
||||
blurb: "Bring a Planning Center People list into People: compare each person, then link, update or add them.",
|
||||
minRole: "admin",
|
||||
},
|
||||
{
|
||||
to: "/admin/blog_posts",
|
||||
label: "Blog posts",
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue