"use client" import { useState, useEffect } from "react" import Navigation from "@/components/Navigation" import Link from "next/link" interface Match { id: number playedAt: string | null team1Score: number team2Score: number status: string isCasual: boolean event?: { id: number name: string } | null player1P1: { id: number; name: string } player1P2: { id: number; name: string } player2P1: { id: number; name: string } player2P2: { id: number; name: string } } export default function AdminMatchesPage() { const [matches, setMatches] = useState([]) const [error, setError] = useState("") const [loading, setLoading] = useState(true) const [deletingId, setDeletingId] = useState(null) useEffect(() => { fetchMatches() }, []) const fetchMatches = async () => { try { const response = await fetch("/api/matches") if (!response.ok) { const data = await response.json() throw new Error(data.error || "Failed to fetch matches") } const data = await response.json() setMatches(data) } catch (err: unknown) { setError(err instanceof Error ? err.message : "Unknown error") } finally { setLoading(false) } } const handleDelete = async (matchId: number) => { if (!confirm("Are you sure you want to delete this match? This action cannot be undone.")) { return } setDeletingId(matchId) try { const response = await fetch(`/api/matches/${matchId}`, { method: "DELETE", }) if (!response.ok) { try { const errorData = await response.json() alert(`Error: ${errorData.error || 'Failed to delete match'}`) } catch { alert(`Error: ${response.status} ${response.statusText}`) } return } const data = await response.json() if (data.success) { setMatches(matches.filter(m => m.id !== matchId)) } else { alert(`Error: ${data.error}`) } } catch (err: unknown) { alert(`Error: ${err instanceof Error ? err.message : "Unknown error occurred"}`) } finally { setDeletingId(null) } } if (loading) { return (

Loading matches...

) } return (
{/* Page Header */}

Match Management

Manage {matches.length} match{matches.length !== 1 ? 'es' : ''} in the system

{error && (
{error}
)} {/* Match Table */}
{matches.map((match) => ( ))}
Date Tournament Team 1 Score Team 2 Score Actions
{match.playedAt ? new Date(match.playedAt).toLocaleDateString() : "N/A"} {match.event ? ( {match.event.name} ) : ( Casual )} {match.player1P1?.name} & {match.player1P2?.name} {match.team1Score} {match.player2P1?.name} & {match.player2P2?.name} {match.team2Score}
View
) }