// @flow import * as React from 'react'; import {Animated, KeyboardAvoidingView, Linking, StyleSheet, View} from "react-native"; import {Avatar, Button, Card, HelperText, Paragraph, TextInput, withTheme} from 'react-native-paper'; import ConnectionManager from "../../managers/ConnectionManager"; import i18n from 'i18n-js'; import ErrorDialog from "../../components/Dialogs/ErrorDialog"; import {withCollapsible} from "../../utils/withCollapsible"; import {Collapsible} from "react-navigation-collapsible"; import CustomTabBar from "../../components/Tabbar/CustomTabBar"; import type {CustomTheme} from "../../managers/ThemeManager"; type Props = { navigation: Object, route: Object, collapsibleStack: Collapsible, theme: CustomTheme } type State = { email: string, password: string, isEmailValidated: boolean, isPasswordValidated: boolean, loading: boolean, dialogVisible: boolean, dialogError: number, } const ICON_AMICALE = require('../../../assets/amicale.png'); const RESET_PASSWORD_PATH = "https://www.amicale-insat.fr//password/reset"; const emailRegex = /^.+@.+\..+$/; class LoginScreen extends React.Component { state = { email: '', password: '', isEmailValidated: false, isPasswordValidated: false, loading: false, dialogVisible: false, dialogError: 0, }; onEmailChange: Function; onPasswordChange: Function; passwordInputRef: Object; nextScreen: string | null; constructor(props) { super(props); this.onEmailChange = this.onInputChange.bind(this, true); this.onPasswordChange = this.onInputChange.bind(this, false); this.props.navigation.addListener('focus', this.onScreenFocus); } onScreenFocus = () => { this.handleNavigationParams(); }; handleNavigationParams () { if (this.props.route.params != null) { if (this.props.route.params.nextScreen != null) this.nextScreen = this.props.route.params.nextScreen; else this.nextScreen = null; } console.log(this.nextScreen); } showErrorDialog = (error: number) => this.setState({ dialogVisible: true, dialogError: error, }); hideErrorDialog = () => this.setState({dialogVisible: false}); handleSuccess = () => { if (this.nextScreen == null) this.props.navigation.goBack(); else this.props.navigation.replace(this.nextScreen); }; onResetPasswordClick = () => Linking.openURL(RESET_PASSWORD_PATH); validateEmail = () => this.setState({isEmailValidated: true}); isEmailValid() { return emailRegex.test(this.state.email); } 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() && !this.state.loading; } onInputChange(isEmail: boolean, value: string) { if (isEmail) { this.setState({ email: value, isEmailValidated: false, }); } else { this.setState({ password: value, isPasswordValidated: false, }); } } onEmailSubmit = () => this.passwordInputRef.focus(); onSubmit = () => { if (this.shouldEnableLogin()) { this.setState({loading: true}); ConnectionManager.getInstance().connect(this.state.email, this.state.password) .then(this.handleSuccess) .catch(this.showErrorDialog) .finally(() => { this.setState({loading: false}); }); } }; getFormInput() { return ( {i18n.t("loginScreen.emailError")} { this.passwordInputRef = ref; }} label={i18n.t("loginScreen.password")} 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} /> {i18n.t("loginScreen.passwordError")} ); } getMainCard() { return ( } /> {this.getFormInput()} ); } getSecondaryCard() { return ( } /> {i18n.t("loginScreen.whyAccountParagraph")} {i18n.t("loginScreen.whyAccountParagraph2")} {i18n.t("loginScreen.noAccount")} ); } render() { const {containerPaddingTop, scrollIndicatorInsetTop, onScroll} = this.props.collapsibleStack; return ( {this.getMainCard()} {this.getSecondaryCard()} ); } } const styles = StyleSheet.create({ container: { flex: 1, flexDirection: 'column', justifyContent: 'center', }, card: { margin: 10, }, header: { fontSize: 36, marginBottom: 48 }, textInput: {}, btnContainer: { marginTop: 5, marginBottom: 10, } }); export default withCollapsible(withTheme(LoginScreen));