"use client"; /** * Church details (ยง2, decision 10): the facts every one of the church's sites shows (name, colours, * fonts and corners, logo, service times, contact, social links). They belong to the church's * Accounts and are saved there; they're not drafts: they change on all its sites at once. */ import { useState } from "react"; import type { Church } from "@crosscode/contracts"; import { ColourField } from "@crosscode/ui"; import { api, canEdit } from "@/admin/api"; import { ImagePicker } from "@/admin/media"; import { Btn, Field, inputClass, Loading, Notice, Panel, Title, useAction, useAdmin } from "@/admin/ui"; import { Building2, Clock, MapPin, Palette, Share2 } from "lucide-react"; const DAYS = ["sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"]; const PAIRINGS: [string, string][] = [["", "Your template's choice"], ["classic", "Classic"], ["scholarly", "Scholarly"], ["editorial", "Editorial"], ["impact", "Impact"], ["contemporary", "Contemporary"], ["friendly", "Friendly"], ["warm", "Warm"], ["modern", "Modern"]]; const NETWORKS = ["facebook", "instagram", "youtube", "tiktok", "x", "spotify", "whatsapp-channel", "other"]; type ServiceTime = Church["serviceTimes"][number]; type Social = Church["social"][number]; type Address = NonNullable; export default function ChurchDetails() { const me = useAdmin<{ user: { role: string } }>("/me"); const { data, error, reload } = useAdmin<{ church: Church | null; linked: boolean }>("/church"); const { busy, message, fields, run } = useAction(); const [edit, setEdit] = useState & { logoMediaId?: string | null } | null>(null); if (!data || !me.data) return ; if (!data.church) return <>Church details{data.linked ? "Your church's details can't be read from Accounts right now. Try again in a minute." : "This website isn't linked to your church's Accounts yet. It links by itself within 15 minutes of being installed."}; const c: Church = { ...data.church, ...edit } as Church; const editor = canEdit(me.data.user.role); const set = (patch: Partial) => setEdit({ ...edit, ...patch }); const times = c.serviceTimes; const setTime = (i: number, t: Partial) => set({ serviceTimes: times.map((x, j) => (j === i ? { ...x, ...t } : x)) }); const address: Address = c.contact.address ?? { line1: "", line2: null, suburb: null, city: "", province: null, postalCode: null, country: "ZA", geo: null }; const setAddress = (a: Partial
) => set({ contact: { ...c.contact, address: { ...address, ...a } } }); const save = () => run(async () => { // The media id only picks the logo here; Accounts keeps its address. const changes = { ...(edit ?? {}) }; delete changes.logoMediaId; await api("/church", { method: "PUT", body: { church: changes, ifUpdatedAt: data.church!.updatedAt } }); setEdit(null); await reload(); }, "Saved: all your church's sites show it now."); const err = (k: string) => fields[k]; return ( <> <Btn onClick={() => setEdit(null)}>Undo</Btn><Btn kind="primary" busy={busy} onClick={save}>Save for all your sites</Btn></>}> Church details {message &&
{message.text}
}
set({ name: e.target.value })} /> set({ shortName: e.target.value || null })} /> set({ branding: { ...c.branding, colours: { ...c.branding.colours, primary } } })} /> set({ branding: { ...c.branding, colours: { ...c.branding.colours, accent } } })} />
{c.branding.logoUrl && Your logo} run(async () => { if (!id) return set({ branding: { ...c.branding, logoUrl: null }, logoMediaId: null } as never); const media = await api<{ items: { id: string; url: string }[] }>("/media"); const url = media.items.find((m) => m.id === id)?.url; set({ branding: { ...c.branding, logoUrl: url ? `${window.location.origin}${url}` : null }, logoMediaId: id } as never); })} />
    {times.map((t, i) => (
  • setTime(i, { time: e.target.value })} /> setTime(i, { label: e.target.value || null })} /> setTime(i, { note: e.target.value || null })} /> set({ serviceTimes: times.filter((_, j) => j !== i) })}>Remove
  • ))}
{err("serviceTimes") &&

{err("serviceTimes")}

} set({ serviceTimes: [...times, { day: "sunday", time: "09:00", label: null, campus: null, note: null }] })}>Add a service time
set({ contact: { ...c.contact, email: e.target.value || null } })} /> set({ contact: { ...c.contact, phone: e.target.value.replace(/\s+/g, "") || null } })} /> set({ contact: { ...c.contact, whatsapp: e.target.value.replace(/\s+/g, "") || null } })} />
setAddress({ line1: e.target.value })} /> setAddress({ suburb: e.target.value || null })} /> setAddress({ city: e.target.value })} /> setAddress({ postalCode: e.target.value || null })} />
    {c.social.map((s, i) => (
  • set({ social: c.social.map((x, j) => (j === i ? { ...x, url: e.target.value } : x)) })} /> set({ social: c.social.filter((_, j) => j !== i) })}>Remove
  • ))}
{err("social") &&

{err("social")}

} set({ social: [...c.social, { network: "facebook", url: "https://" }] })}>Add a link
); }