import { Form, Link } from "react-router"; import type { Route } from "./+types/admin.sports-seasons.$id.participants"; import { findSportsSeasonById } from "~/models/sports-season"; import { findParticipantsBySportsSeasonId, createParticipant, deleteParticipant } from "~/models/participant"; import { Button } from "~/components/ui/button"; import { Input } from "~/components/ui/input"; import { Label } from "~/components/ui/label"; import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from "~/components/ui/card"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from "~/components/ui/table"; import { Plus, Trash2, ArrowLeft } from "lucide-react"; export async function loader({ params }: Route.LoaderArgs) { const sportsSeason = await findSportsSeasonById(params.id); if (!sportsSeason) { throw new Response("Sports season not found", { status: 404 }); } const participants = await findParticipantsBySportsSeasonId(params.id); // Type assertion since we know the sport relation is included return { sportsSeason: sportsSeason as typeof sportsSeason & { sport: { id: string; name: string; type: string; slug: string } }, participants }; } export async function action({ request, params }: Route.ActionArgs) { const formData = await request.formData(); const intent = formData.get("intent"); if (intent === "delete") { const participantId = formData.get("participantId"); if (typeof participantId === "string") { await deleteParticipant(participantId); } return { success: true }; } // Add participant const name = formData.get("name"); const shortName = formData.get("shortName"); if (typeof name !== "string" || !name.trim()) { return { error: "Participant name is required" }; } try { await createParticipant({ sportsSeasonId: params.id, name: name.trim(), shortName: typeof shortName === "string" && shortName.trim() ? shortName.trim() : null, externalId: null, }); return { success: true }; } catch (error) { console.error("Error creating participant:", error); return { error: "Failed to add participant. Please try again." }; } } export default function ManageParticipants({ loaderData, actionData }: Route.ComponentProps) { const { sportsSeason, participants } = loaderData; return (

Manage Participants

{sportsSeason.sport.name} - {sportsSeason.name}

Add Participant Add a {sportsSeason.sport.type === "team" ? "team" : "player"} to this season
{actionData?.error && (
{actionData.error}
)} {actionData?.success && (
Participant added successfully!
)}
All Participants {participants.length} {participants.length === 1 ? "participant" : "participants"} total {participants.length === 0 ? (

No participants yet. Add your first {sportsSeason.sport.type === "team" ? "team" : "player"}.

) : (
Name Short Name {participants.map((participant) => ( {participant.name} {participant.shortName || "-"}
))}
)}
); }