2023-10-02 15:42:23 +03:00
|
|
|
import { useEffect } from "react";
|
|
|
|
import { useTranslation } from "react-i18next";
|
2023-06-27 12:28:09 +03:00
|
|
|
|
2023-07-05 19:33:18 +03:00
|
|
|
import { useChatApi } from "@/lib/api/chat/useChatApi";
|
|
|
|
import { useChatsContext } from "@/lib/context/ChatsProvider/hooks/useChatsContext";
|
|
|
|
import { useToast } from "@/lib/hooks";
|
2023-06-27 12:28:09 +03:00
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
|
|
|
export const useChatsList = () => {
|
2023-10-02 15:42:23 +03:00
|
|
|
const { t } = useTranslation(["chat"]);
|
2023-06-27 12:28:09 +03:00
|
|
|
|
2023-07-05 19:33:18 +03:00
|
|
|
const { setAllChats } = useChatsContext();
|
|
|
|
const { publish } = useToast();
|
|
|
|
const { getChats } = useChatApi();
|
|
|
|
|
2023-06-27 12:28:09 +03:00
|
|
|
useEffect(() => {
|
2023-07-05 19:33:18 +03:00
|
|
|
const fetchAllChats = async () => {
|
|
|
|
try {
|
|
|
|
const response = await getChats();
|
|
|
|
setAllChats(response.reverse());
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
publish({
|
|
|
|
variant: "danger",
|
2023-10-02 15:42:23 +03:00
|
|
|
text: t("errorFetching", { ns: "chat" }),
|
2023-07-05 19:33:18 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
void fetchAllChats();
|
|
|
|
}, []);
|
2023-06-27 12:28:09 +03:00
|
|
|
};
|