Chore: Migrate NewMessageView to Typescript (#3502)

This commit is contained in:
Reinaldo Neto 2021-12-02 10:20:19 -03:00 committed by GitHub
parent f69b82dae9
commit fa07bbec2f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 61 additions and 42 deletions

View File

@ -46,7 +46,7 @@ interface IUserItem {
testID: string; testID: string;
onLongPress?: () => void; onLongPress?: () => void;
style?: StyleProp<ViewStyle>; style?: StyleProp<ViewStyle>;
icon: string; icon?: string;
theme: string; theme: string;
} }

View File

@ -1,11 +1,12 @@
import React from 'react'; import React from 'react';
import PropTypes from 'prop-types'; import { StackNavigationOptions, StackNavigationProp } from '@react-navigation/stack';
import { FlatList, StyleSheet, Text, View } from 'react-native'; import { FlatList, StyleSheet, Text, View } from 'react-native';
import { Dispatch } from 'redux';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import { Q } from '@nozbe/watermelondb'; import { Q } from '@nozbe/watermelondb';
import { dequal } from 'dequal'; import { dequal } from 'dequal';
import * as List from '../containers/List';
import * as List from '../containers/List';
import Touch from '../utils/touch'; import Touch from '../utils/touch';
import database from '../lib/database'; import database from '../lib/database';
import RocketChat from '../lib/rocketchat'; import RocketChat from '../lib/rocketchat';
@ -18,7 +19,6 @@ import * as HeaderButton from '../containers/HeaderButton';
import StatusBar from '../containers/StatusBar'; import StatusBar from '../containers/StatusBar';
import { themes } from '../constants/colors'; import { themes } from '../constants/colors';
import { withTheme } from '../theme'; import { withTheme } from '../theme';
import { getUserSelector } from '../selectors/login';
import Navigation from '../lib/Navigation'; import Navigation from '../lib/Navigation';
import { createChannelRequest } from '../actions/createChannel'; import { createChannelRequest } from '../actions/createChannel';
import { goRoom } from '../utils/goRoom'; import { goRoom } from '../utils/goRoom';
@ -47,33 +47,54 @@ const styles = StyleSheet.create({
} }
}); });
class NewMessageView extends React.Component { interface IButton {
static navigationOptions = ({ navigation }) => ({ onPress: () => void;
testID: string;
title: string;
icon: string;
first?: boolean;
}
interface ISearch {
_id: string;
status: string;
username: string;
avatarETag: string;
outside: boolean;
rid: string;
name: string;
t: string;
search: boolean;
}
interface INewMessageViewState {
search: ISearch[];
// TODO: Refactor when migrate room
chats: any[];
permissions: boolean[];
}
interface INewMessageViewProps {
navigation: StackNavigationProp<any, 'NewMessageView'>;
create: (params: { group: boolean }) => void;
maxUsers: number;
theme: string;
isMasterDetail: boolean;
serverVersion: string;
createTeamPermission: string[];
createDirectMessagePermission: string[];
createPublicChannelPermission: string[];
createPrivateChannelPermission: string[];
createDiscussionPermission: string[];
}
class NewMessageView extends React.Component<INewMessageViewProps, INewMessageViewState> {
static navigationOptions = ({ navigation }: INewMessageViewProps): StackNavigationOptions => ({
headerLeft: () => <HeaderButton.CloseModal navigation={navigation} testID='new-message-view-close' />, headerLeft: () => <HeaderButton.CloseModal navigation={navigation} testID='new-message-view-close' />,
title: I18n.t('New_Message') title: I18n.t('New_Message')
}); });
static propTypes = { constructor(props: INewMessageViewProps) {
navigation: PropTypes.object,
baseUrl: PropTypes.string,
user: PropTypes.shape({
id: PropTypes.string,
token: PropTypes.string,
roles: PropTypes.array
}),
create: PropTypes.func,
maxUsers: PropTypes.number,
theme: PropTypes.string,
isMasterDetail: PropTypes.bool,
serverVersion: PropTypes.string,
createTeamPermission: PropTypes.array,
createDirectMessagePermission: PropTypes.array,
createPublicChannelPermission: PropTypes.array,
createPrivateChannelPermission: PropTypes.array,
createDiscussionPermission: PropTypes.array
};
constructor(props) {
super(props); super(props);
this.init(); this.init();
this.state = { this.state = {
@ -102,7 +123,7 @@ class NewMessageView extends React.Component {
this.handleHasPermission(); this.handleHasPermission();
} }
componentDidUpdate(prevProps) { componentDidUpdate(prevProps: INewMessageViewProps) {
const { const {
createTeamPermission, createTeamPermission,
createPublicChannelPermission, createPublicChannelPermission,
@ -122,7 +143,7 @@ class NewMessageView extends React.Component {
} }
} }
onSearchChangeText(text) { onSearchChangeText(text: string) {
this.search(text); this.search(text);
} }
@ -131,7 +152,7 @@ class NewMessageView extends React.Component {
return navigation.pop(); return navigation.pop();
}; };
search = async text => { search = async (text: string) => {
const result = await RocketChat.search({ text, filterRooms: false }); const result = await RocketChat.search({ text, filterRooms: false });
this.setState({ this.setState({
search: result search: result
@ -162,7 +183,8 @@ class NewMessageView extends React.Component {
}); });
}; };
goRoom = item => { // TODO: Refactor when migrate room
goRoom = (item: any) => {
logEvent(events.NEW_MSG_CHAT_WITH_USER); logEvent(events.NEW_MSG_CHAT_WITH_USER);
const { isMasterDetail, navigation } = this.props; const { isMasterDetail, navigation } = this.props;
if (isMasterDetail) { if (isMasterDetail) {
@ -171,7 +193,7 @@ class NewMessageView extends React.Component {
goRoom({ item, isMasterDetail }); goRoom({ item, isMasterDetail });
}; };
renderButton = ({ onPress, testID, title, icon, first }) => { renderButton = ({ onPress, testID, title, icon, first }: IButton) => {
const { theme } = this.props; const { theme } = this.props;
return ( return (
<Touch onPress={onPress} style={{ backgroundColor: themes[theme].backgroundColor }} testID={testID} theme={theme}> <Touch onPress={onPress} style={{ backgroundColor: themes[theme].backgroundColor }} testID={testID} theme={theme}>
@ -218,7 +240,7 @@ class NewMessageView extends React.Component {
return ( return (
<View style={{ backgroundColor: themes[theme].auxiliaryBackground }}> <View style={{ backgroundColor: themes[theme].auxiliaryBackground }}>
<SearchBox onChangeText={text => this.onSearchChangeText(text)} testID='new-message-view-search' /> <SearchBox onChangeText={(text: string) => this.onSearchChangeText(text)} testID='new-message-view-search' />
<View style={styles.buttonContainer}> <View style={styles.buttonContainer}>
{permissions[0] || permissions[1] {permissions[0] || permissions[1]
? this.renderButton({ ? this.renderButton({
@ -258,9 +280,10 @@ class NewMessageView extends React.Component {
); );
}; };
renderItem = ({ item, index }) => { // TODO: Refactor when migrate room
renderItem = ({ item, index }: { item: ISearch | any; index: number }) => {
const { search, chats } = this.state; const { search, chats } = this.state;
const { baseUrl, user, theme } = this.props; const { theme } = this.props;
let style = { borderColor: themes[theme].separatorColor }; let style = { borderColor: themes[theme].separatorColor };
if (index === 0) { if (index === 0) {
@ -277,10 +300,8 @@ class NewMessageView extends React.Component {
name={item.search ? item.name : item.fname} name={item.search ? item.name : item.fname}
username={item.search ? item.username : item.name} username={item.search ? item.username : item.name}
onPress={() => this.goRoom(item)} onPress={() => this.goRoom(item)}
baseUrl={baseUrl}
testID={`new-message-view-item-${item.name}`} testID={`new-message-view-item-${item.name}`}
style={style} style={style}
user={user}
theme={theme} theme={theme}
/> />
); );
@ -313,12 +334,10 @@ class NewMessageView extends React.Component {
} }
} }
const mapStateToProps = state => ({ const mapStateToProps = (state: any) => ({
serverVersion: state.server.version, serverVersion: state.server.version,
isMasterDetail: state.app.isMasterDetail, isMasterDetail: state.app.isMasterDetail,
baseUrl: state.server.server,
maxUsers: state.settings.DirectMesssage_maxUsers || 1, maxUsers: state.settings.DirectMesssage_maxUsers || 1,
user: getUserSelector(state),
createTeamPermission: state.permissions['create-team'], createTeamPermission: state.permissions['create-team'],
createDirectMessagePermission: state.permissions['create-d'], createDirectMessagePermission: state.permissions['create-d'],
createPublicChannelPermission: state.permissions['create-c'], createPublicChannelPermission: state.permissions['create-c'],
@ -326,8 +345,8 @@ const mapStateToProps = state => ({
createDiscussionPermission: state.permissions['start-discussion'] createDiscussionPermission: state.permissions['start-discussion']
}); });
const mapDispatchToProps = dispatch => ({ const mapDispatchToProps = (dispatch: Dispatch) => ({
create: params => dispatch(createChannelRequest(params)) create: (params: { group: boolean }) => dispatch(createChannelRequest(params))
}); });
export default connect(mapStateToProps, mapDispatchToProps)(withTheme(NewMessageView)); export default connect(mapStateToProps, mapDispatchToProps)(withTheme(NewMessageView));