"use client"; import { useCallback, useEffect, useState } from "react"; import { useRouter } from "next/navigation"; import { Button } from "@payloadcms/ui"; /** * Admin close/reopen control for minigame scoring periods. * * Rendered above the minigame-periods list. It reads periods through the REST * API (admin session cookie) and drives POST /api/minigame-periods/close, which * is gated on the `minigame-periods:close` permission; a 403 is surfaced inline. * The scheduled sweep lives in the period-tick bin, so this is the manual * override for closing early or reopening a closed period. */ interface PeriodRow { id: number; label: string; status: string; startDate: string; endDate: string; } const dateOnly = (iso: string): string => (iso ? iso.slice(0, 10) : ""); export default function PeriodCloseControl() { const router = useRouter(); const [rows, setRows] = useState(null); const [busy, setBusy] = useState(null); const [message, setMessage] = useState(null); const load = useCallback(async () => { try { const res = await fetch("/api/minigame-periods?limit=20&sort=-startDate&depth=0", { credentials: "include", }); if (!res.ok) throw new Error("load failed"); const json = (await res.json()) as { docs?: PeriodRow[] }; setRows( (json.docs ?? []).map((doc) => ({ id: doc.id, label: doc.label, status: doc.status, startDate: doc.startDate, endDate: doc.endDate, })), ); } catch { setRows([]); } }, []); useEffect(() => { void load(); }, [load]); const act = async (action: "close" | "reopen", periodId: number) => { setBusy(periodId); setMessage(null); try { const res = await fetch("/api/minigame-periods/close", { method: "POST", headers: { "content-type": "application/json" }, credentials: "include", body: JSON.stringify({ action, periodId }), }); const json = (await res.json().catch(() => ({}))) as { ok?: boolean; error?: string; closed?: { label?: string }[]; }; if (!res.ok || json.ok === false) { setMessage( res.status === 403 ? "You do not have permission to close or reopen periods." : (json.error ?? `Request failed (${res.status}).`), ); return; } setMessage( action === "close" ? `Closed "${json.closed?.[0]?.label ?? "period"}".` : "Period reopened.", ); await load(); router.refresh(); } catch { setMessage("Request failed."); } finally { setBusy(null); } }; if (rows === null) return null; return (
Period control Close the open period early, or reopen a closed one. The period tick usually does this on schedule.
{message && (

{message}

)}
{rows.length === 0 && ( No periods yet. The first one appears when a Core run is recorded or the period tick runs. )} {rows.slice(0, 6).map((row) => (
{row.label} {dateOnly(row.startDate)} to {dateOnly(row.endDate)} UTC {row.status} {row.status === "open" ? ( ) : ( )}
))}
); }