From 83b7f3867de851533e48de33e27cfa10b91aaa28 Mon Sep 17 00:00:00 2001 From: Gung Wah <41157464+kresnaputra@users.noreply.github.com> Date: Fri, 19 Mar 2021 01:33:20 +0800 Subject: [PATCH] [CHORE] Refactor RoomActionsView permissions (#2872) Co-authored-by: Diego Mello --- app/views/RoomActionsView/index.js | 34 +++++++++++++++++------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/app/views/RoomActionsView/index.js b/app/views/RoomActionsView/index.js index 41707ee46..7f5ba713a 100644 --- a/app/views/RoomActionsView/index.js +++ b/app/views/RoomActionsView/index.js @@ -125,18 +125,22 @@ class RoomActionsView extends React.Component { this.updateRoomMember(); } - const canAutoTranslate = RocketChat.canAutoTranslate(); - this.setState({ canAutoTranslate }); + const canAutoTranslate = await RocketChat.canAutoTranslate(); + const canAddUser = await this.canAddUser(); + const canInviteUser = await this.canInviteUser(); + const canEdit = await this.canEdit(); + const canToggleEncryption = await this.canToggleEncryption(); + const canViewMembers = await this.canViewMembers(); - this.canAddUser(); - this.canInviteUser(); - this.canEdit(); - this.canToggleEncryption(); + this.setState({ + canAutoTranslate, canAddUser, canInviteUser, canEdit, canToggleEncryption, canViewMembers + }); // livechat permissions if (room.t === 'l') { - this.canForwardGuest(); - this.canReturnQueue(); + const canForwardGuest = await this.canForwardGuest(); + const canReturnQueue = await this.canReturnQueue(); + this.setState({ canForwardGuest, canReturnQueue }); } } } @@ -182,7 +186,7 @@ class RoomActionsView extends React.Component { if (t === 'p' && permissions[2]) { canAddUser = true; } - this.setState({ canAddUser }); + return canAddUser; } canInviteUser = async() => { @@ -192,7 +196,7 @@ class RoomActionsView extends React.Component { const permissions = await RocketChat.hasPermission([createInviteLinksPermission], rid); const canInviteUser = permissions[0]; - this.setState({ canInviteUser }); + return canInviteUser; } canEdit = async() => { @@ -202,7 +206,7 @@ class RoomActionsView extends React.Component { const permissions = await RocketChat.hasPermission([editRoomPermission], rid); const canEdit = permissions[0]; - this.setState({ canEdit }); + return canEdit; } canToggleEncryption = async() => { @@ -212,7 +216,7 @@ class RoomActionsView extends React.Component { const permissions = await RocketChat.hasPermission([toggleRoomE2EEncryptionPermission], rid); const canToggleEncryption = permissions[0]; - this.setState({ canToggleEncryption }); + return canToggleEncryption; } canViewMembers = async() => { @@ -229,7 +233,6 @@ class RoomActionsView extends React.Component { // This method is executed only in componentDidMount and returns a value // We save the state to read in render const result = (t === 'c' || t === 'p'); - this.setState({ canViewMembers: result }); return result; } @@ -238,13 +241,13 @@ class RoomActionsView extends React.Component { const { transferLivechatGuestPermission } = this.props; const { rid } = room; const permissions = await RocketChat.hasPermission([transferLivechatGuestPermission], rid); - this.setState({ canForwardGuest: permissions[0] }); + return permissions[0]; } canReturnQueue = async() => { try { const { returnQueue } = await RocketChat.getRoutingConfig(); - this.setState({ canReturnQueue: returnQueue }); + return returnQueue; } catch { // do nothing } @@ -582,6 +585,7 @@ class RoomActionsView extends React.Component { rid, t, encrypted } = room; const isGroupChat = RocketChat.isGroupChat(room); + return (