Archived
Admin -> Users 'Limit to campuses'; staff campus pickers, reports, dashboards and audience picker follow the scope (useMyCampuses); scoped people pick one of their campuses for new events, and church-wide / other campuses' events open read-only; 'You work with ...' note; warning before turning campuses off while staff are limited. e2e: scanner refusal naming the campus, scoped events list and read-only editor. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
69 lines
2.1 KiB
TypeScript
69 lines
2.1 KiB
TypeScript
"use client";
|
|
|
|
import { useEffect, useState } from "react";
|
|
import { apiFetch } from "@/lib/api";
|
|
import { useAuth } from "@/hooks/useAuth";
|
|
|
|
export type Campus = {
|
|
id: string;
|
|
name: string;
|
|
slug: string;
|
|
address: string | null;
|
|
colour: string | null;
|
|
logoUrl: string | null;
|
|
defaultLocation: string | null;
|
|
contactName: string | null;
|
|
contactEmail: string | null;
|
|
contactPhone: string | null;
|
|
order: number;
|
|
};
|
|
|
|
/** What an event carries about its campus. */
|
|
export type EventCampus = { id: string; name: string; slug: string; colour: string | null };
|
|
|
|
type CampusState = { enabled: boolean; campuses: Campus[] };
|
|
|
|
// One request per page load, shared by every component that asks.
|
|
let cache: Promise<CampusState> | null = null;
|
|
|
|
function load(): Promise<CampusState> {
|
|
if (!cache) {
|
|
cache = apiFetch<CampusState>("/api/campuses").catch(() => {
|
|
cache = null; // try again next time
|
|
return { enabled: false, campuses: [] };
|
|
});
|
|
}
|
|
return cache;
|
|
}
|
|
|
|
/** Forget the cached list (after Site settings → Campuses changes it). */
|
|
export function reloadCampuses() {
|
|
cache = null;
|
|
}
|
|
|
|
/**
|
|
* Whether campuses are on (Site settings → Campuses) and the active campuses. While off,
|
|
* `enabled` is false and `campuses` is empty, so callers hide every campus field.
|
|
*/
|
|
export function useCampuses() {
|
|
const [state, setState] = useState<CampusState & { loading: boolean }>({ enabled: false, campuses: [], loading: true });
|
|
useEffect(() => {
|
|
let alive = true;
|
|
load().then(s => { if (alive) setState({ ...s, loading: false }); });
|
|
return () => { alive = false; };
|
|
}, []);
|
|
return state;
|
|
}
|
|
|
|
/**
|
|
* The campuses the signed-in person works with: for campus-scoped staff only their own campuses
|
|
* (`scoped` true — they can't set up events for all campuses), for everyone else every campus.
|
|
* Staff dropdowns use this; the public site uses useCampuses.
|
|
*/
|
|
export function useMyCampuses() {
|
|
const all = useCampuses();
|
|
const { user } = useAuth();
|
|
const scope = all.enabled ? user?.campusScope || null : null;
|
|
return { ...all, scoped: !!scope, campuses: scope ? all.campuses.filter(c => scope.includes(c.id)) : all.campuses };
|
|
}
|