Chore: Migrate ForwardLivechatView to Typescript (#3497)

Co-authored-by: AlexAlexandre <alexalexandrejr@gmail.com>
Co-authored-by: Diego Mello <diegolmello@gmail.com>
This commit is contained in:
Reinaldo Neto 2021-11-17 17:09:20 -03:00 committed by GitHub
parent da21f58293
commit 7715f93faf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 69 additions and 23 deletions

View File

@ -8,10 +8,10 @@ import ActivityIndicator from '../../ActivityIndicator';
import styles from './styles'; import styles from './styles';
interface IInput { interface IInput {
children: JSX.Element; children?: JSX.Element;
onPress: Function; onPress: Function;
theme: string; theme: string;
inputStyle: object; inputStyle?: object;
disabled?: boolean | object; disabled?: boolean | object;
placeholder?: string; placeholder?: string;
loading?: boolean; loading?: boolean;

View File

@ -1,7 +1,10 @@
import React, { useEffect, useState } from 'react'; import React, { useEffect, useState } from 'react';
import PropTypes from 'prop-types'; import { StackNavigationProp } from '@react-navigation/stack';
import { RouteProp } from '@react-navigation/native';
import { StyleSheet, View } from 'react-native'; import { StyleSheet, View } from 'react-native';
import { Dispatch } from 'redux';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import isEmpty from 'lodash/isEmpty';
import I18n from '../i18n'; import I18n from '../i18n';
import { withTheme } from '../theme'; import { withTheme } from '../theme';
@ -10,6 +13,7 @@ import RocketChat from '../lib/rocketchat';
import OrSeparator from '../containers/OrSeparator'; import OrSeparator from '../containers/OrSeparator';
import Input from '../containers/UIKit/MultiSelect/Input'; import Input from '../containers/UIKit/MultiSelect/Input';
import { forwardRoom as forwardRoomAction } from '../actions/room'; import { forwardRoom as forwardRoomAction } from '../actions/room';
import { ILivechatDepartment } from './definition/ILivechatDepartment';
const styles = StyleSheet.create({ const styles = StyleSheet.create({
container: { container: {
@ -18,12 +22,43 @@ const styles = StyleSheet.create({
} }
}); });
const ForwardLivechatView = ({ forwardRoom, navigation, route, theme }) => { // TODO: Refactor when migrate room
const [departments, setDepartments] = useState([]); interface IRoom {
const [departmentId, setDepartment] = useState(); departmentId?: any;
const [users, setUsers] = useState([]); 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[]>([]);
const [userId, setUser] = useState(); const [userId, setUser] = useState();
const [room, setRoom] = useState(); const [room, setRoom] = useState<IRoom>({});
const rid = route.params?.rid; const rid = route.params?.rid;
@ -31,7 +66,9 @@ const ForwardLivechatView = ({ forwardRoom, navigation, route, theme }) => {
try { try {
const result = await RocketChat.getDepartments(); const result = await RocketChat.getDepartments();
if (result.success) { if (result.success) {
setDepartments(result.departments.map(department => ({ label: department.name, value: department._id }))); setDepartments(
result.departments.map((department: ILivechatDepartment) => ({ label: department.name, value: department._id }))
);
} }
} catch { } catch {
// do nothing // do nothing
@ -47,7 +84,7 @@ const ForwardLivechatView = ({ forwardRoom, navigation, route, theme }) => {
term term
}); });
if (result.success) { if (result.success) {
const parsedUsers = result.items.map(user => ({ label: user.username, value: user._id })); const parsedUsers = result.items.map((user: IUser) => ({ label: user.username, value: user._id }));
setUsers(parsedUsers); setUsers(parsedUsers);
return parsedUsers; return parsedUsers;
} }
@ -69,7 +106,7 @@ const ForwardLivechatView = ({ forwardRoom, navigation, route, theme }) => {
}; };
const submit = () => { const submit = () => {
const transferData = { roomId: rid }; const transferData: ITransferData = { roomId: rid };
if (!departmentId && !userId) { if (!departmentId && !userId) {
return; return;
@ -85,11 +122,14 @@ const ForwardLivechatView = ({ forwardRoom, navigation, route, theme }) => {
}; };
useEffect(() => { useEffect(() => {
navigation.setOptions({
title: I18n.t('Forward_Chat')
});
getRoom(); getRoom();
}, []); }, []);
useEffect(() => { useEffect(() => {
if (room) { if (!isEmpty(room)) {
getUsers(); getUsers();
getDepartments(); getDepartments();
} }
@ -129,18 +169,9 @@ const ForwardLivechatView = ({ forwardRoom, navigation, route, theme }) => {
</View> </View>
); );
}; };
ForwardLivechatView.propTypes = {
forwardRoom: PropTypes.func,
navigation: PropTypes.object,
route: PropTypes.object,
theme: PropTypes.string
};
ForwardLivechatView.navigationOptions = {
title: I18n.t('Forward_Chat')
};
const mapDispatchToProps = dispatch => ({ const mapDispatchToProps = (dispatch: Dispatch) => ({
forwardRoom: (rid, transferData) => dispatch(forwardRoomAction(rid, transferData)) forwardRoom: (rid: string, transferData: ITransferData) => dispatch(forwardRoomAction(rid, transferData))
}); });
export default connect(null, mapDispatchToProps)(withTheme(ForwardLivechatView)); export default connect(null, mapDispatchToProps)(withTheme(ForwardLivechatView));

View File

@ -0,0 +1,15 @@
export interface ILivechatDepartment {
_id: string;
name: string;
enabled: boolean;
description: string;
showOnRegistration: boolean;
showOnOfflineForm: boolean;
requestTagBeforeClosingChat: boolean;
email: string;
chatClosingTags: string[];
offlineMessageChannelName: string;
numAgents: number;
_updatedAt?: Date;
businessHourId?: string;
}