2022-02-18 13:46:19 +00:00
|
|
|
import isEmpty from 'lodash/isEmpty';
|
2020-05-08 17:36:10 +00:00
|
|
|
import React, { useEffect, useState } from 'react';
|
2021-09-13 20:41:05 +00:00
|
|
|
import { StyleSheet, View } from 'react-native';
|
2022-02-18 13:46:19 +00:00
|
|
|
import { useDispatch } from 'react-redux';
|
2020-05-08 17:36:10 +00:00
|
|
|
|
2022-02-18 13:46:19 +00:00
|
|
|
import { forwardRoom, ITransferData } from '../actions/room';
|
2020-05-08 17:36:10 +00:00
|
|
|
import { themes } from '../constants/colors';
|
|
|
|
import OrSeparator from '../containers/OrSeparator';
|
|
|
|
import Input from '../containers/UIKit/MultiSelect/Input';
|
2022-02-18 13:46:19 +00:00
|
|
|
import { IBaseScreen, IRoom } from '../definitions';
|
|
|
|
import I18n from '../i18n';
|
|
|
|
import RocketChat from '../lib/rocketchat';
|
2021-12-03 19:27:57 +00:00
|
|
|
import { ChatsStackParamList } from '../stacks/types';
|
2022-02-18 13:46:19 +00:00
|
|
|
import { withTheme } from '../theme';
|
2022-02-17 14:57:30 +00:00
|
|
|
import { IOptionsField } from './NotificationPreferencesView/options';
|
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
|
|
|
interface IUser {
|
|
|
|
username: string;
|
|
|
|
_id: string;
|
|
|
|
}
|
2022-02-18 13:46:19 +00:00
|
|
|
|
|
|
|
interface IParsedData {
|
|
|
|
label: string;
|
|
|
|
value: string;
|
2021-11-17 20:09:20 +00:00
|
|
|
}
|
|
|
|
|
2022-02-17 14:57:30 +00:00
|
|
|
const COUNT_DEPARTMENT = 50;
|
|
|
|
|
2022-02-18 13:46:19 +00:00
|
|
|
const ForwardLivechatView = ({ navigation, route, theme }: IBaseScreen<ChatsStackParamList, 'ForwardLivechatView'>) => {
|
|
|
|
const [departments, setDepartments] = useState<IParsedData[]>([]);
|
2021-11-17 20:09:20 +00:00
|
|
|
const [departmentId, setDepartment] = useState('');
|
2022-02-17 14:57:30 +00:00
|
|
|
const [departmentTotal, setDepartmentTotal] = useState(0);
|
|
|
|
const [users, setUsers] = useState<IOptionsField[]>([]);
|
2020-05-08 17:36:10 +00:00
|
|
|
const [userId, setUser] = useState();
|
2022-02-17 14:57:30 +00:00
|
|
|
const [room, setRoom] = useState<IRoom>({} as IRoom);
|
2022-02-18 13:46:19 +00:00
|
|
|
const dispatch = useDispatch();
|
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
|
|
|
|
2022-02-17 14:57:30 +00:00
|
|
|
const getDepartments = async (text = '', offset = 0) => {
|
2020-05-08 17:36:10 +00:00
|
|
|
try {
|
2022-02-17 14:57:30 +00:00
|
|
|
const result = await RocketChat.getDepartments({ count: COUNT_DEPARTMENT, text, offset });
|
2020-05-08 17:36:10 +00:00
|
|
|
if (result.success) {
|
2022-02-18 13:46:19 +00:00
|
|
|
const parsedDepartments: IParsedData[] = result.departments.map(department => ({
|
2022-02-17 14:57:30 +00:00
|
|
|
label: department.name,
|
|
|
|
value: department._id
|
|
|
|
}));
|
|
|
|
if (!text && !offset) {
|
|
|
|
setDepartments(parsedDepartments);
|
|
|
|
setDepartmentTotal(result?.total);
|
|
|
|
}
|
|
|
|
return { data: parsedDepartments, total: result?.total, offset: result?.offset };
|
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 };
|
2022-02-17 14:57:30 +00:00
|
|
|
const result = await RocketChat.usersAutoComplete({
|
2021-09-13 20:41:05 +00:00
|
|
|
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 }));
|
2022-02-17 14:57:30 +00:00
|
|
|
if (!term) {
|
|
|
|
setUsers(parsedUsers);
|
|
|
|
}
|
|
|
|
return { data: parsedUsers };
|
2020-05-08 17:36:10 +00:00
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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) {
|
2022-02-17 14:57:30 +00:00
|
|
|
setRoom(result.room as IRoom);
|
2020-05-08 17:36:10 +00:00
|
|
|
}
|
|
|
|
} 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;
|
|
|
|
}
|
|
|
|
|
2022-02-18 13:46:19 +00:00
|
|
|
dispatch(forwardRoom(rid, transferData));
|
2020-05-08 17:36:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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,
|
2022-02-17 14:57:30 +00:00
|
|
|
onSearch: getDepartments,
|
|
|
|
onEndReached: getDepartments,
|
|
|
|
total: departmentTotal,
|
2020-05-08 17:36:10 +00:00
|
|
|
goBack: false
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const onPressUser = () => {
|
|
|
|
navigation.navigate('PickerView', {
|
|
|
|
title: I18n.t('Forward_to_user'),
|
|
|
|
data: users,
|
|
|
|
onChangeValue: setUser,
|
2022-02-17 14:57:30 +00:00
|
|
|
onSearch: getUsers,
|
2020-05-08 17:36:10 +00:00
|
|
|
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>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-02-18 13:46:19 +00:00
|
|
|
export default withTheme(ForwardLivechatView);
|