Fixes for user and chat reducers
This commit is contained in:
parent
c3d55e58b5
commit
1285499e96
@ -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<string, ApiChat>): GlobalState {
|
||||
const updatedById = mapValues(newById, (chat, id) => {
|
||||
return getUpdatedChat(global, id, chat);
|
||||
});
|
||||
const updatedById = Object.keys(newById).reduce((acc: Record<string, ApiChat>, 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<string, ApiChat
|
||||
// @optimization Allows to avoid redundant updates which cause a lot of renders
|
||||
export function addChats(global: GlobalState, newById: Record<string, ApiChat>): GlobalState {
|
||||
const { byId } = global.chats;
|
||||
let isAdded = false;
|
||||
let isUpdated = false;
|
||||
|
||||
const addedById = Object.keys(newById).reduce<Record<string, ApiChat>>((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;
|
||||
|
||||
@ -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<string, ApiUser>): 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<string, ApiUser>): GlobalState {
|
||||
const updatedById = mapValues(newById, (user, id) => {
|
||||
return getUpdatedUser(global, id, user);
|
||||
});
|
||||
const updatedById = Object.keys(newById).reduce((acc: Record<string, ApiUser>, 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<string, ApiUser
|
||||
// @optimization Allows to avoid redundant updates which cause a lot of renders
|
||||
export function addUsers(global: GlobalState, newById: Record<string, ApiUser>): GlobalState {
|
||||
const { byId } = global.users;
|
||||
let isAdded = false;
|
||||
let isUpdated = false;
|
||||
|
||||
const addedById = Object.keys(newById).reduce<Record<string, ApiUser>>((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;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user