added files in git
This commit is contained in:
32
src/hooks/useDeleteChat.js
Normal file
32
src/hooks/useDeleteChat.js
Normal file
@@ -0,0 +1,32 @@
|
||||
import { useState, useCallback } from "react";
|
||||
import { API_URL } from "../config/authConstants";
|
||||
|
||||
export function useDeleteChat() {
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [error, setError] = useState("");
|
||||
|
||||
const deleteChat = useCallback(async (chatId) => {
|
||||
setLoading(true);
|
||||
setError("");
|
||||
try {
|
||||
const token = localStorage.getItem("token");
|
||||
const res = await fetch(`${API_URL}/api/v1/chats/${chatId}`, {
|
||||
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 { deleteChat, loading, error };
|
||||
}
|
||||
Reference in New Issue
Block a user