From 1285499e964d79d17cb0abe8069f472a87cc5a70 Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Tue, 14 Dec 2021 22:40:37 +0400 Subject: [PATCH] Fixes for user and chat reducers --- src/modules/reducers/chats.ts | 32 +++++++++++++++++++++----------- src/modules/reducers/users.ts | 32 +++++++++++++++++++++----------- 2 files changed, 42 insertions(+), 22 deletions(-) diff --git a/src/modules/reducers/chats.ts b/src/modules/reducers/chats.ts index 7bca789f3..5ad0de83a 100644 --- a/src/modules/reducers/chats.ts +++ b/src/modules/reducers/chats.ts @@ -2,7 +2,7 @@ import { GlobalState } from '../../global/types'; import { ApiChat, ApiPhoto } from '../../api/types'; import { ARCHIVED_FOLDER_ID } from '../../config'; -import { mapValues, omit } from '../../util/iteratees'; +import { omit } from '../../util/iteratees'; import { selectChatListType } from '../selectors'; export function replaceChatListIds( @@ -54,6 +54,9 @@ export function updateChat( const { byId } = global.chats; const updatedChat = getUpdatedChat(global, chatId, chatUpdate, photo); + if (!updatedChat) { + return global; + } return replaceChats(global, { ...byId, @@ -62,9 +65,14 @@ export function updateChat( } export function updateChats(global: GlobalState, newById: Record): GlobalState { - const updatedById = mapValues(newById, (chat, id) => { - return getUpdatedChat(global, id, chat); - }); + const updatedById = Object.keys(newById).reduce((acc: Record, id) => { + const updatedChat = getUpdatedChat(global, id, newById[id]); + if (updatedChat) { + acc[id] = updatedChat; + } + + return acc; + }, {}); global = replaceChats(global, { ...global.chats.byId, @@ -77,20 +85,22 @@ export function updateChats(global: GlobalState, newById: Record): GlobalState { const { byId } = global.chats; - let isAdded = false; + let isUpdated = false; const addedById = Object.keys(newById).reduce>((acc, id) => { if (!byId[id] || (byId[id].isMin && !newById[id].isMin)) { - acc[id] = getUpdatedChat(global, id, newById[id]); - - if (!isAdded) { - isAdded = true; + const updatedChat = getUpdatedChat(global, id, newById[id]); + if (updatedChat) { + acc[id] = updatedChat; + if (!isUpdated) { + isUpdated = true; + } } } return acc; }, {}); - if (!isAdded) { + if (!isUpdated) { return global; } @@ -116,7 +126,7 @@ function getUpdatedChat( }; if (!updatedChat.id || !updatedChat.type) { - return updatedChat; + return undefined; } return updatedChat; diff --git a/src/modules/reducers/users.ts b/src/modules/reducers/users.ts index 707b9ee82..6a91fd12e 100644 --- a/src/modules/reducers/users.ts +++ b/src/modules/reducers/users.ts @@ -1,7 +1,7 @@ import { GlobalState } from '../../global/types'; import { ApiUser, ApiUserStatus } from '../../api/types'; -import { mapValues, omit, pick } from '../../util/iteratees'; +import { omit, pick } from '../../util/iteratees'; import { MEMO_EMPTY_ARRAY } from '../../util/memo'; export function replaceUsers(global: GlobalState, newById: Record): GlobalState { @@ -40,6 +40,9 @@ export function updateUser(global: GlobalState, userId: string, userUpdate: Part const { byId } = global.users; const updatedUser = getUpdatedUser(global, userId, userUpdate); + if (!updatedUser) { + return global; + } global = updateContactList(global, [updatedUser]); @@ -50,9 +53,14 @@ export function updateUser(global: GlobalState, userId: string, userUpdate: Part } export function updateUsers(global: GlobalState, newById: Record): GlobalState { - const updatedById = mapValues(newById, (user, id) => { - return getUpdatedUser(global, id, user); - }); + const updatedById = Object.keys(newById).reduce((acc: Record, id) => { + const updatedUser = getUpdatedUser(global, id, newById[id]); + if (updatedUser) { + acc[id] = updatedUser; + } + + return acc; + }, {}); global = replaceUsers(global, { ...global.users.byId, @@ -67,20 +75,22 @@ export function updateUsers(global: GlobalState, newById: Record): GlobalState { const { byId } = global.users; - let isAdded = false; + let isUpdated = false; const addedById = Object.keys(newById).reduce>((acc, id) => { if (!byId[id] || (byId[id].isMin && !newById[id].isMin)) { - acc[id] = getUpdatedUser(global, id, newById[id]); - - if (!isAdded) { - isAdded = true; + const updatedUser = getUpdatedUser(global, id, newById[id]); + if (updatedUser) { + acc[id] = updatedUser; + if (!isUpdated) { + isUpdated = true; + } } } return acc; }, {}); - if (!isAdded) { + if (!isUpdated) { return global; } @@ -106,7 +116,7 @@ function getUpdatedUser(global: GlobalState, userId: string, userUpdate: Partial }; if (!updatedUser.id || !updatedUser.type) { - return user; + return undefined; } return updatedUser;