verdnatura-chat/app/views/ForwardLivechatView.js

151 lines
3.5 KiB
JavaScript
Raw Normal View History

[NEW] Livechat (#2004) * [WIP][NEW] Livechat info/actions * [IMPROVEMENT] RoomActionsView * [NEW] Visitor Navigation * [NEW] Get Department REST * [FIX] Borders * [IMPROVEMENT] Refactor RoomInfo View * [FIX] Error while navigate from mention -> roomInfo * [NEW] Livechat Fields * [NEW] Close Livechat * [WIP] Forward livechat * [NEW] Return inquiry * [WIP] Comment when close livechat * [WIP] Improve roomInfo * [IMPROVEMENT] Forward room * [FIX] Department picker * [FIX] Picker without results * [FIX] Superfluous argument * [FIX] Check permissions on RoomActionsView * [FIX] Livechat permissions * [WIP] Show edit to livechat * [I18N] Add pt-br translations * [WIP] Livechat Info * [IMPROVEMENT] Livechat info * [WIP] Livechat Edit * [WIP] Livechat edit * [WIP] Livechat Edit * [WIP] Livechat edit scroll * [FIX] Edit customFields * [FIX] Clean livechat customField * [FIX] Visitor Navigation * [NEW] Next input logic LivechatEdit * [FIX] Add livechat data to subscription * [FIX] Revert change * [NEW] Livechat user Status * [WIP] Livechat tags * [NEW] Edit livechat tags * [FIX] Prevent some crashes * [FIX] Forward * [FIX] Return Livechat error * [FIX] Prevent livechat info crash * [IMPROVEMENT] Use input style on forward chat * OnboardingSeparator -> OrSeparator * [FIX] Go to next input * [NEW] Added some icons * [NEW] Livechat close * [NEW] Forward Room Action * [FIX] Livechat edit style * [FIX] Change status logic * [CHORE] Remove unnecessary logic * [CHORE] Remove unnecessary code * [CHORE] Remove unecessary case * [FIX] Superfluous argument * [IMPROVEMENT] Submit livechat edit * [CHORE] Remove textInput type * [FIX] Livechat edit * [FIX] Livechat Edit * [FIX] Use same effect * [IMPROVEMENT] Tags input * [FIX] Add empty tag * Fix minor issues * Fix typo * insert livechat room data to our room object * review * add method calls server version Co-authored-by: Diego Mello <diegolmello@gmail.com>
2020-05-08 17:36:10 +00:00
import React, { useEffect, useState } from 'react';
import PropTypes from 'prop-types';
import { View, StyleSheet } from 'react-native';
import { connect } from 'react-redux';
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';
const styles = StyleSheet.create({
container: {
flex: 1,
padding: 16
}
});
const ForwardLivechatView = ({ forwardRoom, navigation, theme }) => {
const [departments, setDepartments] = useState([]);
const [departmentId, setDepartment] = useState();
const [users, setUsers] = useState([]);
const [userId, setUser] = useState();
const [room, setRoom] = useState();
const rid = navigation.getParam('rid');
const getDepartments = async() => {
try {
const result = await RocketChat.getDepartments();
if (result.success) {
setDepartments(result.departments.map(department => ({ label: department.name, value: department._id })));
}
} catch {
// do nothing
}
};
const getUsers = async(term = '') => {
try {
const { servedBy: { _id: agentId } = {} } = room;
const _id = agentId && { $ne: agentId };
const result = await RocketChat.usersAutoComplete({ conditions: { _id, status: { $ne: 'offline' }, statusLivechat: 'available' }, term });
if (result.success) {
const parsedUsers = result.items.map(user => ({ label: user.username, value: user._id }));
setUsers(parsedUsers);
return parsedUsers;
}
} catch {
// do nothing
}
return [];
};
const getRoom = async() => {
try {
const result = await RocketChat.getRoomInfo(rid);
if (result.success) {
setRoom(result.room);
}
} catch {
// do nothing
}
};
const submit = () => {
const transferData = { roomId: rid };
if (!departmentId && !userId) {
return;
}
if (userId) {
transferData.userId = userId;
} else {
transferData.departmentId = departmentId;
}
forwardRoom(rid, transferData);
};
useEffect(() => {
getRoom();
}, []);
useEffect(() => {
if (room) {
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 }]}>
<Input
onPress={onPressDepartment}
placeholder={I18n.t('Select_a_Department')}
theme={theme}
/>
<OrSeparator theme={theme} />
<Input
onPress={onPressUser}
placeholder={I18n.t('Select_a_User')}
theme={theme}
/>
</View>
);
};
ForwardLivechatView.propTypes = {
forwardRoom: PropTypes.func,
navigation: PropTypes.object,
theme: PropTypes.string
};
ForwardLivechatView.navigationOptions = {
title: I18n.t('Forward_Chat')
};
const mapDispatchToProps = dispatch => ({
forwardRoom: (rid, transferData) => dispatch(forwardRoomAction(rid, transferData))
});
export default connect(null, mapDispatchToProps)(withTheme(ForwardLivechatView));