"use client"; import { useEffect, useState } from "react"; import { useSession } from "next-auth/react"; import { useRouter } from "next/navigation"; import Navigation from "@/components/Navigation"; interface UserPreferences { id: string; userId: string; homeBackground: string | null; eventsBackground: string | null; leaderboardBackground: string | null; user: { id: string; username: string; email: string; }; } export default function AdminPage() { const { data: session, status } = useSession(); const router = useRouter(); const [preferences, setPreferences] = useState([]); const [loading, setLoading] = useState(true); const [editingUserId, setEditingUserId] = useState(null); 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); } } catch (error) { console.error("Error fetching preferences:", error); } finally { setLoading(false); } }; const handleEdit = (pref: UserPreferences) => { setEditingUserId(pref.userId); setFormData({ homeBackground: pref.homeBackground || "", eventsBackground: pref.eventsBackground || "", leaderboardBackground: pref.leaderboardBackground || "", }); }; const handleSave = async () => { if (!editingUserId) return; try { const response = await fetch("/api/admin/preferences", { method: "PUT", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ userId: editingUserId, ...formData, }), }); if (response.ok) { await fetchPreferences(); setEditingUserId(null); setFormData({ homeBackground: "", eventsBackground: "", leaderboardBackground: "", }); } } catch (error) { console.error("Error updating preferences:", error); } }; const handleCancel = () => { setEditingUserId(null); setFormData({ homeBackground: "", eventsBackground: "", leaderboardBackground: "", }); }; if (status === "loading" || loading) { return (
Chargement...
); } return (

ADMIN - GESTION DES PRÉFÉRENCES UI

{preferences.map((pref) => (

{pref.user.username}

{pref.user.email}

{editingUserId !== pref.userId && ( )}
{editingUserId === pref.userId ? (
setFormData({ ...formData, homeBackground: e.target.value, }) } placeholder="/got-2.jpg" className="w-full px-3 py-2 bg-black/60 border border-pixel-gold/30 rounded text-white text-sm" />
setFormData({ ...formData, eventsBackground: e.target.value, }) } placeholder="/got-2.jpg" className="w-full px-3 py-2 bg-black/60 border border-pixel-gold/30 rounded text-white text-sm" />
setFormData({ ...formData, leaderboardBackground: e.target.value, }) } placeholder="/leaderboard-bg.jpg" className="w-full px-3 py-2 bg-black/60 border border-pixel-gold/30 rounded text-white text-sm" />
) : (
Home: {pref.homeBackground || "Par défaut"}
Events: {pref.eventsBackground || "Par défaut"}
Leaderboard:{" "} {pref.leaderboardBackground || "Par défaut"}
)}
))} {preferences.length === 0 && (
Aucune préférence trouvée
)}
); }