2017-11-10 13:42:02 +00:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2018-04-24 19:34:03 +00:00
|
|
|
import { Text, View, SafeAreaView, ScrollView } from 'react-native';
|
2017-11-10 13:42:02 +00:00
|
|
|
import { connect } from 'react-redux';
|
2018-04-03 16:24:59 +00:00
|
|
|
|
|
|
|
import LoggedView from './View';
|
2018-04-24 19:34:03 +00:00
|
|
|
import { forgotPasswordInit, forgotPasswordRequest } from '../actions/login';
|
2017-11-10 13:42:02 +00:00
|
|
|
import KeyboardView from '../presentation/KeyboardView';
|
2018-04-24 19:34:03 +00:00
|
|
|
import TextInput from '../containers/TextInput';
|
|
|
|
import Button from '../containers/Button';
|
|
|
|
import Loading from '../containers/Loading';
|
2017-11-10 13:42:02 +00:00
|
|
|
import styles from './Styles';
|
2018-01-15 20:43:52 +00:00
|
|
|
import { showErrorAlert } from '../utils/info';
|
2018-04-24 19:34:03 +00:00
|
|
|
import scrollPersistTaps from '../utils/scrollPersistTaps';
|
2018-06-01 17:38:13 +00:00
|
|
|
import I18n from '../i18n';
|
2018-04-24 19:34:03 +00:00
|
|
|
|
|
|
|
@connect(state => ({
|
|
|
|
login: state.login
|
|
|
|
}), dispatch => ({
|
|
|
|
forgotPasswordInit: () => dispatch(forgotPasswordInit()),
|
|
|
|
forgotPasswordRequest: email => dispatch(forgotPasswordRequest(email))
|
|
|
|
}))
|
|
|
|
export default class ForgotPasswordView extends LoggedView {
|
2017-11-10 13:42:02 +00:00
|
|
|
static propTypes = {
|
|
|
|
forgotPasswordInit: PropTypes.func.isRequired,
|
|
|
|
forgotPasswordRequest: PropTypes.func.isRequired,
|
|
|
|
login: PropTypes.object,
|
|
|
|
navigation: PropTypes.object.isRequired
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor(props) {
|
2018-04-03 16:24:59 +00:00
|
|
|
super('ForgotPasswordView', props);
|
2017-11-10 13:42:02 +00:00
|
|
|
|
|
|
|
this.state = {
|
|
|
|
email: '',
|
|
|
|
invalidEmail: false
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-03-02 21:31:44 +00:00
|
|
|
componentDidMount() {
|
2017-11-10 13:42:02 +00:00
|
|
|
this.props.forgotPasswordInit();
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate() {
|
|
|
|
const { login } = this.props;
|
|
|
|
if (login.success) {
|
|
|
|
this.props.navigation.goBack();
|
|
|
|
setTimeout(() => {
|
2018-06-01 17:38:13 +00:00
|
|
|
showErrorAlert(I18n.t('Forgot_password_If_this_email_is_registered'), I18n.t('Alert'));
|
2017-11-10 13:42:02 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
validate = (email) => {
|
|
|
|
/* eslint-disable no-useless-escape */
|
|
|
|
const reg = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
|
|
|
|
if (!reg.test(email)) {
|
|
|
|
this.setState({ invalidEmail: true });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.setState({ email, invalidEmail: false });
|
|
|
|
}
|
|
|
|
|
|
|
|
resetPassword = () => {
|
2018-04-24 19:34:03 +00:00
|
|
|
const { email, invalidEmail } = this.state;
|
|
|
|
if (invalidEmail || !email) {
|
2017-11-10 13:42:02 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
this.props.forgotPasswordRequest(email);
|
2017-11-10 13:42:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<KeyboardView
|
|
|
|
contentContainerStyle={styles.container}
|
|
|
|
keyboardVerticalOffset={128}
|
|
|
|
>
|
2018-04-24 19:34:03 +00:00
|
|
|
<ScrollView {...scrollPersistTaps} contentContainerStyle={styles.containerScrollView}>
|
2018-05-23 13:39:18 +00:00
|
|
|
<SafeAreaView testID='forgot-password-view'>
|
2018-04-24 19:34:03 +00:00
|
|
|
<View style={styles.loginView}>
|
|
|
|
<View style={styles.formContainer}>
|
|
|
|
<TextInput
|
|
|
|
inputStyle={this.state.invalidEmail ? { borderColor: 'red' } : {}}
|
2018-06-01 17:38:13 +00:00
|
|
|
label={I18n.t('Email')}
|
|
|
|
placeholder={I18n.t('Email')}
|
2018-04-24 19:34:03 +00:00
|
|
|
keyboardType='email-address'
|
|
|
|
returnKeyType='next'
|
|
|
|
onChangeText={email => this.validate(email)}
|
|
|
|
onSubmitEditing={() => this.resetPassword()}
|
2018-05-23 13:39:18 +00:00
|
|
|
testID='forgot-password-view-email'
|
2018-04-24 19:34:03 +00:00
|
|
|
/>
|
|
|
|
|
|
|
|
<View style={styles.alignItemsFlexStart}>
|
|
|
|
<Button
|
2018-06-01 17:38:13 +00:00
|
|
|
title={I18n.t('Reset_password')}
|
2018-04-24 19:34:03 +00:00
|
|
|
type='primary'
|
|
|
|
onPress={this.resetPassword}
|
2018-05-23 13:39:18 +00:00
|
|
|
testID='forgot-password-view-submit'
|
2018-04-24 19:34:03 +00:00
|
|
|
/>
|
|
|
|
</View>
|
|
|
|
|
|
|
|
{this.props.login.failure && <Text style={styles.error}>{this.props.login.error.reason}</Text>}
|
|
|
|
</View>
|
|
|
|
<Loading visible={this.props.login.isFetching} />
|
2017-11-13 12:49:19 +00:00
|
|
|
</View>
|
2018-04-24 19:34:03 +00:00
|
|
|
</SafeAreaView>
|
|
|
|
</ScrollView>
|
2017-11-10 13:42:02 +00:00
|
|
|
</KeyboardView>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|