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 OrSeparator from '../containers/OrSeparator';
|
|
|
|
import Input from '../containers/UIKit/MultiSelect/Input';
|
2023-06-30 14:01:01 +00:00
|
|
|
import { IServerRoom } from '../definitions';
|
2022-02-18 13:46:19 +00:00
|
|
|
import I18n from '../i18n';
|
2023-06-30 14:01:01 +00:00
|
|
|
import { useAppNavigation, useAppRoute } from '../lib/hooks/navigation';
|
|
|
|
import { Services } from '../lib/services';
|
|
|
|
import { TNavigation } from '../stacks/stackType';
|
2022-05-02 12:03:55 +00:00
|
|
|
import { useTheme } 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
|
|
|
|
}
|
|
|
|
});
|
2022-02-18 13:46:19 +00:00
|
|
|
|
2022-02-17 14:57:30 +00:00
|
|
|
const COUNT_DEPARTMENT = 50;
|
|
|
|
|
2023-06-30 14:01:01 +00:00
|
|
|
const ForwardLivechatView = (): React.ReactElement => {
|
|
|
|
const { navigate, setOptions } = useAppNavigation<TNavigation, 'PickerView'>();
|
|
|
|
const {
|
|
|
|
params: { rid }
|
|
|
|
} = useAppRoute<TNavigation, 'ForwardLivechatView'>();
|
|
|
|
const [departments, setDepartments] = useState<IOptionsField[]>([]);
|
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-05-02 12:03:55 +00:00
|
|
|
const [room, setRoom] = useState({} as IServerRoom);
|
2022-02-18 13:46:19 +00:00
|
|
|
const dispatch = useDispatch();
|
2022-05-02 12:03:55 +00:00
|
|
|
const { theme, colors } = useTheme();
|
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-04-28 20:37:25 +00:00
|
|
|
const result = await Services.getDepartments({ count: COUNT_DEPARTMENT, text, offset });
|
2020-05-08 17:36:10 +00:00
|
|
|
if (result.success) {
|
2022-03-07 15:32:51 +00:00
|
|
|
const parsedDepartments = 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-04-28 20:37:25 +00:00
|
|
|
const result = await Services.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) {
|
2022-03-07 15:32:51 +00:00
|
|
|
const parsedUsers = result.items.map(user => ({ 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 {
|
2022-04-28 20:37:25 +00:00
|
|
|
const result = await Services.getRoomInfo(rid);
|
2020-05-08 17:36:10 +00:00
|
|
|
if (result.success) {
|
2022-03-08 16:25:27 +00:00
|
|
|
setRoom(result.room as IServerRoom);
|
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(() => {
|
2023-06-30 14:01:01 +00:00
|
|
|
setOptions({
|
2021-11-17 20:09:20 +00:00
|
|
|
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 = () => {
|
2023-06-30 14:01:01 +00:00
|
|
|
navigate('PickerView', {
|
2020-05-08 17:36:10 +00:00
|
|
|
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,
|
2023-06-30 14:01:01 +00:00
|
|
|
total: departmentTotal
|
2020-05-08 17:36:10 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const onPressUser = () => {
|
2023-06-30 14:01:01 +00:00
|
|
|
navigate('PickerView', {
|
2020-05-08 17:36:10 +00:00
|
|
|
title: I18n.t('Forward_to_user'),
|
|
|
|
data: users,
|
|
|
|
onChangeValue: setUser,
|
2023-06-30 14:01:01 +00:00
|
|
|
onSearch: getUsers
|
2020-05-08 17:36:10 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2022-05-02 12:03:55 +00:00
|
|
|
<View style={[styles.container, { backgroundColor: colors.auxiliaryBackground }]}>
|
2022-07-13 19:31:58 +00:00
|
|
|
<Input onPress={onPressDepartment} placeholder={I18n.t('Select_a_Department')} />
|
2020-05-08 17:36:10 +00:00
|
|
|
<OrSeparator theme={theme} />
|
2022-07-13 19:31:58 +00:00
|
|
|
<Input onPress={onPressUser} placeholder={I18n.t('Select_a_User')} />
|
2020-05-08 17:36:10 +00:00
|
|
|
</View>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-05-02 12:03:55 +00:00
|
|
|
export default ForwardLivechatView;
|