Files
crosscode-platform/apps/website/frontend/src/app/admin/people/page.tsx
T
joshuaandClaude Opus 5.5 dcc22a5c16 Accounts 1.8.0, Website 1.1.0: admins redesigned as Cross Events' dashboard and settings
- Website: an icon sidebar in groups (Your site, Content, Go live, Team), a
  dashboard with cards and a tile per part of the admin, steps to the first
  publish across the top of every admin page, and a live preview beside the
  page editor and Template and style (computer or phone width, folds away,
  reloads on save). A framed request (Sec-Fetch-Dest: iframe) with an admin
  session reads the drafts, so no preview cookie is set. /preview/start
  takes ?to=. The page editor has Content and Page details tabs.
- Accounts: /admin is an overview (cards, "Needs you", a tile per section);
  People moved to /admin/people; the sidebar shows the part you're in,
  grouped, with a count on Deletion requests; People redesigned, with a
  confirm before Deactivate; Email and WhatsApp split into two cards.
- Both: darker grey labels for contrast; lucide-react in the Website.
- e2e: the live preview shows drafts while the site stays live; axe on the
  page editor.
- docs: VERSIONS, changelogs, WHATS-NEW.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-26 19:57:22 +02:00

106 lines
6.2 KiB
TypeScript

"use client";
/**
* People (§10): who can use this website's admin, and as what. Adding someone by email gives them
* an account at the church's sign-in if they have none (they get an email to choose a password).
*/
import { useState } from "react";
import { api, ROLE_NAMES } from "@/admin/api";
import { Btn, Field, inputClass, Loading, Notice, Panel, Title, useAction, useAdmin, when } from "@/admin/ui";
import { UserCog } from "lucide-react";
type Person = { id: string; name: string; email: string; role: string; ministryId: string | null; ministry: string | null; lastSignInAt: string | null };
const ROLE_TEXT: Record<string, string> = {
owner: "Everything, including who is Owner",
admin: "Everything but Owners",
editor: "Edits everything and publishes",
ministry_leader: "Edits their own ministry; an Editor publishes it",
viewer: "Looks, changes nothing",
};
function RolePicker({ role, ministryId, onChange, ministries, canOwner, errors }: { role: string; ministryId: string | null; onChange: (r: string, m: string | null) => void; ministries: { id: string; name: string }[]; canOwner: boolean; errors: Record<string, string> }) {
return (
<div className="grid gap-3 sm:grid-cols-2">
<Field label="Role" hint={ROLE_TEXT[role]} error={errors.role}>
<select className={inputClass} value={role} onChange={(e) => onChange(e.target.value, ministryId)}>
{Object.keys(ROLE_TEXT).filter((r) => canOwner || r !== "owner").map((r) => <option key={r} value={r}>{ROLE_NAMES[r]}</option>)}
</select>
</Field>
{role === "ministry_leader" && (
<Field label="Their ministry" error={errors.ministryId}>
<select className={inputClass} value={ministryId ?? ""} onChange={(e) => onChange(role, e.target.value || null)}>
<option value="">Choose…</option>
{ministries.map((m) => <option key={m.id} value={m.id}>{m.name}</option>)}
</select>
</Field>
)}
</div>
);
}
export default function People() {
const me = useAdmin<{ user: { id: string; role: string } }>("/me");
const list = useAdmin<Person[]>("/people");
const ministries = useAdmin<{ id: string; name: string }[]>("/ministries");
const [add, setAdd] = useState({ name: "", email: "", role: "editor", ministryId: null as string | null });
const [editing, setEditing] = useState<{ id: string; role: string; ministryId: string | null } | null>(null);
const { busy, message, fields, run } = useAction();
if (!list.data || !me.data) return <Loading error={list.error ?? me.error} />;
const owner = me.data.user.role === "owner";
const mayTouch = (p: Person) => p.id !== me.data!.user.id && (owner || p.role !== "owner");
return (
<>
<Title icon={UserCog} sub="Who can use this website's admin. They sign in with their church account.">People</Title>
{message && <div className="mb-4"><Notice tone={message.tone}>{message.text}</Notice></div>}
<div className="grid gap-6 lg:grid-cols-[3fr_2fr]">
<Panel>
<ul className="divide-y divide-gray-200">
{list.data.map((p) => (
<li key={p.id} className="py-3">
{editing?.id === p.id ? (
<div className="space-y-3">
<p className="font-medium">{p.name}</p>
<RolePicker role={editing.role} ministryId={editing.ministryId} ministries={ministries.data ?? []} canOwner={owner} errors={fields} onChange={(role, ministryId) => setEditing({ ...editing, role, ministryId })} />
<div className="flex gap-2">
<Btn kind="primary" busy={busy} onClick={() => run(async () => { await api(`/people/${p.id}`, { method: "PUT", body: { role: editing.role, ministryId: editing.ministryId } }); setEditing(null); await list.reload(); }, "Saved. A lower role takes effect at once.")}>Save</Btn>
<Btn onClick={() => setEditing(null)}>Cancel</Btn>
</div>
</div>
) : (
<div className="flex flex-wrap items-center justify-between gap-3">
<div>
<p className="font-medium">{p.name}{p.id === me.data!.user.id && " (you)"}</p>
<p className="text-sm text-gray-600">{p.email} · {ROLE_NAMES[p.role]}{p.ministry && ` of ${p.ministry}`}</p>
<p className="text-xs text-gray-500">{p.lastSignInAt ? `Last signed in ${when(p.lastSignInAt)}` : "Hasn't signed in yet"}</p>
</div>
{mayTouch(p) && (
<div className="flex gap-2">
<Btn onClick={() => setEditing({ id: p.id, role: p.role, ministryId: p.ministryId })}>Change role</Btn>
<Btn kind="danger" onClick={() => run(async () => { await api(`/people/${p.id}`, { method: "DELETE" }); await list.reload(); }, `${p.name} no longer has access.`)}>Remove</Btn>
</div>
)}
</div>
)}
</li>
))}
</ul>
</Panel>
<Panel title="Add someone">
<div className="space-y-3">
<Field label="Name" error={fields.name}><input className={inputClass} value={add.name} maxLength={200} onChange={(e) => setAdd({ ...add, name: e.target.value })} /></Field>
<Field label="Email" hint="If they have no church account yet, they get an email to choose a password." error={fields.email}><input type="email" className={inputClass} value={add.email} onChange={(e) => setAdd({ ...add, email: e.target.value })} /></Field>
<RolePicker role={add.role} ministryId={add.ministryId} ministries={ministries.data ?? []} canOwner={owner} errors={fields} onChange={(role, ministryId) => setAdd({ ...add, role, ministryId })} />
<Btn kind="primary" busy={busy} disabled={!add.name || !add.email} onClick={() => run(async () => {
const r = await api<{ newAccount: boolean }>("/people", { body: add });
setAdd({ name: "", email: "", role: "editor", ministryId: null });
await list.reload();
return r;
}, "Added. They can sign in now.")}>Add</Btn>
</div>
</Panel>
</div>
</>
);
}