diff --git a/frontend/src/components/EditPost.jsx b/frontend/src/components/EditPost.jsx index e7d4cf2..fe5a8e3 100644 --- a/frontend/src/components/EditPost.jsx +++ b/frontend/src/components/EditPost.jsx @@ -1,33 +1,135 @@ -import { useState } from "react"; -import { updatePost } from "../wordpress"; +import { useState, useEffect } from "react"; +import { useParams, useNavigate } from "react-router-dom"; +import { getPostById, updatePost, uploadImage } from "../wordpress"; +import { getToken } from "../auth"; +import { + Box, Container, Typography, TextField, Button, Paper, Input, IconButton +} from "@mui/material"; +import { ArrowBack, Publish } from "@mui/icons-material"; -function EditPost({ post }) { - const [title, setTitle] = useState(post.title.rendered); - const [content, setContent] = useState(post.content.rendered); +function EditPost() { + const { id } = useParams(); // ✅ Récupère l'ID de l'article depuis l'URL + const navigate = useNavigate(); + const [title, setTitle] = useState(""); + const [content, setContent] = useState(""); + const [file, setFile] = useState(null); + const [loading, setLoading] = useState(true); - const handleUpdate = async () => { - const success = await updatePost(post.id, title, content); - if (success) { - alert("Article mis à jour !"); - } else { - alert("Échec de la mise à jour."); + // ✅ Vérification de l'authentification + useEffect(() => { + if (!getToken()) { + navigate("/admin/login"); + } + }, [navigate]); + + // ✅ Récupérer les données de l'article + useEffect(() => { + const fetchPost = async () => { + try { + const post = await getPostById(id); + setTitle(post.title.rendered); + setContent(post.content.rendered.replace(/(<([^>]+)>)/gi, "")); // Enlever le HTML + setLoading(false); + } catch (error) { + console.error("Erreur chargement article :", error); + navigate("/admin/gestion-articles"); // Redirige si erreur + } + }; + + fetchPost(); + }, [id, navigate]); + + const handleUpdate = async (e) => { + e.preventDefault(); + try { + const updatedPost = await updatePost(id, title, content, file); + alert("✅ Article mis à jour avec succès !"); + navigate("/admin/gestion-articles"); // Redirige après modification + } catch (error) { + alert("❌ Erreur lors de la mise à jour de l'article."); } }; + if (loading) return Chargement...; + return ( -
-

Modifier l’article

- setTitle(e.target.value)} - /> -