Files
nahodka-front/src/hooks/useFeedActions.jsx
SlimusMinus 7f59ee1f38 fixed code
2026-08-11 00:24:07 +03:00

81 lines
3.1 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.

// src/hooks/useFeedActions.js
import { useState, useCallback } from "react";
import { useDeletePost } from "./useDeletePost";
import { useFavorites } from "./useFavorites";
import { usePostStatus } from "./usePostStatus";
/**
* Объединяет действия над объявлениями (лайк, удаление, отметка "найдено")
* и общий паттерн "выполнить действие -> перезагрузить ленту и мои объявления".
*
* Авторизация (Authorization header + авто-обновление токена) обрабатывается
* внутри apiFetch — сам token сюда передавать не нужно, только isAuthenticated
* как ранняя проверка перед запросом.
*
* Каждое действие (deletePost/toggleFavorite/markAsFound) возвращает
* { success, error } напрямую из вызова — это исключает гонку между
* обновлением error state дочернего хука и его чтением здесь же в том же
* тике (React batching иначе мог бы подставить устаревшее значение).
*
* @param {Function} refresh — перезагрузить текущий список (лента/город)
* @param {Function} loadMyPosts — перезагрузить "мои объявления"
* @param {Function} onAuthRequired — вызывается когда нужна авторизация
* @param {boolean} isAuthenticated
*/
export function useFeedActions({ refresh, loadMyPosts, onAuthRequired, isAuthenticated, t }) {
const { deletePost } = useDeletePost();
const { toggleFavorite } = useFavorites();
const { markAsFound } = usePostStatus();
const [error, setError] = useState("");
const afterMutation = useCallback(() => {
refresh();
loadMyPosts();
}, [refresh, loadMyPosts]);
const toggleLike = useCallback(async (id) => {
if (!isAuthenticated) {
onAuthRequired();
return;
}
const result = await toggleFavorite(id);
if (result.success) {
afterMutation();
} else {
setError(result.error);
}
}, [isAuthenticated, onAuthRequired, toggleFavorite, afterMutation]);
const handleDelete = useCallback(async (postId) => {
if (!window.confirm(t('feed.confirmDelete'))) return;
const result = await deletePost(postId);
if (result.success) {
afterMutation();
} else {
setError(result.error);
}
}, [t, deletePost, afterMutation]);
const handleMarkFound = useCallback(async (postId) => {
if (!window.confirm(t('feed.confirmMarkFound'))) return;
const result = await markAsFound(postId);
if (result.success) {
afterMutation();
} else {
setError(result.error);
}
}, [t, markAsFound, afterMutation]);
const clearError = useCallback(() => setError(""), []);
return {
toggleLike,
handleDelete,
handleMarkFound,
handleNewAd: afterMutation,
handleEditSave: afterMutation,
error,
clearError,
};
}