Files
nahodka-front/src/hooks/useDeletePost.js
2026-07-03 00:04:56 +03:00

41 lines
1.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { useState, useCallback } from "react";
import { API_URL } from "../config/authConstants";
/**
* useDeletePost — хук для удаления объявления.
*
* @returns {Object} { deletePost, loading, error }
*/
export function useDeletePost() {
const [loading, setLoading] = useState(false);
const [error, setError] = useState("");
const deletePost = useCallback(async (postId) => {
setLoading(true);
setError("");
try {
const token = localStorage.getItem("token");
const res = await fetch(`${API_URL}/api/v1/posts/${postId}`, {
method: "DELETE",
headers: {
...(token ? { Authorization: `Bearer ${token}` } : {}),
},
});
if (!res.ok) {
throw new Error(`Ошибка ${res.status}`);
}
return true;
} catch (err) {
setError(err.message || "Не удалось удалить объявление");
return false;
} finally {
setLoading(false);
}
}, []);
return { deletePost, loading, error };
}