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))
|
|
|
|
}))
|
2018-07-10 13:40:32 +00:00
|
|
|
/** @extends React.Component */
|
2018-04-24 19:34:03 +00:00
|
|
|
export default class ForgotPasswordView extends LoggedView {
|
2017-11-10 13:42:02 +00:00
|
|
|
static propTypes = {
|
2018-07-10 13:40:32 +00:00
|
|
|
navigator: PropTypes.object,
|
2017-11-10 13:42:02 +00:00
|
|
|
forgotPasswordInit: PropTypes.func.isRequired,
|
|
|
|
forgotPasswordRequest: PropTypes.func.isRequired,
|
2018-07-10 13:40:32 +00:00
|
|
|
login: PropTypes.object
|
2017-11-10 13:42:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2018-07-10 13:40:32 +00:00
|
|
|
this.props.navigator.pop();
|
2017-11-10 13:42:02 +00:00
|
|
|
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}>
|
2018-06-13 01:33:00 +00:00
|
|
|
<TextInput
|
|
|
|
inputStyle={this.state.invalidEmail ? { borderColor: 'red' } : {}}
|
|
|
|
label={I18n.t('Email')}
|
|
|
|
placeholder={I18n.t('Email')}
|
|
|
|
keyboardType='email-address'
|
|
|
|
returnKeyType='next'
|
|
|
|
onChangeText={email => this.validate(email)}
|
|
|
|
onSubmitEditing={() => this.resetPassword()}
|
|
|
|
testID='forgot-password-view-email'
|
|
|
|
/>
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2018-06-13 01:33:00 +00:00
|
|
|
<View style={styles.alignItemsFlexStart}>
|
|
|
|
<Button
|
|
|
|
title={I18n.t('Reset_password')}
|
|
|
|
type='primary'
|
|
|
|
onPress={this.resetPassword}
|
|
|
|
testID='forgot-password-view-submit'
|
|
|
|
/>
|
2018-04-24 19:34:03 +00:00
|
|
|
</View>
|
2018-06-13 01:33:00 +00:00
|
|
|
|
|
|
|
{this.props.login.failure ? <Text style={styles.error}>{this.props.login.error.reason}</Text> : null}
|
2018-04-24 19:34:03 +00:00
|
|
|
<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>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|