refactor: extract omnichannel permissions' logic from RoomActionsView

This commit is contained in:
Gerzon Z 2022-04-18 16:27:57 -04:00
parent ff708b1c57
commit bedc3a7927
2 changed files with 36 additions and 61 deletions

View File

@ -49,11 +49,10 @@ interface IRoomActionsViewProps extends IBaseScreen<ChatsStackParamList, 'RoomAc
editRoomPermission?: string[];
toggleRoomE2EEncryptionPermission?: string[];
viewBroadcastMemberListPermission?: string[];
transferLivechatGuestPermission?: string[];
createTeamPermission?: string[];
addTeamChannelPermission?: string[];
convertTeamPermission?: string[];
viewCannedResponsesPermission?: string[];
omnichannelPermissions: string[];
}
interface IRoomActionsViewState {
@ -65,14 +64,11 @@ interface IRoomActionsViewState {
canAutoTranslate: boolean;
canAddUser: boolean;
canInviteUser: boolean;
canForwardGuest: boolean;
canReturnQueue: boolean;
canEdit: boolean;
canToggleEncryption: boolean;
canCreateTeam: boolean;
canAddChannelToTeam: boolean;
canConvertTeam: boolean;
canViewCannedResponse: boolean;
}
class RoomActionsView extends React.Component<IRoomActionsViewProps, IRoomActionsViewState> {
@ -80,6 +76,7 @@ class RoomActionsView extends React.Component<IRoomActionsViewProps, IRoomAction
private rid: string;
private t: string;
private joined: boolean;
private omnichannelPermissions: boolean[];
private roomObservable?: Observable<TSubscriptionModel>;
private subscription?: Subscription;
@ -104,6 +101,7 @@ class RoomActionsView extends React.Component<IRoomActionsViewProps, IRoomAction
this.rid = props.route.params?.rid;
this.t = props.route.params?.t;
this.joined = props.route.params?.joined;
this.omnichannelPermissions = props.route.params?.omnichannelPermissions;
this.state = {
room: room || { rid: this.rid, t: this.t },
membersCount: 0,
@ -113,14 +111,11 @@ class RoomActionsView extends React.Component<IRoomActionsViewProps, IRoomAction
canAutoTranslate: false,
canAddUser: false,
canInviteUser: false,
canForwardGuest: false,
canReturnQueue: false,
canEdit: false,
canToggleEncryption: false,
canCreateTeam: false,
canAddChannelToTeam: false,
canConvertTeam: false,
canViewCannedResponse: false
canConvertTeam: false
};
if (room && room.observe && room.rid) {
this.roomObservable = room.observe();
@ -185,14 +180,6 @@ class RoomActionsView extends React.Component<IRoomActionsViewProps, IRoomAction
canAddChannelToTeam,
canConvertTeam
});
// livechat permissions
if (room.t === 'l') {
const canForwardGuest = await this.canForwardGuest();
const canReturnQueue = await this.canReturnQueue();
const canViewCannedResponse = await this.canViewCannedResponse();
this.setState({ canForwardGuest, canReturnQueue, canViewCannedResponse });
}
}
}
@ -326,31 +313,6 @@ class RoomActionsView extends React.Component<IRoomActionsViewProps, IRoomAction
return result;
};
canForwardGuest = async () => {
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<IRoomActionsViewProps, IRoomAction
};
renderOmnichannelSection = () => {
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<IRoomActionsViewProps, IRoomAction
return (
<List.Section>
{canForwardGuest ? (
{this.omnichannelPermissions[0] ? (
<>
<List.Item
title='Forward'
@ -999,7 +961,7 @@ class RoomActionsView extends React.Component<IRoomActionsViewProps, IRoomAction
</>
) : null}
{canReturnQueue ? (
{this.omnichannelPermissions[1] ? (
<>
<List.Item
title='Return'
@ -1034,8 +996,7 @@ class RoomActionsView extends React.Component<IRoomActionsViewProps, IRoomAction
};
render() {
const { room, membersCount, canViewMembers, canAddUser, canInviteUser, joined, canAutoTranslate, canViewCannedResponse } =
this.state;
const { room, membersCount, canViewMembers, canAddUser, canInviteUser, joined, canAutoTranslate } = this.state;
const { rid, t, prid } = room;
const isGroupChat = RocketChat.isGroupChat(room);
@ -1125,7 +1086,7 @@ class RoomActionsView extends React.Component<IRoomActionsViewProps, IRoomAction
</>
) : null}
{['l'].includes(t) && !this.isOmnichannelPreview && canViewCannedResponse ? (
{['l'].includes(t) && !this.isOmnichannelPreview && this.omnichannelPermissions[2] ? (
<>
<List.Item
title='Canned_Responses'
@ -1288,11 +1249,9 @@ const mapStateToProps = (state: IApplicationState) => ({
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)));

View File

@ -137,6 +137,7 @@ interface IRoomViewProps extends IBaseScreen<ChatsStackParamList, 'RoomView'> {
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<IRoomViewProps, IRoomViewState> {
@ -237,7 +239,8 @@ class RoomView extends React.Component<IRoomViewProps, IRoomViewState> {
unreadsCount: null,
roomUserId,
canReturnQueue: false,
canForwardGuest: false
canForwardGuest: false,
canViewCannedResponse: false
};
this.setHeader();
@ -437,6 +440,14 @@ class RoomView extends React.Component<IRoomViewProps, IRoomViewState> {
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<IRoomViewProps, IRoomViewState> {
};
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<IRoomViewProps, IRoomViewState> {
numIconsRight = 3;
}
const headerTitlePosition = getHeaderTitlePosition({ insets, numIconsRight });
const omnichannelPermissions = [canForwardGuest, canReturnQueue];
navigation.setOptions({
headerShown: true,
@ -552,7 +565,7 @@ class RoomView extends React.Component<IRoomViewProps, IRoomViewState> {
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<IRoomViewProps, IRoomViewState> {
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<IRoomViewProps, IRoomViewState> {
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<IRoomViewProps, IRoomViewState> {
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))));