Chore: Evaluate RoomInfoView - TypeScript (#4107)

* Chore: Evaluate RoomInfoView - TypeScript

* refactor interfaces
This commit is contained in:
Reinaldo Neto 2022-05-11 15:20:59 -03:00 committed by GitHub
parent fe84090703
commit 81c6ffce40
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 17 additions and 16 deletions

View File

@ -22,7 +22,7 @@ export interface IVisitorEmail {
export interface ILivechatVisitor extends IRocketChatRecord { export interface ILivechatVisitor extends IRocketChatRecord {
username: string; username: string;
ts: Date; ts: Date | string;
token: string; token: string;
department?: string; department?: string;
name?: string; name?: string;

View File

@ -2,7 +2,7 @@ import React from 'react';
import Item from './Item'; import Item from './Item';
const CustomFields = ({ customFields }: { customFields: { [key: string]: string } }) => { const CustomFields = ({ customFields }: { customFields?: { [key: string]: string } }) => {
if (customFields) { if (customFields) {
return ( return (
<> <>

View File

@ -7,8 +7,9 @@ import { useTheme } from '../../theme';
import Timezone from './Timezone'; import Timezone from './Timezone';
import CustomFields from './CustomFields'; import CustomFields from './CustomFields';
import styles from './styles'; import styles from './styles';
import { IUserParsed } from '.';
const Roles = ({ roles }: { roles: string[] }) => { const Roles = ({ roles }: { roles?: string[] }) => {
const { theme } = useTheme(); const { theme } = useTheme();
if (roles && roles.length) { if (roles && roles.length) {
@ -29,7 +30,7 @@ const Roles = ({ roles }: { roles: string[] }) => {
return null; return null;
}; };
const Direct = ({ roomUser }: { roomUser: any }) => ( const Direct = ({ roomUser }: { roomUser: IUserParsed }) => (
<> <>
<Roles roles={roomUser.parsedRoles} /> <Roles roles={roomUser.parsedRoles} />
<Timezone utcOffset={roomUser.utcOffset} /> <Timezone utcOffset={roomUser.utcOffset} />

View File

@ -93,7 +93,7 @@ interface IRoomInfoViewProps {
roles: { [key: string]: string }; roles: { [key: string]: string };
} }
interface IUserParsed extends IUser { export interface IUserParsed extends IUser {
parsedRoles?: string[]; parsedRoles?: string[];
} }
@ -104,8 +104,7 @@ export interface ILivechatVisitorModified extends ILivechatVisitor {
interface IRoomInfoViewState { interface IRoomInfoViewState {
room: ISubscription; room: ISubscription;
// TODO: Could be IUserParsed or ILivechatVisitorModified roomUser: IUserParsed | ILivechatVisitorModified;
roomUser: any;
showEdit: boolean; showEdit: boolean;
} }
@ -214,7 +213,7 @@ class RoomInfoView extends React.Component<IRoomInfoViewProps, IRoomInfoViewStat
params.os = `${ua.getOS().name} ${ua.getOS().version}`; params.os = `${ua.getOS().name} ${ua.getOS().version}`;
params.browser = `${ua.getBrowser().name} ${ua.getBrowser().version}`; params.browser = `${ua.getBrowser().name} ${ua.getBrowser().version}`;
} }
this.setState({ roomUser: { ...visitor, ...params } }, () => this.setHeader()); this.setState({ roomUser: { ...visitor, ...params } as ILivechatVisitorModified }, () => this.setHeader());
} }
} }
} catch (error) { } catch (error) {
@ -245,14 +244,14 @@ class RoomInfoView extends React.Component<IRoomInfoViewProps, IRoomInfoViewStat
parsedRoles.parsedRoles = await this.parseRoles(roles); parsedRoles.parsedRoles = await this.parseRoles(roles);
} }
this.setState({ roomUser: { ...user, ...parsedRoles } }); this.setState({ roomUser: { ...user, ...parsedRoles } as IUserParsed });
} }
} catch { } catch {
// do nothing // do nothing
} }
} else { } else {
try { try {
const { roles } = roomUser; const { roles } = roomUser as IUserParsed;
if (roles && roles.length) { if (roles && roles.length) {
const parsedRoles = await this.parseRoles(roles); const parsedRoles = await this.parseRoles(roles);
this.setState({ roomUser: { ...roomUser, parsedRoles } }); this.setState({ roomUser: { ...roomUser, parsedRoles } });
@ -412,11 +411,11 @@ class RoomInfoView extends React.Component<IRoomInfoViewProps, IRoomInfoViewStat
const { room, roomUser } = this.state; const { room, roomUser } = this.state;
if (this.isDirect) { if (this.isDirect) {
return <Direct roomUser={roomUser} />; return <Direct roomUser={roomUser as IUserParsed} />;
} }
if (this.t === SubscriptionType.OMNICHANNEL) { if (this.t === SubscriptionType.OMNICHANNEL) {
return <Livechat room={room} roomUser={roomUser} />; return <Livechat room={room} roomUser={roomUser as ILivechatVisitorModified} />;
} }
return <Channel room={room} />; return <Channel room={room} />;
}; };
@ -424,20 +423,21 @@ class RoomInfoView extends React.Component<IRoomInfoViewProps, IRoomInfoViewStat
render() { render() {
const { room, roomUser } = this.state; const { room, roomUser } = this.state;
const { theme } = this.props; const { theme } = this.props;
const roomUserParsed = roomUser as IUserParsed;
return ( return (
<ScrollView style={[styles.scroll, { backgroundColor: themes[theme].backgroundColor }]}> <ScrollView style={[styles.scroll, { backgroundColor: themes[theme].backgroundColor }]}>
<StatusBar /> <StatusBar />
<SafeAreaView style={{ backgroundColor: themes[theme].backgroundColor }} testID='room-info-view'> <SafeAreaView style={{ backgroundColor: themes[theme].backgroundColor }} testID='room-info-view'>
<View style={[styles.avatarContainer, { backgroundColor: themes[theme].auxiliaryBackground }]}> <View style={[styles.avatarContainer, { backgroundColor: themes[theme].auxiliaryBackground }]}>
{this.renderAvatar(room, roomUser)} {this.renderAvatar(room, roomUserParsed)}
<View style={styles.roomTitleContainer}> <View style={styles.roomTitleContainer}>
{renderRoomTitle({ {renderRoomTitle({
room, room,
type: this.t, type: this.t,
name: roomUser?.name, name: roomUserParsed?.name,
username: roomUser?.username, username: roomUserParsed?.username,
statusText: roomUser?.statusText, statusText: roomUserParsed?.statusText,
theme theme
})} })}
</View> </View>