From 486be6577affdc4b4ce876c3d5eb62618edcac31 Mon Sep 17 00:00:00 2001 From: SlimusMinus Date: Tue, 23 Jun 2026 00:39:03 +0300 Subject: [PATCH] added delete posts --- src/components/MainFeed.jsx | 15 +++++++++++++++ src/components/PostOnFeed.jsx | 11 +++++++++-- src/styles/card.css | 16 ++++++++++++++++ 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src/components/MainFeed.jsx b/src/components/MainFeed.jsx index 35b6756..cd58aaa 100644 --- a/src/components/MainFeed.jsx +++ b/src/components/MainFeed.jsx @@ -11,6 +11,7 @@ import { ProfileModal } from "./profile"; import AuthModal from "./AuthModal"; import RulesModal from "./RulesModal"; import { useAuthContext } from "../context/AuthContext"; +import { useDeletePost } from "../hooks/useDeletePost"; /** * MainFeed — корневой компонент приложения. @@ -20,6 +21,7 @@ import { useAuthContext } from "../context/AuthContext"; export default function MainFeed() { const navigate = useNavigate(); const { user, token, isAuthenticated, logout } = useAuthContext(); + const { deletePost } = useDeletePost(); // Открыт ли левый сайдбар (мобильный слайд-аут) const [sidebarOpen, setSidebarOpen] = useState(false); @@ -139,6 +141,18 @@ export default function MainFeed() { }); }, []); + /** Удаляет объявление и перезагружает ленту */ + const handleDelete = useCallback(async (postId) => { + if (!window.confirm("Вы уверены, что хотите удалить это объявление?")) { + return; + } + const success = await deletePost(postId); + if (success) { + loadFeed(); + loadMyPosts(); + } + }, [deletePost, loadFeed, loadMyPosts]); + // После создания или редактирования объявления перезагружаем ленту, // чтобы получить актуальные данные (включая изображения) из БД const handleNewAd = useCallback(() => { loadFeed(); loadMyPosts(); }, [loadFeed, loadMyPosts]); @@ -249,6 +263,7 @@ export default function MainFeed() { liked={liked.has(ad.id)} onLike={toggleLike} onEdit={activeNav === "Мои объявления" ? handleEdit : undefined} + onDelete={activeNav === "Мои объявления" ? handleDelete : undefined} isOwner={activeNav === "Мои объявления"} /> )); diff --git a/src/components/PostOnFeed.jsx b/src/components/PostOnFeed.jsx index 074c69d..aaa49f7 100644 --- a/src/components/PostOnFeed.jsx +++ b/src/components/PostOnFeed.jsx @@ -3,12 +3,13 @@ import CalendarIcon from "./icons/CalendarIcon"; import PhoneIcon from "./icons/PhoneIcon"; import MapIcon from "./icons/MapIcon"; import HeartIcon from "./icons/HeartIcon"; +import TrashIcon from "./icons/TrashIcon"; import { useNavigate } from "react-router-dom"; import { useChats } from "../hooks/useChats"; import { useAuthContext } from "../context/AuthContext"; import { API_URL } from "../config/authConstants"; -export default function PostOnFeed({ ad, liked, onLike, onEdit, isOwner }) { +export default function PostOnFeed({ ad, liked, onLike, onEdit, onDelete, isOwner }) { const navigate = useNavigate(); const { createOrGetChat } = useChats(); const { isAuthenticated } = useAuthContext(); @@ -75,11 +76,17 @@ export default function PostOnFeed({ ad, liked, onLike, onEdit, isOwner }) { )} {onEdit && isOwner && ( - )} + {onDelete && isOwner && ( + + )}