maj de la gestion des articles add mofif et suppr
This commit is contained in:
@@ -8,6 +8,7 @@ import {
|
||||
import { Add, ArrowBack, Edit, Delete } from "@mui/icons-material";
|
||||
import api from "../../api";
|
||||
import { getToken } from "../../auth";
|
||||
import { deletePost } from "../../wordpress";
|
||||
|
||||
const GestionArticles = () => {
|
||||
const [posts, setPosts] = useState([]);
|
||||
@@ -54,41 +55,17 @@ const GestionArticles = () => {
|
||||
|
||||
// ✅ Supprimer un article et son image associée
|
||||
const handleDeletePost = async (postId, imageId) => {
|
||||
if (!window.confirm("⚠ Es-tu sûr de vouloir supprimer cet article ?")) return;
|
||||
|
||||
try {
|
||||
const token = getToken(); // 🔑 Récupérer le token JWT
|
||||
if (!token) {
|
||||
alert("❌ Erreur : utilisateur non authentifié !");
|
||||
navigate("/admin/login");
|
||||
return;
|
||||
}
|
||||
|
||||
const headers = {
|
||||
Authorization: `Bearer ${token}`, // ✅ Ajout du token dans l'en-tête
|
||||
"Content-Type": "application/json",
|
||||
};
|
||||
|
||||
console.log(`📢 Suppression de l'article ID: ${postId}`);
|
||||
|
||||
// ✅ Suppression de l'article (avec force=true)
|
||||
await api.delete(`wp/v2/posts/${postId}?force=true`, { headers });
|
||||
|
||||
// ✅ Suppression de l'image associée si elle existe
|
||||
if (imageId) {
|
||||
console.log(`📢 Suppression de l'image ID: ${imageId}`);
|
||||
await api.delete(`wp/v2/media/${imageId}?force=true`, { headers });
|
||||
}
|
||||
|
||||
// ✅ Mise à jour de la liste après suppression
|
||||
setPosts(posts.filter((post) => post.id !== postId));
|
||||
|
||||
alert("✅ Article supprimé avec succès !");
|
||||
} catch (error) {
|
||||
console.error("❌ Erreur suppression article :", error);
|
||||
alert(`⚠ Erreur : impossible de supprimer l'article.\n\nDétails: ${error.response?.data?.message || error.message}`);
|
||||
if (window.confirm("Es-tu sûr de vouloir supprimer cet article ?")) {
|
||||
try {
|
||||
await deletePost(postId, imageId); // ✅ Appel de la fonction deletePost
|
||||
setPosts(posts.filter((post) => post.id !== postId)); // ✅ Met à jour la liste après suppression
|
||||
alert("✅ Article supprimé avec succès !");
|
||||
} catch (error) {
|
||||
console.error("❌ Erreur suppression article :", error);
|
||||
alert("⚠ Erreur : impossible de supprimer l'article.");
|
||||
}
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
// ✅ Filtrage des articles
|
||||
const filteredPosts = posts.filter((post) =>
|
||||
@@ -152,7 +129,12 @@ const GestionArticles = () => {
|
||||
</TableCell>
|
||||
{/* ✅ Boutons Modifier et Supprimer */}
|
||||
<TableCell sx={{ textAlign: "center" }}>
|
||||
<Button startIcon={<Edit />} variant="outlined" color="warning" sx={{ mr: 1 }} >
|
||||
<Button startIcon={<Edit />}
|
||||
variant="outlined"
|
||||
color="warning"
|
||||
sx={{ mr: 1 }}
|
||||
onClick={() => navigate(`/admin/edit-post/${post.id}`)}
|
||||
>
|
||||
Modifier
|
||||
</Button>
|
||||
<Button
|
||||
|
||||
Reference in New Issue
Block a user