From ac15661c3e04051d0e1e4a71be90f182f6202301 Mon Sep 17 00:00:00 2001 From: Renan Cleyson Date: Tue, 16 Nov 2021 18:03:08 -0300 Subject: [PATCH] Fix bulk invite button getting a negative count (#7122) * Fix bulk invite button getting a negative counter Signed-off-by: Renan * Change invites to be count on membership events Signed-off-by: Renan * Remove unnecessary bind * Change invitedRooms state to a set of ids Signed-off-by: Renan --- .../tabs/user/SecurityUserSettingsTab.tsx | 67 +++++++++++++------ 1 file changed, 48 insertions(+), 19 deletions(-) diff --git a/src/components/views/settings/tabs/user/SecurityUserSettingsTab.tsx b/src/components/views/settings/tabs/user/SecurityUserSettingsTab.tsx index 5e0cdfcbe3..ed560b8929 100644 --- a/src/components/views/settings/tabs/user/SecurityUserSettingsTab.tsx +++ b/src/components/views/settings/tabs/user/SecurityUserSettingsTab.tsx @@ -76,7 +76,7 @@ interface IState { ignoredUserIds: string[]; waitingUnignored: string[]; managingInvites: boolean; - invitedRoomAmt: number; + invitedRoomIds: Set; } @replaceableComponent("views.settings.tabs.user.SecurityUserSettingsTab") @@ -86,14 +86,14 @@ export default class SecurityUserSettingsTab extends React.Component room.roomId)); this.state = { ignoredUserIds: MatrixClientPeg.get().getIgnoredUsers(), waitingUnignored: [], managingInvites: false, - invitedRoomAmt: invitedRooms.length, + invitedRoomIds, }; } @@ -107,10 +107,12 @@ export default class SecurityUserSettingsTab extends React.Component { @@ -119,6 +121,36 @@ export default class SecurityUserSettingsTab extends React.Component { + if (room.isSpaceRoom()) { + return; + } + + if (membership === "invite") { + this.addInvitedRoom(room); + } else if (this.state.invitedRoomIds.has(room.roomId)) { + // The user isn't invited anymore + this.removeInvitedRoom(room.roomId); + } + }; + + private addInvitedRoom = (room: Room): void => { + this.setState(({ invitedRoomIds }) => ({ + invitedRoomIds: new Set(invitedRoomIds).add(room.roomId), + })); + }; + + private removeInvitedRoom = (roomId: string): void => { + this.setState(({ invitedRoomIds }) => { + const newInvitedRoomIds = new Set(invitedRoomIds); + newInvitedRoomIds.delete(roomId); + + return { + invitedRoomIds: newInvitedRoomIds, + }; + }); + }; + private onGoToUserProfileClick = (): void => { dis.dispatch({ action: 'view_user_info', @@ -150,21 +182,19 @@ export default class SecurityUserSettingsTab extends React.Component { - return room.roomId; - }); + // iterate with a normal for loop in order to retry on action failure + const invitedRoomIdsValues = Array.from(this.state.invitedRoomIds); // Execute all acceptances/rejections sequentially const cli = MatrixClientPeg.get(); const action = accept ? cli.joinRoom.bind(cli) : cli.leave.bind(cli); - for (let i = 0; i < invitedRoomIds.length; i++) { - const roomId = invitedRoomIds[i]; + for (let i = 0; i < invitedRoomIdsValues.length; i++) { + const roomId = invitedRoomIdsValues[i]; // Accept/reject invite await action(roomId).then(() => { // No error, update invited rooms button - this.setState({ invitedRoomAmt: this.state.invitedRoomAmt - 1 }); + this.removeInvitedRoom(roomId); }, async (e) => { // Action failure if (e.errcode === "M_LIMIT_EXCEEDED") { @@ -221,21 +251,20 @@ export default class SecurityUserSettingsTab extends React.Component { _t('Bulk options') } - - { _t("Accept all %(invitedRooms)s invites", { invitedRooms: this.state.invitedRoomAmt }) } + + { _t("Accept all %(invitedRooms)s invites", { invitedRooms: invitedRoomIds.size }) } - - { _t("Reject all %(invitedRooms)s invites", { invitedRooms: this.state.invitedRoomAmt }) } + + { _t("Reject all %(invitedRooms)s invites", { invitedRooms: invitedRoomIds.size }) } { this.state.managingInvites ? :
}