From 7715f93fafdaf6cb84f7a7ff3b5283405798e905 Mon Sep 17 00:00:00 2001 From: Reinaldo Neto <47038980+reinaldonetof@users.noreply.github.com> Date: Wed, 17 Nov 2021 17:09:20 -0300 Subject: [PATCH] Chore: Migrate ForwardLivechatView to Typescript (#3497) Co-authored-by: AlexAlexandre Co-authored-by: Diego Mello --- app/containers/UIKit/MultiSelect/Input.tsx | 4 +- ...ivechatView.js => ForwardLivechatView.tsx} | 73 +++++++++++++------ app/views/definition/ILivechatDepartment.ts | 15 ++++ 3 files changed, 69 insertions(+), 23 deletions(-) rename app/views/{ForwardLivechatView.js => ForwardLivechatView.tsx} (62%) create mode 100644 app/views/definition/ILivechatDepartment.ts diff --git a/app/containers/UIKit/MultiSelect/Input.tsx b/app/containers/UIKit/MultiSelect/Input.tsx index e03837a2b..ed4835c5e 100644 --- a/app/containers/UIKit/MultiSelect/Input.tsx +++ b/app/containers/UIKit/MultiSelect/Input.tsx @@ -8,10 +8,10 @@ import ActivityIndicator from '../../ActivityIndicator'; import styles from './styles'; interface IInput { - children: JSX.Element; + children?: JSX.Element; onPress: Function; theme: string; - inputStyle: object; + inputStyle?: object; disabled?: boolean | object; placeholder?: string; loading?: boolean; diff --git a/app/views/ForwardLivechatView.js b/app/views/ForwardLivechatView.tsx similarity index 62% rename from app/views/ForwardLivechatView.js rename to app/views/ForwardLivechatView.tsx index 4f61272d4..42a29782d 100644 --- a/app/views/ForwardLivechatView.js +++ b/app/views/ForwardLivechatView.tsx @@ -1,7 +1,10 @@ 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 { Dispatch } from 'redux'; import { connect } from 'react-redux'; +import isEmpty from 'lodash/isEmpty'; import I18n from '../i18n'; import { withTheme } from '../theme'; @@ -10,6 +13,7 @@ import RocketChat from '../lib/rocketchat'; import OrSeparator from '../containers/OrSeparator'; import Input from '../containers/UIKit/MultiSelect/Input'; import { forwardRoom as forwardRoomAction } from '../actions/room'; +import { ILivechatDepartment } from './definition/ILivechatDepartment'; const styles = StyleSheet.create({ container: { @@ -18,12 +22,43 @@ const styles = StyleSheet.create({ } }); -const ForwardLivechatView = ({ forwardRoom, navigation, route, theme }) => { - const [departments, setDepartments] = useState([]); - const [departmentId, setDepartment] = useState(); - const [users, setUsers] = useState([]); +// 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; + 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([]); + const [departmentId, setDepartment] = useState(''); + const [users, setUsers] = useState([]); const [userId, setUser] = useState(); - const [room, setRoom] = useState(); + const [room, setRoom] = useState({}); const rid = route.params?.rid; @@ -31,7 +66,9 @@ const ForwardLivechatView = ({ forwardRoom, navigation, route, theme }) => { try { const result = await RocketChat.getDepartments(); 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 { // do nothing @@ -47,7 +84,7 @@ const ForwardLivechatView = ({ forwardRoom, navigation, route, theme }) => { term }); 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); return parsedUsers; } @@ -69,7 +106,7 @@ const ForwardLivechatView = ({ forwardRoom, navigation, route, theme }) => { }; const submit = () => { - const transferData = { roomId: rid }; + const transferData: ITransferData = { roomId: rid }; if (!departmentId && !userId) { return; @@ -85,11 +122,14 @@ const ForwardLivechatView = ({ forwardRoom, navigation, route, theme }) => { }; useEffect(() => { + navigation.setOptions({ + title: I18n.t('Forward_Chat') + }); getRoom(); }, []); useEffect(() => { - if (room) { + if (!isEmpty(room)) { getUsers(); getDepartments(); } @@ -129,18 +169,9 @@ const ForwardLivechatView = ({ forwardRoom, navigation, route, theme }) => { ); }; -ForwardLivechatView.propTypes = { - forwardRoom: PropTypes.func, - navigation: PropTypes.object, - route: PropTypes.object, - theme: PropTypes.string -}; -ForwardLivechatView.navigationOptions = { - title: I18n.t('Forward_Chat') -}; -const mapDispatchToProps = dispatch => ({ - forwardRoom: (rid, transferData) => dispatch(forwardRoomAction(rid, transferData)) +const mapDispatchToProps = (dispatch: Dispatch) => ({ + forwardRoom: (rid: string, transferData: ITransferData) => dispatch(forwardRoomAction(rid, transferData)) }); export default connect(null, mapDispatchToProps)(withTheme(ForwardLivechatView)); diff --git a/app/views/definition/ILivechatDepartment.ts b/app/views/definition/ILivechatDepartment.ts new file mode 100644 index 000000000..22c352007 --- /dev/null +++ b/app/views/definition/ILivechatDepartment.ts @@ -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; +}