Browse Source

fix: show error if chat don't loaded

stage
Vitalik 2 months ago
parent
commit
8157221154
  1. 2
      src/containers/Chats/components/chats-list.component.tsx
  2. 40
      src/containers/Chats/hooks/use-chat-details.hook.ts
  3. 7
      src/services/domain/chats.service.ts

2
src/containers/Chats/components/chats-list.component.tsx

@ -76,7 +76,7 @@ export const ChatsList: FC<IProps> = (props) => { @@ -76,7 +76,7 @@ export const ChatsList: FC<IProps> = (props) => {
chatId: item.id,
unreadCount: item.unreadMessagesCount,
});
history.push(`/chats?id=${item.id}`);
history.push(`/chats?id=${item}`);
};
const renderEndMessage = useCallback(() => {

40
src/containers/Chats/hooks/use-chat-details.hook.ts

@ -3,7 +3,7 @@ import { @@ -3,7 +3,7 @@ import {
ChatType,
IChatDetails,
useEventsListener,
useSocketListener
useSocketListener,
} from "@/shared";
import { getProfile } from "@/store/account";
import { UnselectChat } from "@/store/chats";
@ -15,12 +15,13 @@ import { useHistory } from "react-router-dom"; @@ -15,12 +15,13 @@ import { useHistory } from "react-router-dom";
import { getHeaderGroupChatInfo, getHeaderPersonalChatInfo } from "../helpers";
import { IHeaderChatInfo } from "../interfaces";
import { transformChatDetailToHeaderChatInfo } from "../transforms";
import { message } from "antd";
const headerChatInfoInitialState: IHeaderChatInfo = {
name: " ",
previewUrl: null,
type: ChatType.Group,
label: " "
label: " ",
};
export const useChatDetails = (chatId: number) => {
@ -38,12 +39,18 @@ export const useChatDetails = (chatId: number) => { @@ -38,12 +39,18 @@ export const useChatDetails = (chatId: number) => {
const fetchDetails = async () => {
setLoading(true);
const chatDetails = await chatsService.fetchChatDetails({ id: chatId });
setHeaderInfo(chatDetails);
setChatDetails(chatDetails);
setLoading(false);
try {
const chatDetails = await chatsService.fetchChatDetails({ id: chatId });
setHeaderInfo(chatDetails);
setChatDetails(chatDetails);
} catch (e) {
message.error("Сталась помилка, такого чату не існує");
console.log("e", e);
setChatDetails(null);
} finally {
setLoading(false);
}
};
const setHeaderInfo = (chatDetails: IChatDetails) => {
@ -57,18 +64,18 @@ export const useChatDetails = (chatId: number) => { @@ -57,18 +64,18 @@ export const useChatDetails = (chatId: number) => {
);
const headerLabel = {
[ChatType.Group]: getHeaderGroupChatInfo(
_.filter(chatDetails.chatMembers, member => !member.isDeleted).length,
_.filter(chatDetails.chatMembers, (member) => !member.isDeleted).length,
onlineUsersCount
),
[ChatType.Personal]: getHeaderPersonalChatInfo(
chatDetails.chatMembers,
accountId.id
)
),
};
setHeaderChatInfo({
...headerChatInfo,
label: headerLabel[chatDetails.type]
label: headerLabel[chatDetails.type],
});
};
@ -134,7 +141,6 @@ export const useChatDetails = (chatId: number) => { @@ -134,7 +141,6 @@ export const useChatDetails = (chatId: number) => {
};
const onChangeChatRole = (data: { chatId: number }) => {
console.log("changed role");
updateChatData(data?.chatId);
};
@ -142,11 +148,11 @@ export const useChatDetails = (chatId: number) => { @@ -142,11 +148,11 @@ export const useChatDetails = (chatId: number) => {
if (!chatDetails || _.isEmpty(chatDetails.pinedMessages)) return;
const pinnedUpdated = _.find(
chatDetails.pinedMessages,
it => it.id === message.id
(it) => it.id === message.id
);
if (!pinnedUpdated) return;
const newPinned = chatDetails.pinedMessages.map(it => {
const newPinned = chatDetails.pinedMessages.map((it) => {
if (it.id === message.id) {
return message;
}
@ -155,12 +161,12 @@ export const useChatDetails = (chatId: number) => { @@ -155,12 +161,12 @@ export const useChatDetails = (chatId: number) => {
setChatDetails({
...chatDetails,
pinedMessages: newPinned
pinedMessages: newPinned,
});
};
useSocketListener("chat/update-message", onUpdateMessage, [
chatDetails?.pinedMessages
chatDetails?.pinedMessages,
]);
// APP EVENTS LISTENERS //
@ -181,6 +187,6 @@ export const useChatDetails = (chatId: number) => { @@ -181,6 +187,6 @@ export const useChatDetails = (chatId: number) => {
return {
chatDetails,
headerChatInfo,
isLoading
isLoading,
};
};

7
src/services/domain/chats.service.ts

@ -5,11 +5,11 @@ import { @@ -5,11 +5,11 @@ import {
IEditGroupChat,
IFetchChatDetail,
IFetchChatsParams,
ISetChatMuted
ISetChatMuted,
} from "@/api/chats/requests.interfaces";
import {
IFetchChatList,
NewChatGroupId
NewChatGroupId,
} from "@/api/chats/responses.interfaces";
import { IRequestConfig } from "@/api/http.service";
import { ApiResponse } from "@/api/http.types";
@ -45,6 +45,7 @@ const fetchChatDetails = async ( @@ -45,6 +45,7 @@ const fetchChatDetails = async (
return chatDetails;
} catch (error) {
console.log("ERROR | FETCH DETAILED CHAT: ", error);
throw error;
}
};
@ -114,5 +115,5 @@ export const chatsService = { @@ -114,5 +115,5 @@ export const chatsService = {
editGroupChatName,
editGroupChatImg,
createPersonalChat,
createGroupChat
createGroupChat,
};

Loading…
Cancel
Save