Phase 7 of the minigame periods feature: - New admin component `PeriodCloseControl` renders above the minigame-periods list: it shows the recent periods with their status and a Close now / Reopen button that drives POST /api/minigame-periods/close (gated on minigame-periods:close; a 403 is shown inline rather than swallowed). - Registered via `admin.components.beforeListTable` and the import map regenerated. Registry CRUD and the period/entry/achievement review lists already exist as permission-gated admin collection views, so this was the only missing surface. Verified end to end in the running admin panel: an open period closed, its status flipped and closedAt stamped, then reopened.
173 lines
5.2 KiB
TypeScript
173 lines
5.2 KiB
TypeScript
"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<PeriodRow[] | null>(null);
|
|
const [busy, setBusy] = useState<number | null>(null);
|
|
const [message, setMessage] = useState<string | null>(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 (
|
|
<div
|
|
style={{
|
|
border: "1px solid var(--theme-elevation-150)",
|
|
borderRadius: 8,
|
|
padding: "12px 16px",
|
|
marginBottom: 16,
|
|
}}
|
|
>
|
|
<div style={{ display: "flex", alignItems: "baseline", gap: 8 }}>
|
|
<strong>Period control</strong>
|
|
<span style={{ opacity: 0.7, fontSize: 13 }}>
|
|
Close the open period early, or reopen a closed one. The period tick usually does this on
|
|
schedule.
|
|
</span>
|
|
</div>
|
|
|
|
{message && (
|
|
<p style={{ marginTop: 8, fontSize: 13 }} role="status">
|
|
{message}
|
|
</p>
|
|
)}
|
|
|
|
<div style={{ marginTop: 12, display: "flex", flexDirection: "column", gap: 8 }}>
|
|
{rows.length === 0 && (
|
|
<span style={{ opacity: 0.7, fontSize: 13 }}>
|
|
No periods yet. The first one appears when a Core run is recorded or the period tick runs.
|
|
</span>
|
|
)}
|
|
{rows.slice(0, 6).map((row) => (
|
|
<div
|
|
key={row.id}
|
|
style={{ display: "flex", alignItems: "center", gap: 12, flexWrap: "wrap" }}
|
|
>
|
|
<span style={{ minWidth: 220 }}>{row.label}</span>
|
|
<span style={{ opacity: 0.7, fontSize: 12 }}>
|
|
{dateOnly(row.startDate)} to {dateOnly(row.endDate)} UTC
|
|
</span>
|
|
<span
|
|
style={{
|
|
fontSize: 11,
|
|
textTransform: "uppercase",
|
|
letterSpacing: 0.5,
|
|
opacity: 0.8,
|
|
}}
|
|
>
|
|
{row.status}
|
|
</span>
|
|
<span style={{ marginLeft: "auto" }}>
|
|
{row.status === "open" ? (
|
|
<Button
|
|
type="button"
|
|
buttonStyle="secondary"
|
|
size="small"
|
|
disabled={busy !== null}
|
|
onClick={() => void act("close", row.id)}
|
|
>
|
|
{busy === row.id ? "Closing..." : "Close now"}
|
|
</Button>
|
|
) : (
|
|
<Button
|
|
type="button"
|
|
buttonStyle="subtle"
|
|
size="small"
|
|
disabled={busy !== null}
|
|
onClick={() => void act("reopen", row.id)}
|
|
>
|
|
{busy === row.id ? "Reopening..." : "Reopen"}
|
|
</Button>
|
|
)}
|
|
</span>
|
|
</div>
|
|
))}
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|