"use client"; import { useState, useEffect } from "react"; import { useSession } from "next-auth/react"; import { useRouter } from "next/navigation"; export default function AdminCluesPage() { const { data: session, status } = useSession(); const [clues, setClues] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(""); const router = useRouter(); // Form state for adding new clues - removed coordinates const [newClue, setNewClue] = useState({ title: "", description: "", location: "", }); // Check if user is admin useEffect(() => { if (status === "authenticated") { if (session?.user?.role !== "admin") { router.push("/team-selection"); } else { fetchClues(); } } else if (status === "unauthenticated") { router.push("/login"); } }, [session, status, router]); // Fetch all clues const fetchClues = async () => { try { setLoading(true); const response = await fetch("/api/admin/clues", { method: "GET", headers: { "Content-Type": "application/json", "Cache-Control": "no-cache, no-store, must-revalidate", }, credentials: "include", }); if (!response.ok) { const errorData = await response.json().catch(() => ({})); throw new Error( errorData.error || `Failed to fetch clues (${response.status})` ); } const data = await response.json(); // Validate the response data if (!Array.isArray(data)) { throw new Error("Invalid response format from server"); } setClues(data); setError(""); } catch (err) { console.error("Error fetching clues:", err); setError(err.message); } finally { setLoading(false); } }; // Handle input change for new clue form const handleInputChange = (e) => { const { name, value } = e.target; setNewClue((prev) => ({ ...prev, [name]: value, })); }; // Handle form submission to add new clue const handleAddClue = async (e) => { e.preventDefault(); try { setLoading(true); const response = await fetch("/api/admin/clues", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify(newClue), credentials: "include", }); if (!response.ok) { throw new Error("Failed to add clue"); } // Refresh the clue list fetchClues(); // Reset the form - removed coordinates setNewClue({ title: "", description: "", location: "", }); setError(""); } catch (err) { console.error("Error adding clue:", err); setError(err.message); } finally { setLoading(false); } }; // Handle deleting a clue const handleDeleteClue = async (clueId) => { if (!confirm("Are you sure you want to delete this clue?")) return; try { setLoading(true); const response = await fetch(`/api/admin/clues/${clueId}`, { method: "DELETE", headers: { "Content-Type": "application/json", }, credentials: "include", }); if (!response.ok) { throw new Error("Failed to delete clue"); } // Refresh the clue list fetchClues(); setError(""); } catch (err) { console.error("Error deleting clue:", err); setError(err.message); } finally { setLoading(false); } }; if (status === "loading" || loading) { return
{error}
} {/* Add New Clue Form - removed coordinate fields */}No clues found.
) : ({clue.description}
{clue.location && (Location: {clue.location}
)}Position: Clue #{clue.id} in sequence