2020-05-08 17:36:10 +00:00
|
|
|
import React, { useEffect, useState } from 'react';
|
2021-11-17 20:09:20 +00:00
|
|
|
import { StackNavigationProp } from '@react-navigation/stack';
|
|
|
|
import { RouteProp } from '@react-navigation/native';
|
2021-09-13 20:41:05 +00:00
|
|
|
import { StyleSheet, View } from 'react-native';
|
2021-11-17 20:09:20 +00:00
|
|
|
import { Dispatch } from 'redux';
|
2020-05-08 17:36:10 +00:00
|
|
|
import { connect } from 'react-redux';
|
2021-11-17 20:09:20 +00:00
|
|
|
import isEmpty from 'lodash/isEmpty';
|
2020-05-08 17:36:10 +00:00
|
|
|
|
|
|
|
import I18n from '../i18n';
|
|
|
|
import { withTheme } from '../theme';
|
|
|
|
import { themes } from '../constants/colors';
|
|
|
|
import RocketChat from '../lib/rocketchat';
|
|
|
|
import OrSeparator from '../containers/OrSeparator';
|
|
|
|
import Input from '../containers/UIKit/MultiSelect/Input';
|
|
|
|
import { forwardRoom as forwardRoomAction } from '../actions/room';
|
2021-11-17 20:09:20 +00:00
|
|
|
import { ILivechatDepartment } from './definition/ILivechatDepartment';
|
2020-05-08 17:36:10 +00:00
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
container: {
|
|
|
|
flex: 1,
|
|
|
|
padding: 16
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-11-17 20:09:20 +00:00
|
|
|
// TODO: Refactor when migrate room
|
|
|
|
interface IRoom {
|
|
|
|
departmentId?: any;
|
|
|
|
servedBy?: {
|
|
|
|
_id: string;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
interface ITransferData {
|
|
|
|
roomId: string;
|
|
|
|
userId?: string;
|
|
|
|
departmentId?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface IUser {
|
|
|
|
username: string;
|
|
|
|
_id: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface IParsedData {
|
|
|
|
label: string;
|
|
|
|
value: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface IForwardLivechatViewProps {
|
|
|
|
navigation: StackNavigationProp<any, 'ForwardLivechatView'>;
|
|
|
|
route: RouteProp<{ ForwardLivechatView: { rid: string } }, 'ForwardLivechatView'>;
|
|
|
|
theme: string;
|
|
|
|
forwardRoom: (rid: string, transferData: ITransferData) => void;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ForwardLivechatView = ({ forwardRoom, navigation, route, theme }: IForwardLivechatViewProps) => {
|
|
|
|
const [departments, setDepartments] = useState<IParsedData[]>([]);
|
|
|
|
const [departmentId, setDepartment] = useState('');
|
|
|
|
const [users, setUsers] = useState<IParsedData[]>([]);
|
2020-05-08 17:36:10 +00:00
|
|
|
const [userId, setUser] = useState();
|
2021-11-17 20:09:20 +00:00
|
|
|
const [room, setRoom] = useState<IRoom>({});
|
2020-05-08 17:36:10 +00:00
|
|
|
|
2020-06-15 14:00:46 +00:00
|
|
|
const rid = route.params?.rid;
|
2020-05-08 17:36:10 +00:00
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
const getDepartments = async () => {
|
2020-05-08 17:36:10 +00:00
|
|
|
try {
|
|
|
|
const result = await RocketChat.getDepartments();
|
|
|
|
if (result.success) {
|
2021-11-17 20:09:20 +00:00
|
|
|
setDepartments(
|
|
|
|
result.departments.map((department: ILivechatDepartment) => ({ label: department.name, value: department._id }))
|
|
|
|
);
|
2020-05-08 17:36:10 +00:00
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
const getUsers = async (term = '') => {
|
2020-05-08 17:36:10 +00:00
|
|
|
try {
|
|
|
|
const { servedBy: { _id: agentId } = {} } = room;
|
|
|
|
const _id = agentId && { $ne: agentId };
|
2021-09-13 20:41:05 +00:00
|
|
|
const result = await RocketChat.usersAutoComplete({
|
|
|
|
conditions: { _id, status: { $ne: 'offline' }, statusLivechat: 'available' },
|
|
|
|
term
|
|
|
|
});
|
2020-05-08 17:36:10 +00:00
|
|
|
if (result.success) {
|
2021-11-17 20:09:20 +00:00
|
|
|
const parsedUsers = result.items.map((user: IUser) => ({ label: user.username, value: user._id }));
|
2020-05-08 17:36:10 +00:00
|
|
|
setUsers(parsedUsers);
|
|
|
|
return parsedUsers;
|
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
return [];
|
|
|
|
};
|
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
const getRoom = async () => {
|
2020-05-08 17:36:10 +00:00
|
|
|
try {
|
|
|
|
const result = await RocketChat.getRoomInfo(rid);
|
|
|
|
if (result.success) {
|
|
|
|
setRoom(result.room);
|
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const submit = () => {
|
2021-11-17 20:09:20 +00:00
|
|
|
const transferData: ITransferData = { roomId: rid };
|
2020-05-08 17:36:10 +00:00
|
|
|
|
|
|
|
if (!departmentId && !userId) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (userId) {
|
|
|
|
transferData.userId = userId;
|
|
|
|
} else {
|
|
|
|
transferData.departmentId = departmentId;
|
|
|
|
}
|
|
|
|
|
|
|
|
forwardRoom(rid, transferData);
|
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
2021-11-17 20:09:20 +00:00
|
|
|
navigation.setOptions({
|
|
|
|
title: I18n.t('Forward_Chat')
|
|
|
|
});
|
2020-05-08 17:36:10 +00:00
|
|
|
getRoom();
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
useEffect(() => {
|
2021-11-17 20:09:20 +00:00
|
|
|
if (!isEmpty(room)) {
|
2020-05-08 17:36:10 +00:00
|
|
|
getUsers();
|
|
|
|
getDepartments();
|
|
|
|
}
|
|
|
|
}, [room]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (departmentId || userId) {
|
|
|
|
submit();
|
|
|
|
}
|
|
|
|
}, [departmentId, userId]);
|
|
|
|
|
|
|
|
const onPressDepartment = () => {
|
|
|
|
navigation.navigate('PickerView', {
|
|
|
|
title: I18n.t('Forward_to_department'),
|
|
|
|
value: room?.departmentId,
|
|
|
|
data: departments,
|
|
|
|
onChangeValue: setDepartment,
|
|
|
|
goBack: false
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const onPressUser = () => {
|
|
|
|
navigation.navigate('PickerView', {
|
|
|
|
title: I18n.t('Forward_to_user'),
|
|
|
|
data: users,
|
|
|
|
onChangeValue: setUser,
|
|
|
|
onChangeText: getUsers,
|
|
|
|
goBack: false
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<View style={[styles.container, { backgroundColor: themes[theme].auxiliaryBackground }]}>
|
2021-09-13 20:41:05 +00:00
|
|
|
<Input onPress={onPressDepartment} placeholder={I18n.t('Select_a_Department')} theme={theme} />
|
2020-05-08 17:36:10 +00:00
|
|
|
<OrSeparator theme={theme} />
|
2021-09-13 20:41:05 +00:00
|
|
|
<Input onPress={onPressUser} placeholder={I18n.t('Select_a_User')} theme={theme} />
|
2020-05-08 17:36:10 +00:00
|
|
|
</View>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2021-11-17 20:09:20 +00:00
|
|
|
const mapDispatchToProps = (dispatch: Dispatch) => ({
|
|
|
|
forwardRoom: (rid: string, transferData: ITransferData) => dispatch(forwardRoomAction(rid, transferData))
|
2020-05-08 17:36:10 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
export default connect(null, mapDispatchToProps)(withTheme(ForwardLivechatView));
|