From bedc3a79275bdff195836bfcc0ca2272fe2452f8 Mon Sep 17 00:00:00 2001 From: Gerzon Z Date: Mon, 18 Apr 2022 16:27:57 -0400 Subject: [PATCH] refactor: extract omnichannel permissions' logic from RoomActionsView --- app/views/RoomActionsView/index.tsx | 61 +++++------------------------ app/views/RoomView/index.tsx | 36 ++++++++++++----- 2 files changed, 36 insertions(+), 61 deletions(-) diff --git a/app/views/RoomActionsView/index.tsx b/app/views/RoomActionsView/index.tsx index e3ab5c437..9f286b82e 100644 --- a/app/views/RoomActionsView/index.tsx +++ b/app/views/RoomActionsView/index.tsx @@ -49,11 +49,10 @@ interface IRoomActionsViewProps extends IBaseScreen { @@ -80,6 +76,7 @@ class RoomActionsView extends React.Component; private subscription?: Subscription; @@ -104,6 +101,7 @@ class RoomActionsView extends React.Component { - const { room } = this.state; - const { transferLivechatGuestPermission } = this.props; - const { rid } = room; - const permissions = await RocketChat.hasPermission([transferLivechatGuestPermission], rid); - return permissions[0]; - }; - - canViewCannedResponse = async () => { - const { room } = this.state; - const { viewCannedResponsesPermission } = this.props; - const { rid } = room; - const permissions = await RocketChat.hasPermission([viewCannedResponsesPermission], rid); - return permissions[0]; - }; - - canReturnQueue = async () => { - try { - const { returnQueue } = await RocketChat.getRoutingConfig(); - return returnQueue; - } catch { - return false; - } - }; - renderEncryptedSwitch = () => { const { room, canToggleEncryption, canEdit } = this.state; const { encrypted } = room; @@ -972,7 +934,7 @@ class RoomActionsView extends React.Component { - const { room, canForwardGuest, canReturnQueue } = this.state; + const { room } = this.state; const { rid, t } = room; const { theme } = this.props; @@ -982,7 +944,7 @@ class RoomActionsView extends React.Component - {canForwardGuest ? ( + {this.omnichannelPermissions[0] ? ( <> ) : null} - {canReturnQueue ? ( + {this.omnichannelPermissions[1] ? ( <> ) : null} - {['l'].includes(t) && !this.isOmnichannelPreview && canViewCannedResponse ? ( + {['l'].includes(t) && !this.isOmnichannelPreview && this.omnichannelPermissions[2] ? ( <> ({ editRoomPermission: state.permissions['edit-room'], toggleRoomE2EEncryptionPermission: state.permissions['toggle-room-e2e-encryption'], viewBroadcastMemberListPermission: state.permissions['view-broadcast-member-list'], - transferLivechatGuestPermission: state.permissions['transfer-livechat-guest'], createTeamPermission: state.permissions['create-team'], addTeamChannelPermission: state.permissions['add-team-channel'], - convertTeamPermission: state.permissions['convert-team'], - viewCannedResponsesPermission: state.permissions['view-canned-responses'] + convertTeamPermission: state.permissions['convert-team'] }); export default connect(mapStateToProps)(withTheme(withDimensions(RoomActionsView))); diff --git a/app/views/RoomView/index.tsx b/app/views/RoomView/index.tsx index f0e3791f7..7764a0ee6 100644 --- a/app/views/RoomView/index.tsx +++ b/app/views/RoomView/index.tsx @@ -137,6 +137,7 @@ interface IRoomViewProps extends IBaseScreen { height: number; insets: EdgeInsets; transferLivechatGuestPermission?: string[]; // TODO: Check if its the correct type + viewCannedResponsesPermission?: string[]; // TODO: Check if its the correct type } type TRoomUpdate = typeof roomAttrsUpdate[number]; @@ -164,8 +165,9 @@ interface IRoomViewState { readOnly: boolean; unreadsCount: number | null; roomUserId?: string | null; - canReturnQueue?: boolean; - canForwardGuest?: boolean; + canReturnQueue: boolean; + canForwardGuest: boolean; + canViewCannedResponse: boolean; } class RoomView extends React.Component { @@ -237,7 +239,8 @@ class RoomView extends React.Component { unreadsCount: null, roomUserId, canReturnQueue: false, - canForwardGuest: false + canForwardGuest: false, + canViewCannedResponse: false }; this.setHeader(); @@ -437,6 +440,14 @@ class RoomView extends React.Component { return permissions[0]; }; + canViewCannedResponse = async () => { + const { room } = this.state; + const { viewCannedResponsesPermission } = this.props; + const { rid } = room; + const permissions = await RocketChat.hasPermission([viewCannedResponsesPermission], rid); + return permissions[0]; + }; + canReturnQueue = async () => { try { const { returnQueue } = await RocketChat.getRoutingConfig(); @@ -447,9 +458,10 @@ class RoomView extends React.Component { }; setOmnichannelPermissions = async () => { - const canReturnQueue = await this.canReturnQueue(); const canForwardGuest = await this.canForwardGuest(); - this.setState({ canReturnQueue, canForwardGuest }); + const canReturnQueue = await this.canReturnQueue(); + const canViewCannedResponse = await this.canViewCannedResponse(); + this.setState({ canForwardGuest, canReturnQueue, canViewCannedResponse }); }; get isOmnichannel() { @@ -505,6 +517,7 @@ class RoomView extends React.Component { numIconsRight = 3; } const headerTitlePosition = getHeaderTitlePosition({ insets, numIconsRight }); + const omnichannelPermissions = [canForwardGuest, canReturnQueue]; navigation.setOptions({ headerShown: true, @@ -552,7 +565,7 @@ class RoomView extends React.Component { teamId={teamId} joined={joined} status={room.status} - omnichannelPermissions={[canForwardGuest, canReturnQueue]} + omnichannelPermissions={omnichannelPermissions} t={this.t || t} encrypted={encrypted} navigation={navigation} @@ -564,7 +577,7 @@ class RoomView extends React.Component { goRoomActionsView = (screen?: string) => { logEvent(events.ROOM_GO_RA); - const { room, member, joined } = this.state; + const { room, member, joined, canForwardGuest, canReturnQueue, canViewCannedResponse } = this.state; const { navigation, isMasterDetail } = this.props; if (isMasterDetail) { // @ts-ignore TODO: find a way to make it work @@ -578,7 +591,8 @@ class RoomView extends React.Component { room, member, showCloseModal: !!screen, - joined + joined, + omnichannelPermissions: [canForwardGuest, canReturnQueue, canViewCannedResponse] } }); } else if (this.rid && this.t) { @@ -587,7 +601,8 @@ class RoomView extends React.Component { t: this.t as SubscriptionType, room: room as TSubscriptionModel, member, - joined + joined, + omnichannelPermissions: [canForwardGuest, canReturnQueue, canViewCannedResponse] }); } }; @@ -1442,7 +1457,8 @@ const mapStateToProps = (state: IApplicationState) => ({ serverVersion: state.server.version, Message_Read_Receipt_Enabled: state.settings.Message_Read_Receipt_Enabled as boolean, Hide_System_Messages: state.settings.Hide_System_Messages as string[], - transferLivechatGuestPermission: state.permissions['transfer-livechat-guest'] + transferLivechatGuestPermission: state.permissions['transfer-livechat-guest'], + viewCannedResponsesPermission: state.permissions['view-canned-responses'] }); export default connect(mapStateToProps)(withDimensions(withTheme(withSafeAreaInsets(RoomView))));