From f1dba2721357b8cda3e5692a7f97725cf51f15b5 Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Sun, 13 Jun 2021 19:56:54 +0300 Subject: [PATCH] Close chat after leaving of deleting history --- src/api/gramjs/methods/messages.ts | 2 +- src/components/common/DeleteChatModal.tsx | 8 ++-- .../right/management/ManageGroup.tsx | 4 +- .../right/management/ManageUser.tsx | 13 +++--- src/modules/actions/api/chats.ts | 44 +++++++++++-------- src/modules/actions/api/messages.ts | 18 +++++--- 6 files changed, 50 insertions(+), 39 deletions(-) diff --git a/src/api/gramjs/methods/messages.ts b/src/api/gramjs/methods/messages.ts index 734b6df80..df6c7a11f 100644 --- a/src/api/gramjs/methods/messages.ts +++ b/src/api/gramjs/methods/messages.ts @@ -585,7 +585,7 @@ export async function deleteScheduledMessages({ export async function deleteHistory({ chat, shouldDeleteForAll, maxId, }: { - chat: ApiChat; shouldDeleteForAll?: boolean; maxId: number; + chat: ApiChat; shouldDeleteForAll?: boolean; maxId?: number; }) { const isChannel = getEntityTypeById(chat.id) === 'channel'; const result = await invokeRequest( diff --git a/src/components/common/DeleteChatModal.tsx b/src/components/common/DeleteChatModal.tsx index b343781de..53b652db4 100644 --- a/src/components/common/DeleteChatModal.tsx +++ b/src/components/common/DeleteChatModal.tsx @@ -61,13 +61,14 @@ const DeleteChatModal: FC = ({ const chatTitle = getChatTitle(lang, chat); const handleDeleteMessageForAll = useCallback(() => { - deleteHistory({ chatId: chat.id, maxId: chat.lastMessage!.id, shouldDeleteForAll: true }); + deleteHistory({ chatId: chat.id, shouldDeleteForAll: true }); + onClose(); - }, [deleteHistory, chat.id, chat.lastMessage, onClose]); + }, [deleteHistory, chat.id, onClose]); const handleDeleteChat = useCallback(() => { if (isPrivateChat || isBasicGroup) { - deleteHistory({ chatId: chat.id, maxId: chat.lastMessage!.id, shouldDeleteForAll: false }); + deleteHistory({ chatId: chat.id, shouldDeleteForAll: false }); } else if ((isChannel || isSuperGroup) && !chat.isCreator) { leaveChannel({ chatId: chat.id }); } else if ((isChannel || isSuperGroup) && chat.isCreator) { @@ -80,7 +81,6 @@ const DeleteChatModal: FC = ({ isChannel, isSuperGroup, chat.isCreator, - chat.lastMessage, chat.id, onClose, deleteHistory, diff --git a/src/components/right/management/ManageGroup.tsx b/src/components/right/management/ManageGroup.tsx index 6a1d6c995..629c5f9c4 100644 --- a/src/components/right/management/ManageGroup.tsx +++ b/src/components/right/management/ManageGroup.tsx @@ -182,7 +182,7 @@ const ManageGroup: FC = ({ const handleDeleteGroup = useCallback(() => { if (isBasicGroup) { - deleteHistory({ chatId: chat.id, maxId: chat.lastMessage!.id, shouldDeleteForAll: false }); + deleteHistory({ chatId: chat.id, shouldDeleteForAll: false }); } else if (!chat.isCreator) { leaveChannel({ chatId: chat.id }); } else { @@ -192,7 +192,7 @@ const ManageGroup: FC = ({ closeManagement(); openChat({ id: undefined }); }, [ - isBasicGroup, chat.isCreator, chat.id, chat.lastMessage, + isBasicGroup, chat.isCreator, chat.id, closeDeleteDialog, closeManagement, deleteHistory, leaveChannel, deleteChannel, openChat, ]); diff --git a/src/components/right/management/ManageUser.tsx b/src/components/right/management/ManageUser.tsx index f120fd8c9..68b934424 100644 --- a/src/components/right/management/ManageUser.tsx +++ b/src/components/right/management/ManageUser.tsx @@ -117,18 +117,15 @@ const ManageUser: FC = ({ }, [firstName, lastName, updateContact, userId, isNotificationsEnabled]); const handleDeleteContact = useCallback(() => { - if (chat.lastMessage) { - deleteHistory({ - chatId: chat.id, - maxId: chat.lastMessage!.id, - shouldDeleteForAll: false, - }); - } + deleteHistory({ + chatId: chat.id, + shouldDeleteForAll: false, + }); deleteUser({ userId }); closeDeleteDialog(); closeManagement(); openChat({ id: undefined }); - }, [chat.id, chat.lastMessage, closeDeleteDialog, closeManagement, deleteHistory, deleteUser, openChat, userId]); + }, [chat.id, closeDeleteDialog, closeManagement, deleteHistory, deleteUser, openChat, userId]); if (!user) { return undefined; diff --git a/src/modules/actions/api/chats.ts b/src/modules/actions/api/chats.ts index af0c69b26..42f3dcef4 100644 --- a/src/modules/actions/api/chats.ts +++ b/src/modules/actions/api/chats.ts @@ -223,31 +223,39 @@ addReducer('joinChannel', (global, actions, payload) => { }); addReducer('leaveChannel', (global, actions, payload) => { - const { chatId } = payload!; - const chat = selectChat(global, chatId); - if (!chat) { - return; - } + (async () => { + const { chatId } = payload!; + const chat = selectChat(global, chatId); + if (!chat) { + return; + } - const { id: channelId, accessHash } = chat; + const { id: channelId, accessHash } = chat; - if (channelId && accessHash) { - void callApi('leaveChannel', { channelId, accessHash }); - } + if (channelId && accessHash) { + await callApi('leaveChannel', { channelId, accessHash }); + } + + actions.openChat({ id: undefined }); + })(); }); addReducer('deleteChannel', (global, actions, payload) => { - const { chatId } = payload!; - const chat = selectChat(global, chatId); - if (!chat) { - return; - } + (async () => { + const { chatId } = payload!; + const chat = selectChat(global, chatId); + if (!chat) { + return; + } - const { id: channelId, accessHash } = chat; + const { id: channelId, accessHash } = chat; - if (channelId && accessHash) { - void callApi('deleteChannel', { channelId, accessHash }); - } + if (channelId && accessHash) { + await callApi('deleteChannel', { channelId, accessHash }); + } + + actions.openChat({ id: undefined }); + })(); }); addReducer('createGroupChat', (global, actions, payload) => { diff --git a/src/modules/actions/api/messages.ts b/src/modules/actions/api/messages.ts index 348a33ae2..2a8b5909e 100644 --- a/src/modules/actions/api/messages.ts +++ b/src/modules/actions/api/messages.ts @@ -388,13 +388,19 @@ addReducer('deleteScheduledMessages', (global, actions, payload) => { }); addReducer('deleteHistory', (global, actions, payload) => { - const { chatId, maxId, shouldDeleteForAll } = payload!; - const chat = selectChat(global, chatId); - if (!chat) { - return; - } + (async () => { + const { chatId, shouldDeleteForAll } = payload!; + const chat = selectChat(global, chatId); + if (!chat) { + return; + } - void callApi('deleteHistory', { chat, shouldDeleteForAll, maxId }); + const maxId = chat.lastMessage && chat.lastMessage.id; + + await callApi('deleteHistory', { chat, shouldDeleteForAll, maxId }); + + actions.openChat({ id: undefined }); + })(); }); addReducer('markMessageListRead', (global, actions, payload) => {