refactor: omnichannel permissions logic outside RightButtons

This commit is contained in:
Gerzon Z 2022-04-18 14:27:10 -04:00
parent 2dcadf8633
commit ff708b1c57
2 changed files with 45 additions and 34 deletions

View File

@ -34,6 +34,7 @@ interface IRightButtonsProps {
showActionSheet: Function; // TODO: Change to proper type
transferLivechatGuestPermission: boolean;
navigation: StackNavigationProp<ChatsStackParamList, 'RoomView'>;
omnichannelPermissions: boolean[];
}
interface IRigthButtonsState {
@ -41,8 +42,6 @@ interface IRigthButtonsState {
tunread: string[];
tunreadUser: string[];
tunreadGroup: string[];
canReturnQueue: boolean;
canForwardGuest: boolean;
}
class RightButtonsContainer extends Component<IRightButtonsProps, IRigthButtonsState> {
@ -55,14 +54,12 @@ class RightButtonsContainer extends Component<IRightButtonsProps, IRigthButtonsS
isFollowingThread: true,
tunread: [],
tunreadUser: [],
tunreadGroup: [],
canReturnQueue: false,
canForwardGuest: false
tunreadGroup: []
};
}
async componentDidMount() {
const { tmid, rid, t } = this.props;
const { tmid, rid } = this.props;
const db = database.active;
if (tmid) {
try {
@ -81,9 +78,6 @@ class RightButtonsContainer extends Component<IRightButtonsProps, IRigthButtonsS
console.log("Can't find subscription to observe.");
}
}
if (t === 'l') {
this.setOmnichannelPermissions();
}
}
shouldComponentUpdate(nextProps: IRightButtonsProps, nextState: IRigthButtonsState) {
@ -113,14 +107,6 @@ class RightButtonsContainer extends Component<IRightButtonsProps, IRigthButtonsS
return false;
}
componentDidUpdate(prevProps: IRightButtonsProps) {
const { status, joined } = this.props;
if (prevProps.status !== status || prevProps.joined !== joined) {
this.setOmnichannelPermissions();
}
}
componentWillUnmount() {
if (this.threadSubscription && this.threadSubscription.unsubscribe) {
this.threadSubscription.unsubscribe();
@ -130,12 +116,6 @@ class RightButtonsContainer extends Component<IRightButtonsProps, IRigthButtonsS
}
}
setOmnichannelPermissions = async () => {
const canReturnQueue = await this.canReturnQueue();
const canForwardGuest = await this.canForwardGuest();
this.setState({ canReturnQueue, canForwardGuest });
};
observeThread = (threadRecord: TMessageModel) => {
const threadObservable: Observable<TMessageModel> = threadRecord.observe();
this.threadSubscription = threadObservable.subscribe(thread => this.updateThread(thread));
@ -232,16 +212,15 @@ class RightButtonsContainer extends Component<IRightButtonsProps, IRigthButtonsS
showMoreActions = () => {
logEvent(events.ROOM_SHOW_MORE_ACTIONS);
const { showActionSheet, rid, navigation } = this.props;
const { canReturnQueue, canForwardGuest } = this.state;
const { showActionSheet, rid, omnichannelPermissions, navigation } = this.props;
const options = [
canForwardGuest && {
omnichannelPermissions[0] && {
title: i18n.t('Forward_Chat'),
icon: 'chat-forward',
onPress: () => navigation.navigate('ForwardLivechatView', { rid })
},
canReturnQueue && {
omnichannelPermissions[1] && {
title: i18n.t('Return_to_waiting_line'),
icon: 'move-to-the-queue',
onPress: () => this.returnLivechat()
@ -335,8 +314,7 @@ class RightButtonsContainer extends Component<IRightButtonsProps, IRigthButtonsS
const mapStateToProps = (state: IApplicationState) => ({
userId: getUserSelector(state).id,
threadsEnabled: state.settings.Threads_enabled as boolean,
isMasterDetail: state.app.isMasterDetail,
transferLivechatGuestPermission: state.permissions['transfer-livechat-guest']
isMasterDetail: state.app.isMasterDetail
});
export default connect(mapStateToProps)(withActionSheet(RightButtonsContainer));

View File

@ -136,6 +136,7 @@ interface IRoomViewProps extends IBaseScreen<ChatsStackParamList, 'RoomView'> {
width: number;
height: number;
insets: EdgeInsets;
transferLivechatGuestPermission?: string[]; // TODO: Check if its the correct type
}
type TRoomUpdate = typeof roomAttrsUpdate[number];
@ -163,6 +164,8 @@ interface IRoomViewState {
readOnly: boolean;
unreadsCount: number | null;
roomUserId?: string | null;
canReturnQueue?: boolean;
canForwardGuest?: boolean;
}
class RoomView extends React.Component<IRoomViewProps, IRoomViewState> {
@ -232,7 +235,9 @@ class RoomView extends React.Component<IRoomViewProps, IRoomViewState> {
reacting: false,
readOnly: false,
unreadsCount: null,
roomUserId
roomUserId,
canReturnQueue: false,
canForwardGuest: false
};
this.setHeader();
@ -280,6 +285,9 @@ class RoomView extends React.Component<IRoomViewProps, IRoomViewState> {
if (isIOS && this.rid) {
this.updateUnreadCount();
}
if (this.t === 'l') {
this.setOmnichannelPermissions();
}
});
if (isTablet) {
EventEmitter.addEventListener(KEY_COMMAND, this.handleCommands);
@ -346,6 +354,7 @@ class RoomView extends React.Component<IRoomViewProps, IRoomViewState> {
!dequal(prevState.roomUpdate.status, roomUpdate.status) ||
prevState.joined !== joined
) {
this.setOmnichannelPermissions();
this.setHeader();
}
}
@ -422,13 +431,34 @@ class RoomView extends React.Component<IRoomViewProps, IRoomViewState> {
console.countReset(`${this.constructor.name}.render calls`);
}
canForwardGuest = async () => {
const { transferLivechatGuestPermission } = this.props;
const permissions = await RocketChat.hasPermission([transferLivechatGuestPermission], this.rid);
return permissions[0];
};
canReturnQueue = async () => {
try {
const { returnQueue } = await RocketChat.getRoutingConfig();
return returnQueue;
} catch {
return false;
}
};
setOmnichannelPermissions = async () => {
const canReturnQueue = await this.canReturnQueue();
const canForwardGuest = await this.canForwardGuest();
this.setState({ canReturnQueue, canForwardGuest });
};
get isOmnichannel() {
const { room } = this.state;
return room.t === 'l';
}
setHeader = () => {
const { room, unreadsCount, roomUserId, joined } = this.state;
const { room, unreadsCount, roomUserId, joined, canReturnQueue, canForwardGuest } = this.state;
const { navigation, isMasterDetail, theme, baseUrl, user, insets, route } = this.props;
const { rid, tmid } = this;
if (!room.rid) {
@ -455,6 +485,7 @@ class RoomView extends React.Component<IRoomViewProps, IRoomViewState> {
let token: string | undefined;
let avatar: string | undefined;
let visitor: IVisitor | undefined;
let status: string | undefined;
if ('id' in room) {
subtitle = room.topic;
t = room.t;
@ -464,10 +495,11 @@ class RoomView extends React.Component<IRoomViewProps, IRoomViewState> {
({ id: userId, token } = user);
avatar = room.name;
visitor = room.visitor;
status = room.status;
}
let numIconsRight = 2;
if (tmid || room.status) {
if (tmid || (status && joined)) {
numIconsRight = 1;
} else if (teamId && isTeamRoom({ teamId, joined })) {
numIconsRight = 3;
@ -520,6 +552,7 @@ class RoomView extends React.Component<IRoomViewProps, IRoomViewState> {
teamId={teamId}
joined={joined}
status={room.status}
omnichannelPermissions={[canForwardGuest, canReturnQueue]}
t={this.t || t}
encrypted={encrypted}
navigation={navigation}
@ -1348,7 +1381,6 @@ class RoomView extends React.Component<IRoomViewProps, IRoomViewState> {
if ('id' in room) {
({ sysMes, bannerClosed, announcement, tunread, ignored } = room);
}
console.log({ room });
return (
<SafeAreaView style={{ backgroundColor: themes[theme].backgroundColor }} testID='room-view'>
<StatusBar />
@ -1409,7 +1441,8 @@ const mapStateToProps = (state: IApplicationState) => ({
baseUrl: state.server.server,
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[]
Hide_System_Messages: state.settings.Hide_System_Messages as string[],
transferLivechatGuestPermission: state.permissions['transfer-livechat-guest']
});
export default connect(mapStateToProps)(withDimensions(withTheme(withSafeAreaInsets(RoomView))));