Chore: Migrate RegisterView to TypeScript (#3417)

* start migration to typescript

* update interface for RegisterView and TextInput

* Update types TextInput

* Minor tweak

* Update TextInput interface

* Minor change

* Update FormContainer.tsx

* Minor tweaks

* style: adding ts-ignore to fix later

* Minor tweak

* update storyshots

* Update TextInput interface

* Minor tweak

* Update snapshots

Co-authored-by: AlexAlexandre <alexalexandrejr@gmail.com>
Co-authored-by: Diego Mello <diegolmello@gmail.com>
This commit is contained in:
Gerzon Z 2021-10-20 13:53:35 -04:00 committed by GitHub
parent 7728997e3e
commit 839645e42c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 49 additions and 35 deletions

View File

@ -1,6 +1,7 @@
import React from 'react'; import React from 'react';
import PropTypes from 'prop-types';
import { Keyboard, StyleSheet, Text, View } from 'react-native'; import { Keyboard, StyleSheet, Text, View } from 'react-native';
import { StackNavigationProp } from '@react-navigation/stack';
import { RouteProp } from '@react-navigation/core';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import RNPickerSelect from 'react-native-picker-select'; import RNPickerSelect from 'react-native-picker-select';
@ -49,27 +50,37 @@ const styles = StyleSheet.create({
} }
}); });
class RegisterView extends React.Component { interface IProps {
static navigationOptions = ({ route, navigation }) => ({ navigation: StackNavigationProp<any>;
title: route.params?.title ?? 'Rocket.Chat', route: RouteProp<any, 'RegisterView'>;
server: string;
Site_Name: string;
Gitlab_URL: string;
CAS_enabled: boolean;
CAS_login_url: string;
Accounts_CustomFields: string;
Accounts_EmailVerification: boolean;
Accounts_ManuallyApproveNewUsers: boolean;
showLoginButton: boolean;
loginRequest: Function;
theme: string;
}
class RegisterView extends React.Component<IProps, any> {
private parsedCustomFields: any;
private usernameInput: any;
private passwordInput: any;
private emailInput: any;
private avatarUrl: any;
static navigationOptions = ({ route, navigation }: Partial<IProps>) => ({
title: route?.params?.title ?? 'Rocket.Chat',
headerRight: () => <HeaderButton.Legal testID='register-view-more' navigation={navigation} /> headerRight: () => <HeaderButton.Legal testID='register-view-more' navigation={navigation} />
}); });
static propTypes = { constructor(props: IProps) {
navigation: PropTypes.object,
server: PropTypes.string,
Accounts_CustomFields: PropTypes.string,
Accounts_EmailVerification: PropTypes.bool,
Accounts_ManuallyApproveNewUsers: PropTypes.bool,
theme: PropTypes.string,
Site_Name: PropTypes.string,
loginRequest: PropTypes.func,
showLoginButton: PropTypes.bool
};
constructor(props) {
super(props); super(props);
const customFields = {}; const customFields: any = {};
this.parsedCustomFields = {}; this.parsedCustomFields = {};
if (props.Accounts_CustomFields) { if (props.Accounts_CustomFields) {
try { try {
@ -78,7 +89,7 @@ class RegisterView extends React.Component {
log(e); log(e);
} }
} }
Object.keys(this.parsedCustomFields).forEach(key => { Object.keys(this.parsedCustomFields).forEach((key: string) => {
if (this.parsedCustomFields[key].defaultValue) { if (this.parsedCustomFields[key].defaultValue) {
customFields[key] = this.parsedCustomFields[key].defaultValue; customFields[key] = this.parsedCustomFields[key].defaultValue;
} }
@ -101,7 +112,7 @@ class RegisterView extends React.Component {
valid = () => { valid = () => {
const { name, email, password, username, customFields } = this.state; const { name, email, password, username, customFields } = this.state;
let requiredCheck = true; let requiredCheck = true;
Object.keys(this.parsedCustomFields).forEach(key => { Object.keys(this.parsedCustomFields).forEach((key: string) => {
if (this.parsedCustomFields[key].required) { if (this.parsedCustomFields[key].required) {
requiredCheck = requiredCheck && customFields[key] && Boolean(customFields[key].trim()); requiredCheck = requiredCheck && customFields[key] && Boolean(customFields[key].trim());
} }
@ -138,7 +149,7 @@ class RegisterView extends React.Component {
} else { } else {
await loginRequest({ user: email, password }); await loginRequest({ user: email, password });
} }
} catch (e) { } catch (e: any) {
if (e.data?.errorType === 'username-invalid') { if (e.data?.errorType === 'username-invalid') {
return loginRequest({ user: email, password }); return loginRequest({ user: email, password });
} }
@ -150,7 +161,7 @@ class RegisterView extends React.Component {
this.setState({ saving: false }); this.setState({ saving: false });
}; };
openContract = route => { openContract = (route: string) => {
const { server, theme } = this.props; const { server, theme } = this.props;
if (!server) { if (!server) {
return; return;
@ -167,19 +178,20 @@ class RegisterView extends React.Component {
try { try {
return Object.keys(this.parsedCustomFields).map((key, index, array) => { return Object.keys(this.parsedCustomFields).map((key, index, array) => {
if (this.parsedCustomFields[key].type === 'select') { if (this.parsedCustomFields[key].type === 'select') {
const options = this.parsedCustomFields[key].options.map(option => ({ label: option, value: option })); const options = this.parsedCustomFields[key].options.map((option: string) => ({ label: option, value: option }));
return ( return (
<RNPickerSelect <RNPickerSelect
key={key} key={key}
items={options} items={options}
onValueChange={value => { onValueChange={value => {
const newValue = {}; const newValue: { [key: string]: string | number } = {};
newValue[key] = value; newValue[key] = value;
this.setState({ customFields: { ...customFields, ...newValue } }); this.setState({ customFields: { ...customFields, ...newValue } });
}} }}
value={customFields[key]}> value={customFields[key]}>
<TextInput <TextInput
inputRef={e => { inputRef={(e: any) => {
// @ts-ignore
this[key] = e; this[key] = e;
}} }}
placeholder={key} placeholder={key}
@ -193,20 +205,22 @@ class RegisterView extends React.Component {
return ( return (
<TextInput <TextInput
inputRef={e => { inputRef={(e: any) => {
// @ts-ignore
this[key] = e; this[key] = e;
}} }}
key={key} key={key}
label={key} label={key}
placeholder={key} placeholder={key}
value={customFields[key]} value={customFields[key]}
onChangeText={value => { onChangeText={(value: string) => {
const newValue = {}; const newValue: { [key: string]: string | number } = {};
newValue[key] = value; newValue[key] = value;
this.setState({ customFields: { ...customFields, ...newValue } }); this.setState({ customFields: { ...customFields, ...newValue } });
}} }}
onSubmitEditing={() => { onSubmitEditing={() => {
if (array.length - 1 > index) { if (array.length - 1 > index) {
// @ts-ignore
return this[array[index + 1]].focus(); return this[array[index + 1]].focus();
} }
this.avatarUrl.focus(); this.avatarUrl.focus();
@ -234,7 +248,7 @@ class RegisterView extends React.Component {
containerStyle={styles.inputContainer} containerStyle={styles.inputContainer}
placeholder={I18n.t('Name')} placeholder={I18n.t('Name')}
returnKeyType='next' returnKeyType='next'
onChangeText={name => this.setState({ name })} onChangeText={(name: string) => this.setState({ name })}
onSubmitEditing={() => { onSubmitEditing={() => {
this.usernameInput.focus(); this.usernameInput.focus();
}} }}
@ -249,7 +263,7 @@ class RegisterView extends React.Component {
}} }}
placeholder={I18n.t('Username')} placeholder={I18n.t('Username')}
returnKeyType='next' returnKeyType='next'
onChangeText={username => this.setState({ username })} onChangeText={(username: string) => this.setState({ username })}
onSubmitEditing={() => { onSubmitEditing={() => {
this.emailInput.focus(); this.emailInput.focus();
}} }}
@ -265,7 +279,7 @@ class RegisterView extends React.Component {
placeholder={I18n.t('Email')} placeholder={I18n.t('Email')}
returnKeyType='next' returnKeyType='next'
keyboardType='email-address' keyboardType='email-address'
onChangeText={email => this.setState({ email })} onChangeText={(email: string) => this.setState({ email })}
onSubmitEditing={() => { onSubmitEditing={() => {
this.passwordInput.focus(); this.passwordInput.focus();
}} }}
@ -281,7 +295,7 @@ class RegisterView extends React.Component {
placeholder={I18n.t('Password')} placeholder={I18n.t('Password')}
returnKeyType='send' returnKeyType='send'
secureTextEntry secureTextEntry
onChangeText={value => this.setState({ password: value })} onChangeText={(value: string) => this.setState({ password: value })}
onSubmitEditing={this.submit} onSubmitEditing={this.submit}
testID='register-view-password' testID='register-view-password'
theme={theme} theme={theme}
@ -334,7 +348,7 @@ class RegisterView extends React.Component {
} }
} }
const mapStateToProps = state => ({ const mapStateToProps = (state: any) => ({
server: state.server.server, server: state.server.server,
Site_Name: state.settings.Site_Name, Site_Name: state.settings.Site_Name,
Gitlab_URL: state.settings.API_Gitlab_URL, Gitlab_URL: state.settings.API_Gitlab_URL,
@ -346,8 +360,8 @@ const mapStateToProps = state => ({
showLoginButton: getShowLoginButton(state) showLoginButton: getShowLoginButton(state)
}); });
const mapDispatchToProps = dispatch => ({ const mapDispatchToProps = (dispatch: any) => ({
loginRequest: params => dispatch(loginRequestAction(params)) loginRequest: (params: any) => dispatch(loginRequestAction(params))
}); });
export default connect(mapStateToProps, mapDispatchToProps)(withTheme(RegisterView)); export default connect(mapStateToProps, mapDispatchToProps)(withTheme(RegisterView));