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

View File

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