"use client"; import { useState } from "react"; import { useRouter } from "next/navigation"; const VISIBILITY_OPTIONS = [ { value: "MEMBERS_ONLY", label: "👥 Tous les adhérents" }, { value: "MUAY_THAI_ONLY", label: "🥊 Muay Thai uniquement" }, { value: "YOGA_ONLY", label: "🧘 Yoga uniquement" }, { value: "PUBLIC", label: "🌍 Public" }, ]; const TYPE_OPTIONS = [ { value: "PHOTO", label: "📷 Photo" }, { value: "VIDEO", label: "🎥 Vidéo" }, ]; export default function AdminMedia({ media }: { media: any[] }) { const router = useRouter(); const [title, setTitle] = useState(""); const [description, setDescription] = useState(""); const [type, setType] = useState("PHOTO"); const [visibility, setVisibility] = useState("MEMBERS_ONLY"); const [file, setFile] = useState(null); const [uploading, setUploading] = useState(false); const [error, setError] = useState(""); async function handleSubmit(e: React.FormEvent) { e.preventDefault(); if (!file) return; setUploading(true); setError(""); try { const dir = type === "VIDEO" ? "media/videos" : "media/photos"; const formData = new FormData(); formData.append("file", file); formData.append("subdir", dir); const up = await fetch("/api/upload", { method: "POST", body: formData }); if (!up.ok) throw new Error("Upload échoué"); const { url } = await up.json(); const res = await fetch("/api/media", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ title, description, url, type, visibility }), }); if (!res.ok) throw new Error("Enregistrement échoué"); setTitle(""); setDescription(""); setFile(null); setType("PHOTO"); router.refresh(); } catch (err: any) { setError(err.message || "Une erreur est survenue"); } finally { setUploading(false); } } async function deleteMedia(id: string) { if (!confirm("Supprimer ce média ?")) return; const res = await fetch("/api/media", { method: "DELETE", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ id }), }); if (res.ok) router.refresh(); } return (

🎬 Photos & vidéos

📤 Ajouter un média

{TYPE_OPTIONS.map((t) => ( ))}
{VISIBILITY_OPTIONS.map((v) => ( ))}
setTitle(e.target.value)} placeholder="Ex: Entraînement du mardi" required />
setDescription(e.target.value)} />
{type === "VIDEO" ? "🎥" : "📷"} {file ? (

{file.name}

) : (

{type === "VIDEO" ? "Choisir une vidéo" : "Choisir une photo"}

)}
{error &&

{error}

}
{media.length === 0 && (

Aucun média.

)} {media.map((m) => (
{m.type === "PHOTO" ? ( {m.title} ) : (
))}
); }