Correctly use arrow function instead of bind

This commit is contained in:
Arnaud Vergnet 2020-04-07 17:57:39 +02:00
parent f433edf902
commit 2ee3ed001c

View file

@ -1,7 +1,7 @@
// @flow // @flow
import * as React from 'react'; import * as React from 'react';
import {Keyboard, KeyboardAvoidingView, ScrollView, StyleSheet, TouchableWithoutFeedback, View} from "react-native"; import {KeyboardAvoidingView, ScrollView, StyleSheet, View} from "react-native";
import {Avatar, Button, Card, HelperText, Paragraph, TextInput, withTheme} from 'react-native-paper'; import {Avatar, Button, Card, HelperText, Paragraph, TextInput, withTheme} from 'react-native-paper';
import ConnectionManager from "../../managers/ConnectionManager"; import ConnectionManager from "../../managers/ConnectionManager";
import {openBrowser} from "../../utils/WebBrowser"; import {openBrowser} from "../../utils/WebBrowser";
@ -44,11 +44,6 @@ class LoginScreen extends React.Component<Props, State> {
onEmailChange: Function; onEmailChange: Function;
onPasswordChange: Function; onPasswordChange: Function;
validateEmail: Function;
validatePassword: Function;
onSubmit: Function;
onEmailSubmit: Function;
onResetPasswordClick: Function;
passwordInputRef: Object; passwordInputRef: Object;
@ -56,11 +51,7 @@ class LoginScreen extends React.Component<Props, State> {
super(props); super(props);
this.onEmailChange = this.onInputChange.bind(this, true); this.onEmailChange = this.onInputChange.bind(this, true);
this.onPasswordChange = this.onInputChange.bind(this, false); this.onPasswordChange = this.onInputChange.bind(this, false);
this.validateEmail = this.validateEmail.bind(this);
this.validatePassword = this.validatePassword.bind(this);
this.onSubmit = this.onSubmit.bind(this);
this.onEmailSubmit = this.onEmailSubmit.bind(this);
this.onResetPasswordClick = this.onResetPasswordClick.bind(this);
this.colors = props.theme.colors; this.colors = props.theme.colors;
} }
@ -72,37 +63,23 @@ class LoginScreen extends React.Component<Props, State> {
hideErrorDialog = () => this.setState({dialogVisible: false}); hideErrorDialog = () => this.setState({dialogVisible: false});
onResetPasswordClick() { handleSuccess = () => this.props.navigation.navigate('ProfileScreen');
openBrowser(RESET_PASSWORD_LINK, this.colors.primary);
}
validateEmail() { onResetPasswordClick = () => openBrowser(RESET_PASSWORD_LINK, this.colors.primary);
this.setState({isEmailValidated: true});
}
isEmailValid() { validateEmail = () => this.setState({isEmailValidated: true});
return emailRegex.test(this.state.email);
}
shouldShowEmailError() { isEmailValid() {return emailRegex.test(this.state.email);}
return this.state.isEmailValidated && !this.isEmailValid();
}
validatePassword() { shouldShowEmailError() {return this.state.isEmailValidated && !this.isEmailValid();}
this.setState({isPasswordValidated: true});
}
isPasswordValid() { validatePassword = () => this.setState({isPasswordValidated: true});
return this.state.password !== '';
}
shouldShowPasswordError() { isPasswordValid() {return this.state.password !== '';}
return this.state.isPasswordValidated && !this.isPasswordValid();
}
shouldEnableLogin() { shouldShowPasswordError() {return this.state.isPasswordValidated && !this.isPasswordValid();}
return this.isEmailValid() && this.isPasswordValid() && !this.state.loading;
} shouldEnableLogin() {return this.isEmailValid() && this.isPasswordValid() && !this.state.loading;}
onInputChange(isEmail: boolean, value: string) { onInputChange(isEmail: boolean, value: string) {
if (isEmail) { if (isEmail) {
@ -118,27 +95,19 @@ class LoginScreen extends React.Component<Props, State> {
} }
} }
onEmailSubmit() { onEmailSubmit = () => this.passwordInputRef.focus();
this.passwordInputRef.focus();
}
onSubmit() { onSubmit = () => {
if (this.shouldEnableLogin()) { if (this.shouldEnableLogin()) {
this.setState({loading: true}); this.setState({loading: true});
ConnectionManager.getInstance().connect(this.state.email, this.state.password) ConnectionManager.getInstance().connect(this.state.email, this.state.password)
.then((data) => { .then(this.handleSuccess)
this.handleSuccess();
})
.catch(this.showErrorDialog) .catch(this.showErrorDialog)
.finally(() => { .finally(() => {
this.setState({loading: false}); this.setState({loading: false});
}); });
} }
} };
handleSuccess() {
this.props.navigation.navigate('ProfileScreen');
}
getFormInput() { getFormInput() {
return ( return (
@ -263,12 +232,10 @@ class LoginScreen extends React.Component<Props, State> {
keyboardVerticalOffset={100} keyboardVerticalOffset={100}
> >
<ScrollView> <ScrollView>
<TouchableWithoutFeedback onPress={Keyboard.dismiss}> <View>
<View> {this.getMainCard()}
{this.getMainCard()} {this.getSecondaryCard()}
{this.getSecondaryCard()} </View>
</View>
</TouchableWithoutFeedback>
<ErrorDialog <ErrorDialog
visible={this.state.dialogVisible} visible={this.state.dialogVisible}
onDismiss={this.hideErrorDialog} onDismiss={this.hideErrorDialog}