2020-03-29 11:47:27 +02:00
|
|
|
// @flow
|
|
|
|
|
|
|
|
import * as React from 'react';
|
2020-03-30 20:42:27 +02:00
|
|
|
import {
|
|
|
|
Alert,
|
|
|
|
Keyboard,
|
|
|
|
KeyboardAvoidingView,
|
|
|
|
ScrollView,
|
|
|
|
StyleSheet,
|
|
|
|
TouchableWithoutFeedback,
|
|
|
|
View
|
|
|
|
} from "react-native";
|
2020-03-30 19:50:16 +02:00
|
|
|
import {Avatar, Button, Card, HelperText, Text, TextInput, withTheme} from 'react-native-paper';
|
2020-03-31 11:05:00 +02:00
|
|
|
import ConnectionManager, {ERROR_TYPE} from "../../managers/ConnectionManager";
|
2020-03-30 19:50:16 +02:00
|
|
|
import {openBrowser} from "../../utils/WebBrowser";
|
2020-03-31 20:40:52 +02:00
|
|
|
import i18n from 'i18n-js';
|
2020-03-29 11:47:27 +02:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
navigation: Object,
|
|
|
|
}
|
|
|
|
|
|
|
|
type State = {
|
|
|
|
email: string,
|
|
|
|
password: string,
|
2020-03-30 19:50:16 +02:00
|
|
|
isEmailValidated: boolean,
|
|
|
|
isPasswordValidated: boolean,
|
2020-03-30 20:42:27 +02:00
|
|
|
loading: boolean,
|
2020-03-29 11:47:27 +02:00
|
|
|
}
|
|
|
|
|
2020-03-30 19:50:16 +02:00
|
|
|
const ICON_AMICALE = require('../../assets/amicale.png');
|
2020-03-29 11:47:27 +02:00
|
|
|
|
2020-03-30 19:50:16 +02:00
|
|
|
const RESET_PASSWORD_LINK = "https://www.amicale-insat.fr/password/reset";
|
|
|
|
|
2020-03-30 20:42:27 +02:00
|
|
|
const emailRegex = /^.+@.+\..+$/;
|
|
|
|
|
2020-03-30 19:50:16 +02:00
|
|
|
class LoginScreen extends React.Component<Props, State> {
|
2020-03-29 11:47:27 +02:00
|
|
|
|
|
|
|
state = {
|
|
|
|
email: '',
|
|
|
|
password: '',
|
2020-03-30 19:50:16 +02:00
|
|
|
isEmailValidated: false,
|
|
|
|
isPasswordValidated: false,
|
2020-03-30 20:42:27 +02:00
|
|
|
loading: false,
|
2020-03-29 11:47:27 +02:00
|
|
|
};
|
|
|
|
|
2020-03-30 19:50:16 +02:00
|
|
|
colors: Object;
|
|
|
|
|
2020-03-29 11:47:27 +02:00
|
|
|
onEmailChange: Function;
|
|
|
|
onPasswordChange: Function;
|
2020-03-30 19:50:16 +02:00
|
|
|
validateEmail: Function;
|
|
|
|
validatePassword: Function;
|
|
|
|
onSubmit: Function;
|
|
|
|
onEmailSubmit: Function;
|
|
|
|
onResetPasswordClick: Function;
|
|
|
|
|
|
|
|
passwordInputRef: Object;
|
2020-03-29 11:47:27 +02:00
|
|
|
|
2020-03-30 19:50:16 +02:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2020-03-29 11:47:27 +02:00
|
|
|
this.onEmailChange = this.onInputChange.bind(this, true);
|
|
|
|
this.onPasswordChange = this.onInputChange.bind(this, false);
|
2020-03-30 19:50:16 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
onResetPasswordClick() {
|
|
|
|
openBrowser(RESET_PASSWORD_LINK, this.colors.primary);
|
|
|
|
}
|
|
|
|
|
|
|
|
validateEmail() {
|
|
|
|
this.setState({isEmailValidated: true});
|
|
|
|
}
|
|
|
|
|
|
|
|
isEmailValid() {
|
2020-03-30 20:42:27 +02:00
|
|
|
return emailRegex.test(this.state.email);
|
2020-03-30 19:50:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
shouldShowEmailError() {
|
|
|
|
return this.state.isEmailValidated && !this.isEmailValid();
|
|
|
|
}
|
|
|
|
|
|
|
|
validatePassword() {
|
|
|
|
this.setState({isPasswordValidated: true});
|
|
|
|
}
|
|
|
|
|
|
|
|
isPasswordValid() {
|
|
|
|
return this.state.password !== '';
|
|
|
|
}
|
|
|
|
|
|
|
|
shouldShowPasswordError() {
|
|
|
|
return this.state.isPasswordValidated && !this.isPasswordValid();
|
|
|
|
}
|
|
|
|
|
|
|
|
shouldEnableLogin() {
|
|
|
|
return this.isEmailValid() && this.isPasswordValid();
|
2020-03-29 11:47:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
onInputChange(isEmail: boolean, value: string) {
|
2020-03-30 19:50:16 +02:00
|
|
|
if (isEmail) {
|
|
|
|
this.setState({
|
|
|
|
email: value,
|
|
|
|
isEmailValidated: false,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.setState({
|
|
|
|
password: value,
|
|
|
|
isPasswordValidated: false,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
onEmailSubmit() {
|
|
|
|
this.passwordInputRef.focus();
|
2020-03-29 11:47:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
onSubmit() {
|
2020-03-30 19:50:16 +02:00
|
|
|
if (this.shouldEnableLogin()) {
|
2020-03-30 20:42:27 +02:00
|
|
|
this.setState({loading: true});
|
2020-03-30 19:50:16 +02:00
|
|
|
ConnectionManager.getInstance().connect(this.state.email, this.state.password)
|
|
|
|
.then((data) => {
|
2020-03-31 14:21:01 +02:00
|
|
|
this.handleSuccess();
|
2020-03-30 19:50:16 +02:00
|
|
|
})
|
|
|
|
.catch((error) => {
|
2020-03-31 11:05:00 +02:00
|
|
|
this.handleErrors(error);
|
2020-03-30 20:42:27 +02:00
|
|
|
})
|
|
|
|
.finally(() => {
|
|
|
|
this.setState({loading: false});
|
2020-03-30 19:50:16 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-31 14:21:01 +02:00
|
|
|
handleSuccess() {
|
|
|
|
this.props.navigation.navigate('ProfileScreen');
|
|
|
|
}
|
|
|
|
|
2020-03-31 11:05:00 +02:00
|
|
|
handleErrors(error: number) {
|
2020-03-31 20:40:52 +02:00
|
|
|
const title = i18n.t("loginScreen.errors.title");
|
|
|
|
let message;
|
2020-03-31 11:05:00 +02:00
|
|
|
switch (error) {
|
|
|
|
case ERROR_TYPE.CONNECTION_ERROR:
|
2020-03-31 20:40:52 +02:00
|
|
|
message = i18n.t("loginScreen.errors.connection");
|
2020-03-31 11:05:00 +02:00
|
|
|
break;
|
|
|
|
case ERROR_TYPE.BAD_CREDENTIALS:
|
2020-03-31 20:40:52 +02:00
|
|
|
message = i18n.t("loginScreen.errors.credentials");
|
2020-03-31 11:05:00 +02:00
|
|
|
break;
|
|
|
|
case ERROR_TYPE.SAVE_TOKEN:
|
2020-03-31 20:40:52 +02:00
|
|
|
message = i18n.t("loginScreen.errors.saveToken");
|
2020-03-31 11:05:00 +02:00
|
|
|
break;
|
2020-03-31 19:56:14 +02:00
|
|
|
case ERROR_TYPE.NO_CONSENT:
|
2020-03-31 20:40:52 +02:00
|
|
|
message = i18n.t("loginScreen.errors.consent");
|
2020-03-31 19:56:14 +02:00
|
|
|
break;
|
2020-03-31 11:05:00 +02:00
|
|
|
default:
|
2020-03-31 20:40:52 +02:00
|
|
|
message = i18n.t("loginScreen.errors.unknown");
|
2020-03-31 11:05:00 +02:00
|
|
|
break;
|
|
|
|
}
|
2020-03-31 20:40:52 +02:00
|
|
|
Alert.alert(title, message);
|
2020-03-31 11:05:00 +02:00
|
|
|
}
|
|
|
|
|
2020-03-30 19:50:16 +02:00
|
|
|
getFormInput() {
|
|
|
|
return (
|
|
|
|
<View>
|
|
|
|
<TextInput
|
2020-03-31 20:40:52 +02:00
|
|
|
label={i18n.t("loginScreen.email")}
|
2020-03-30 19:50:16 +02:00
|
|
|
mode='outlined'
|
|
|
|
value={this.state.email}
|
|
|
|
onChangeText={this.onEmailChange}
|
|
|
|
onBlur={this.validateEmail}
|
|
|
|
onSubmitEditing={this.onEmailSubmit}
|
|
|
|
error={this.shouldShowEmailError()}
|
|
|
|
textContentType={'emailAddress'}
|
|
|
|
autoCapitalize={'none'}
|
|
|
|
autoCompleteType={'email'}
|
|
|
|
autoCorrect={false}
|
|
|
|
keyboardType={'email-address'}
|
|
|
|
returnKeyType={'next'}
|
|
|
|
secureTextEntry={false}
|
|
|
|
/>
|
|
|
|
<HelperText
|
|
|
|
type="error"
|
|
|
|
visible={this.shouldShowEmailError()}
|
|
|
|
>
|
2020-03-31 20:40:52 +02:00
|
|
|
{i18n.t("loginScreen.emailError")}
|
2020-03-30 19:50:16 +02:00
|
|
|
</HelperText>
|
|
|
|
<TextInput
|
|
|
|
ref={(ref) => {
|
|
|
|
this.passwordInputRef = ref;
|
|
|
|
}}
|
2020-03-31 20:40:52 +02:00
|
|
|
label={i18n.t("loginScreen.password")}
|
2020-03-30 19:50:16 +02:00
|
|
|
mode='outlined'
|
|
|
|
value={this.state.password}
|
|
|
|
onChangeText={this.onPasswordChange}
|
|
|
|
onBlur={this.validatePassword}
|
|
|
|
onSubmitEditing={this.onSubmit}
|
|
|
|
error={this.shouldShowPasswordError()}
|
|
|
|
textContentType={'password'}
|
|
|
|
autoCapitalize={'none'}
|
|
|
|
autoCompleteType={'password'}
|
|
|
|
autoCorrect={false}
|
|
|
|
keyboardType={'default'}
|
|
|
|
returnKeyType={'done'}
|
|
|
|
secureTextEntry={true}
|
|
|
|
/>
|
|
|
|
<HelperText
|
|
|
|
type="error"
|
|
|
|
visible={this.shouldShowPasswordError()}
|
|
|
|
>
|
2020-03-31 20:40:52 +02:00
|
|
|
{i18n.t("loginScreen.passwordError")}
|
2020-03-30 19:50:16 +02:00
|
|
|
</HelperText>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
getMainCard() {
|
|
|
|
return (
|
|
|
|
<Card style={styles.card}>
|
|
|
|
<Card.Title
|
2020-03-31 20:40:52 +02:00
|
|
|
title={i18n.t("loginScreen.title")}
|
|
|
|
subtitle={i18n.t("loginScreen.subtitle")}
|
2020-03-30 19:50:16 +02:00
|
|
|
left={(props) => <Avatar.Image
|
|
|
|
{...props}
|
|
|
|
source={ICON_AMICALE}
|
|
|
|
style={{backgroundColor: 'transparent'}}/>}
|
|
|
|
/>
|
|
|
|
<Card.Content>
|
|
|
|
{this.getFormInput()}
|
|
|
|
<Card.Actions>
|
|
|
|
<Button
|
|
|
|
icon="send"
|
|
|
|
mode="contained"
|
|
|
|
disabled={!this.shouldEnableLogin()}
|
2020-03-30 20:42:27 +02:00
|
|
|
loading={this.state.loading}
|
2020-03-30 19:50:16 +02:00
|
|
|
onPress={this.onSubmit}
|
|
|
|
style={{marginLeft: 'auto'}}>
|
2020-03-31 20:40:52 +02:00
|
|
|
{i18n.t("loginScreen.login")}
|
2020-03-30 19:50:16 +02:00
|
|
|
</Button>
|
|
|
|
</Card.Actions>
|
|
|
|
</Card.Content>
|
|
|
|
</Card>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
getSecondaryCard() {
|
|
|
|
return (
|
|
|
|
<Card style={styles.card}>
|
|
|
|
<Card.Content>
|
2020-03-31 20:40:52 +02:00
|
|
|
<Text>{i18n.t("loginScreen.forgotPassword")}</Text>
|
2020-03-30 19:50:16 +02:00
|
|
|
<View style={styles.btnContainer}>
|
|
|
|
<Button
|
|
|
|
icon="reload"
|
|
|
|
mode="contained"
|
|
|
|
onPress={this.onResetPasswordClick}
|
|
|
|
style={{marginLeft: 'auto'}}>
|
2020-03-31 20:40:52 +02:00
|
|
|
{i18n.t("loginScreen.resetPassword")}
|
2020-03-30 19:50:16 +02:00
|
|
|
</Button>
|
|
|
|
</View>
|
2020-03-31 20:40:52 +02:00
|
|
|
<Text>{i18n.t("loginScreen.noAccount")}</Text>
|
2020-03-30 19:50:16 +02:00
|
|
|
</Card.Content>
|
|
|
|
</Card>
|
|
|
|
);
|
2020-03-29 11:47:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<KeyboardAvoidingView
|
2020-03-30 19:50:16 +02:00
|
|
|
behavior={"height"}
|
2020-03-29 11:47:27 +02:00
|
|
|
contentContainerStyle={styles.container}
|
|
|
|
style={styles.container}
|
2020-03-30 19:50:16 +02:00
|
|
|
enabled
|
|
|
|
keyboardVerticalOffset={100}
|
2020-03-29 11:47:27 +02:00
|
|
|
>
|
2020-03-30 19:50:16 +02:00
|
|
|
<ScrollView>
|
|
|
|
<TouchableWithoutFeedback onPress={Keyboard.dismiss}>
|
|
|
|
<View>
|
|
|
|
{this.getMainCard()}
|
|
|
|
{this.getSecondaryCard()}
|
2020-03-29 11:47:27 +02:00
|
|
|
</View>
|
2020-03-30 19:50:16 +02:00
|
|
|
</TouchableWithoutFeedback>
|
|
|
|
</ScrollView>
|
2020-03-29 11:47:27 +02:00
|
|
|
</KeyboardAvoidingView>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
container: {
|
|
|
|
flex: 1,
|
2020-03-30 19:50:16 +02:00
|
|
|
flexDirection: 'column',
|
|
|
|
justifyContent: 'center',
|
|
|
|
},
|
|
|
|
card: {
|
|
|
|
margin: 10,
|
2020-03-29 11:47:27 +02:00
|
|
|
},
|
|
|
|
header: {
|
|
|
|
fontSize: 36,
|
|
|
|
marginBottom: 48
|
|
|
|
},
|
|
|
|
textInput: {},
|
|
|
|
btnContainer: {
|
2020-03-30 19:50:16 +02:00
|
|
|
marginTop: 5,
|
|
|
|
marginBottom: 10,
|
2020-03-29 11:47:27 +02:00
|
|
|
}
|
|
|
|
});
|
2020-03-30 19:50:16 +02:00
|
|
|
|
|
|
|
export default withTheme(LoginScreen);
|