"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"; type Person = { id: string; name: string; email: string; role: string; ministryId: string | null; ministry: string | null; lastSignInAt: string | null }; const ROLE_TEXT: Record = { 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 }) { return (
{role === "ministry_leader" && ( )}
); } export default function People() { const me = useAdmin<{ user: { id: string; role: string } }>("/me"); const list = useAdmin("/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 ; const owner = me.data.user.role === "owner"; const mayTouch = (p: Person) => p.id !== me.data!.user.id && (owner || p.role !== "owner"); return ( <> People {message &&
{message.text}
}
    {list.data.map((p) => (
  • {editing?.id === p.id ? (

    {p.name}

    setEditing({ ...editing, role, ministryId })} />
    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 setEditing(null)}>Cancel
    ) : (

    {p.name}{p.id === me.data!.user.id && " (you)"}

    {p.email} · {ROLE_NAMES[p.role]}{p.ministry && ` of ${p.ministry}`}

    {p.lastSignInAt ? `Last signed in ${when(p.lastSignInAt)}` : "Hasn't signed in yet"}

    {mayTouch(p) && (
    setEditing({ id: p.id, role: p.role, ministryId: p.ministryId })}>Change role run(async () => { await api(`/people/${p.id}`, { method: "DELETE" }); await list.reload(); }, `${p.name} no longer has access.`)}>Remove
    )}
    )}
  • ))}
setAdd({ ...add, name: e.target.value })} /> setAdd({ ...add, email: e.target.value })} /> setAdd({ ...add, role, ministryId })} /> 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
); }