fixed code
This commit is contained in:
@@ -1,10 +1,14 @@
|
||||
import { useState, useCallback } from "react";
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { API_URL } from "../config/authConstants";
|
||||
import { apiFetch } from "../api/apiClient";
|
||||
import { useUnread } from "../context/UnreadContext";
|
||||
|
||||
export function useDeleteChat() {
|
||||
const { t } = useTranslation();
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [error, setError] = useState("");
|
||||
const { clearChatUnread } = useUnread();
|
||||
|
||||
const deleteChat = useCallback(async (chatId) => {
|
||||
setLoading(true);
|
||||
@@ -13,17 +17,22 @@ export function useDeleteChat() {
|
||||
const res = await apiFetch(`${API_URL}/api/v1/chats/${chatId}`, {
|
||||
method: "DELETE",
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
throw new Error(`Ошибка ${res.status}`);
|
||||
let body;
|
||||
try { body = await res.json(); } catch { body = null; }
|
||||
throw new Error(body?.message || t('chat.deleteErrorWithStatus', { status: res.status }));
|
||||
}
|
||||
|
||||
clearChatUnread(chatId);
|
||||
return true;
|
||||
} catch (err) {
|
||||
setError(err.message || "Failed to delete chat");
|
||||
setError(err.message || t('chat.deleteError'));
|
||||
return false;
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
}, []);
|
||||
}, [t, clearChatUnread]);
|
||||
|
||||
return { deleteChat, loading, error };
|
||||
}
|
||||
Reference in New Issue
Block a user