- /login sends a Supabase magic link (admin emails only); /auth/confirm completes sign-in; proxy.ts guards /admin and every server action re-checks with requireAdmin(), since actions are plain POST endpoints. - /admin lists, creates, duplicates and deletes events. - /admin/events/[eventId] edits teams (colour, order) and games with criteria, presets and notes. All input goes through zod schemas that mirror the database constraints so errors read as plain sentences. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
138 lines
3.9 KiB
TypeScript
138 lines
3.9 KiB
TypeScript
"use client";
|
|
|
|
import { useState } from "react";
|
|
|
|
import { addTeam, deleteTeam, moveTeam, updateTeam } from "@/app/admin/actions";
|
|
import { Button } from "@/components/ui/button";
|
|
import { inputClass } from "@/components/ui/field";
|
|
import type { Tables } from "@/lib/database.types";
|
|
|
|
import { ErrorText } from "./error-text";
|
|
import { useAction } from "./use-action";
|
|
|
|
type Team = Tables<"teams">;
|
|
|
|
export function TeamsEditor({ eventId, teams }: { eventId: string; teams: Team[] }) {
|
|
const [newName, setNewName] = useState("");
|
|
const { pending, error, run } = useAction();
|
|
|
|
return (
|
|
<div className="mt-3 flex flex-col gap-3">
|
|
{teams.length === 0 && <p>No teams yet. Add the first one below.</p>}
|
|
<ul className="flex flex-col gap-2">
|
|
{teams.map((team, index) => (
|
|
<TeamRow
|
|
// Re-mount when saved values change so local edits reset to the server's copy.
|
|
key={`${team.id}:${team.name}:${team.color}`}
|
|
eventId={eventId}
|
|
team={team}
|
|
isFirst={index === 0}
|
|
isLast={index === teams.length - 1}
|
|
/>
|
|
))}
|
|
</ul>
|
|
|
|
<form
|
|
className="flex flex-wrap items-center gap-2"
|
|
onSubmit={(event) => {
|
|
event.preventDefault();
|
|
run(() => addTeam(eventId, newName), () => setNewName(""));
|
|
}}
|
|
>
|
|
<input
|
|
aria-label="New team name"
|
|
value={newName}
|
|
onChange={(event) => setNewName(event.target.value)}
|
|
required
|
|
maxLength={60}
|
|
placeholder="Team name"
|
|
className={`${inputClass} max-w-xs`}
|
|
/>
|
|
<Button type="submit" disabled={pending}>
|
|
Add team
|
|
</Button>
|
|
</form>
|
|
<ErrorText>{error}</ErrorText>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
function TeamRow({
|
|
eventId,
|
|
team,
|
|
isFirst,
|
|
isLast,
|
|
}: {
|
|
eventId: string;
|
|
team: Team;
|
|
isFirst: boolean;
|
|
isLast: boolean;
|
|
}) {
|
|
const [name, setName] = useState(team.name);
|
|
const [color, setColor] = useState(team.color);
|
|
const { pending, error, run } = useAction();
|
|
|
|
// Saves on blur rather than per keystroke: one request per edit.
|
|
const save = () => {
|
|
if (name === team.name && color === team.color) return;
|
|
run(() => updateTeam(eventId, team.id, { name, color }));
|
|
};
|
|
|
|
return (
|
|
<li className="flex flex-col gap-1">
|
|
<div className="flex flex-wrap items-center gap-2">
|
|
<input
|
|
type="color"
|
|
aria-label={`Colour for ${team.name}`}
|
|
value={color}
|
|
onChange={(event) => setColor(event.target.value)}
|
|
onBlur={save}
|
|
className="h-11 w-14 cursor-pointer rounded-md border border-ink/25 bg-white p-1"
|
|
/>
|
|
<input
|
|
aria-label="Team name"
|
|
value={name}
|
|
onChange={(event) => setName(event.target.value)}
|
|
onBlur={save}
|
|
onKeyDown={(event) => {
|
|
if (event.key === "Enter") event.currentTarget.blur();
|
|
}}
|
|
maxLength={60}
|
|
className={`${inputClass} min-w-40 flex-1`}
|
|
/>
|
|
<Button
|
|
variant="secondary"
|
|
size="sm"
|
|
aria-label={`Move ${team.name} up`}
|
|
disabled={pending || isFirst}
|
|
onClick={() => run(() => moveTeam(eventId, team.id, "up"))}
|
|
>
|
|
↑
|
|
</Button>
|
|
<Button
|
|
variant="secondary"
|
|
size="sm"
|
|
aria-label={`Move ${team.name} down`}
|
|
disabled={pending || isLast}
|
|
onClick={() => run(() => moveTeam(eventId, team.id, "down"))}
|
|
>
|
|
↓
|
|
</Button>
|
|
<Button
|
|
variant="danger"
|
|
size="sm"
|
|
disabled={pending}
|
|
onClick={() => {
|
|
if (window.confirm(`Delete team "${team.name}" and its scores?`)) {
|
|
run(() => deleteTeam(eventId, team.id));
|
|
}
|
|
}}
|
|
>
|
|
Delete
|
|
</Button>
|
|
</div>
|
|
<ErrorText>{error}</ErrorText>
|
|
</li>
|
|
);
|
|
}
|