"use client"; import { useEffect, useState } from "react"; import { useSession } from "next-auth/react"; import { useRouter } from "next/navigation"; import Navigation from "@/components/Navigation"; import ImageSelector from "@/components/ImageSelector"; import UserManagement from "@/components/UserManagement"; import EventManagement from "@/components/EventManagement"; interface SitePreferences { id: string; homeBackground: string | null; eventsBackground: string | null; leaderboardBackground: string | null; } type AdminSection = "preferences" | "users" | "events"; export default function AdminPage() { const { data: session, status } = useSession(); const router = useRouter(); const [activeSection, setActiveSection] = useState("preferences"); const [preferences, setPreferences] = useState(null); const [loading, setLoading] = useState(true); const [isEditing, setIsEditing] = useState(false); const [formData, setFormData] = useState({ homeBackground: "", eventsBackground: "", leaderboardBackground: "", }); useEffect(() => { if (status === "unauthenticated") { router.push("/login"); return; } if (status === "authenticated" && session?.user?.role !== "ADMIN") { router.push("/"); return; } if (status === "authenticated" && session?.user?.role === "ADMIN") { fetchPreferences(); } }, [status, session, router]); const fetchPreferences = async () => { try { const response = await fetch("/api/admin/preferences"); if (response.ok) { const data = await response.json(); setPreferences(data); setFormData({ homeBackground: data.homeBackground || "", eventsBackground: data.eventsBackground || "", leaderboardBackground: data.leaderboardBackground || "", }); } } catch (error) { console.error("Error fetching preferences:", error); } finally { setLoading(false); } }; const handleEdit = () => { setIsEditing(true); }; const handleSave = async () => { try { const response = await fetch("/api/admin/preferences", { method: "PUT", headers: { "Content-Type": "application/json", }, body: JSON.stringify(formData), }); if (response.ok) { await fetchPreferences(); setIsEditing(false); } } catch (error) { console.error("Error updating preferences:", error); } }; const handleCancel = () => { setIsEditing(false); if (preferences) { setFormData({ homeBackground: preferences.homeBackground || "", eventsBackground: preferences.eventsBackground || "", leaderboardBackground: preferences.leaderboardBackground || "", }); } }; if (status === "loading" || loading) { return (
Chargement...
); } return (

ADMIN

{/* Navigation Tabs */}
{activeSection === "preferences" && (

Préférences UI Globales

Images de fond du site

Ces préférences s'appliquent à tous les utilisateurs

{!isEditing && ( )}
{isEditing ? (
setFormData({ ...formData, homeBackground: url, }) } label="Background Home" /> setFormData({ ...formData, eventsBackground: url, }) } label="Background Events" /> setFormData({ ...formData, leaderboardBackground: url, }) } label="Background Leaderboard" />
) : (
Home: {preferences?.homeBackground || "Par défaut"}
Events: {preferences?.eventsBackground || "Par défaut"}
Leaderboard:{" "} {preferences?.leaderboardBackground || "Par défaut"}
)}
)} {activeSection === "users" && (

Gestion des Utilisateurs

)} {activeSection === "events" && (

Gestion des Événements

)}
); }