Browse Source

FIX | chat settings: delete chat correctly (BANK-794)

merge-requests/194/head
Yevhen Romanenko 2 years ago
parent
commit
a2a4b47096
  1. 2
      src/containers/Chats/components/settings-chat-modal.component.tsx
  2. 19
      src/containers/Chats/hooks/use-chat-details.hook.ts
  3. 17
      src/containers/Chats/hooks/use-chat-messages.hook.ts
  4. 18
      src/containers/Chats/hooks/use-chats-list.hook.ts
  5. 1
      src/shared/hooks/index.ts
  6. 5
      src/shared/hooks/use-socket.hook.ts

2
src/containers/Chats/components/settings-chat-modal.component.tsx

@ -146,7 +146,7 @@ export const SettingsChatModal: FC<IProps> = ({ @@ -146,7 +146,7 @@ export const SettingsChatModal: FC<IProps> = ({
}, [previewImg]);
const closeModal = () => {
socketEvents.emit("chat/edit-chat", { chatId: chatId });
// socketEvents.emit("chat/edit-chat", { chatId: chatId });
setConfirmDeleteModal(false);
setConfirmClearModal(false);
setOpen(false);

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

@ -94,6 +94,20 @@ export const useChatDetails = (chatId: number) => { @@ -94,6 +94,20 @@ export const useChatDetails = (chatId: number) => {
history.push(`/chats`);
};
const onDeleteChatForMe = () => {
setHeaderChatInfo(headerChatInfoInitialState);
simpleDispatch(new UnselectChat());
history.push(`/chats`);
};
const onDeleteChatForAll = () => {
setHeaderChatInfo(headerChatInfoInitialState);
simpleDispatch(new UnselectChat());
history.push(`/chats`);
};
const updateChatData = (id: number) => {
if (id !== chatId) return;
fetchDetails();
@ -112,16 +126,17 @@ export const useChatDetails = (chatId: number) => { @@ -112,16 +126,17 @@ export const useChatDetails = (chatId: number) => {
};
const onChangeChatRole = (data: { chatId: number }) => {
console.log("changed role");
updateChatData(data?.chatId);
};
// APP EVENTS LISTENERS //
useEventsListener("onDeleteChatForMe", onDeleteChatHeaderData);
useEventsListener("onDeleteChatForMe", onDeleteChatForMe);
useEventsListener("onClearAllChats", onClearAllChats);
useEventsListener("onDeleteAllChats", onDeleteAllChats);
useSocketListener("chat/delete-chat", onDeleteChatHeaderData, [chatId]);
useSocketListener("chat/delete-chat", onDeleteChatForAll, [chatId]);
useSocketListener("chat/edit-chat", onEditChat, [chatDetails]);
useSocketListener("chat/pined-message", onPinedMessage, [chatDetails]);
useSocketListener("chat/delete-message", onDeleteChatMessage, [chatDetails]);

17
src/containers/Chats/hooks/use-chat-messages.hook.ts

@ -7,6 +7,7 @@ import { @@ -7,6 +7,7 @@ import {
import {
useEventsListener,
useIdsList,
useSocket,
useSocketListener,
} from "@/shared/hooks/";
import _ from "lodash";
@ -26,6 +27,7 @@ export const useChatMessages = ( @@ -26,6 +27,7 @@ export const useChatMessages = (
lastMessageId: number
) => {
const account = useSelector(getProfile);
const socket = useSocket();
const [replyTo, setReplyTo] = useState<IChatMessage>(null);
const [scrollToId, setScrollToId] = useState(null);
@ -208,6 +210,12 @@ export const useChatMessages = ( @@ -208,6 +210,12 @@ export const useChatMessages = (
) {
_setItems([message, ...messages]);
socket.emit("chat/read-message", {
userId: account.id,
messagesIds: [message.id],
chatId,
});
if (message.userId === account.id) setScrollToId(message.id);
}
};
@ -224,8 +232,13 @@ export const useChatMessages = ( @@ -224,8 +232,13 @@ export const useChatMessages = (
if (!_.isEmpty(messagesToAdd)) {
_setItems([...messagesToAdd, ...messages]);
// const messagesIds = messagesToAdd.map((it) => it.id);
resetList(); // temporary solution
const messagesIds = messagesToAdd.map((it) => it.id);
// resetList(); // temporary solution
socket.emit("chat/read-message", {
userId: account.id,
messagesIds,
chatId,
});
}
}
};

18
src/containers/Chats/hooks/use-chats-list.hook.ts

@ -134,6 +134,12 @@ export const useChatList = () => { @@ -134,6 +134,12 @@ export const useChatList = () => {
[reloadIfIncludes]
);
useEventsListener(
"onDeleteChatForMe",
({ chatId }) => reloadIfIncludes(chatId),
[reloadIfIncludes]
);
useEventsListener("onDeleteChatForMe", onDeleteChat, [chats]);
useEventsListener("onDeleteAllChats", () => _setItems([]), [_setItems]);
@ -158,7 +164,11 @@ export const useChatList = () => { @@ -158,7 +164,11 @@ export const useChatList = () => {
// SOCKET LISTENERS //
useSocketListener("chat/delete-chat", onDeleteChat, [chats]);
useSocketListener(
"chat/delete-chat",
({ chatId }) => reloadIfIncludes(chatId),
[reloadIfIncludes]
);
useSocketListener("chat/edit-chat", onEditChat, [chats]);
@ -172,6 +182,12 @@ export const useChatList = () => { @@ -172,6 +182,12 @@ export const useChatList = () => {
[reloadIfIncludes]
);
useSocketListener(
"chat/change-role",
({ chatId }) => reloadIfIncludes(chatId),
[reloadIfIncludes]
);
return {
chats: chats as IChat[],
searchString,

1
src/shared/hooks/index.ts

@ -2,3 +2,4 @@ export * from "./usePaginationList"; @@ -2,3 +2,4 @@ export * from "./usePaginationList";
export * from "./use-ids-list.hook";
export * from "./use-events-listener.hook";
export * from "./use-socket-listener.hook";
export * from "./use-socket.hook";

5
src/shared/hooks/use-socket.hook.ts

@ -0,0 +1,5 @@ @@ -0,0 +1,5 @@
import { SocketIo } from "@/services/system";
export const useSocket = () => {
return SocketIo.get();
};
Loading…
Cancel
Save