"use client"; import { useState, useEffect } from "react"; import { useSession } from "next-auth/react"; import { useRouter } from "next/navigation"; export default function AdminLeaderboardPage() { const { data: session, status } = useSession(); const [teams, setTeams] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(""); const router = useRouter(); // Check if user is admin useEffect(() => { if (status === "authenticated") { if (session?.user?.role !== "admin") { router.push("/team-selection"); } else { fetchLeaderboardData(); } } else if (status === "unauthenticated") { router.push("/login"); } }, [session, status, router]); // Fetch leaderboard data const fetchLeaderboardData = async () => { try { setLoading(true); setError(""); // This is a placeholder - you'll need to implement the actual API // For now, we'll just set dummy data const dummyTeams = [ { id: 1, name: "Team Alpha", code: "ALPHA1", memberCount: 4, cluesFound: 5, totalClues: 10, lastActivity: new Date().toISOString(), }, { id: 2, name: "Team Beta", code: "BETA12", memberCount: 3, cluesFound: 8, totalClues: 10, lastActivity: new Date().toISOString(), }, { id: 3, name: "Team Gamma", code: "GAMMA3", memberCount: 5, cluesFound: 2, totalClues: 10, lastActivity: new Date().toISOString(), }, ]; setTeams(dummyTeams); // Uncomment this when API is ready /* const response = await fetch("/api/admin/leaderboard", { method: "GET", headers: { "Content-Type": "application/json", "Cache-Control": "no-cache, no-store, must-revalidate" }, credentials: "include" }); if (!response.ok) { throw new Error("Failed to fetch leaderboard data"); } const data = await response.json(); setTeams(data); */ } catch (err) { console.error("Error fetching leaderboard data:", err); setError(err.message); } finally { setLoading(false); } }; if (status === "loading" || loading) { return
{error}
}No team data available.
) : (| Rank | Team Name | Members | Progress | Last Activity | Actions |
|---|---|---|---|---|---|
| {index + 1} | {team.name} ({team.code}) | {team.memberCount} | {team.cluesFound} / {team.totalClues} | {team.lastActivity ? new Date(team.lastActivity).toLocaleString() : "Never"} |