fixed code

This commit is contained in:
SlimusMinus
2026-08-11 00:24:07 +03:00
parent c50b75eece
commit 7f59ee1f38
95 changed files with 3623 additions and 2830 deletions

View File

@@ -1,13 +1,19 @@
import { useState, useCallback } from "react";
import { useTranslation } from 'react-i18next';
import { API_URL } from "../config/authConstants";
import { apiFetch } from "../api/apiClient";
/**
* useDeletePost — хук для удаления объявления.
*
* deletePost возвращает { success, error } вместо голого boolean — так
* вызывающий код получает актуальный текст ошибки сразу, без риска прочитать
* устаревшее значение error state до его обновления React'ом.
*
* @returns {Object} { deletePost, loading, error }
*/
export function useDeletePost() {
const { t } = useTranslation();
const [loading, setLoading] = useState(false);
const [error, setError] = useState("");
@@ -21,17 +27,20 @@ export function useDeletePost() {
});
if (!res.ok) {
throw new Error(`Ошибка ${res.status}`);
let body;
try { body = await res.json(); } catch { body = null; }
throw new Error(body?.message || t('post.deleteErrorWithStatus', { status: res.status }));
}
return true;
return { success: true, error: null };
} catch (err) {
setError(err.message || "Не удалось удалить объявление");
return false;
const message = err.message || t('post.deleteError');
setError(message);
return { success: false, error: message };
} finally {
setLoading(false);
}
}, []);
}, [t]);
return { deletePost, loading, error };
}