2017-11-10 13:42:02 +00:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2018-11-14 21:42:03 +00:00
|
|
|
import { Text, ScrollView } from 'react-native';
|
2017-11-10 13:42:02 +00:00
|
|
|
import { connect } from 'react-redux';
|
2018-10-23 21:39:48 +00:00
|
|
|
import { Navigation } from 'react-native-navigation';
|
|
|
|
import SafeAreaView from 'react-native-safe-area-view';
|
2018-04-03 16:24:59 +00:00
|
|
|
|
|
|
|
import LoggedView from './View';
|
2018-11-14 21:42:03 +00:00
|
|
|
import { forgotPasswordRequest as forgotPasswordRequestAction } 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';
|
2018-11-14 21:42:03 +00:00
|
|
|
import sharedStyles 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-11-14 21:42:03 +00:00
|
|
|
import { DARK_HEADER } from '../constants/headerOptions';
|
2018-04-24 19:34:03 +00:00
|
|
|
|
|
|
|
@connect(state => ({
|
|
|
|
login: state.login
|
|
|
|
}), dispatch => ({
|
2018-09-25 19:28:42 +00:00
|
|
|
forgotPasswordRequest: email => dispatch(forgotPasswordRequestAction(email))
|
2018-04-24 19:34:03 +00:00
|
|
|
}))
|
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 {
|
2018-11-14 21:42:03 +00:00
|
|
|
static options() {
|
|
|
|
return {
|
|
|
|
...DARK_HEADER
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-11-10 13:42:02 +00:00
|
|
|
static propTypes = {
|
2018-10-23 21:39:48 +00:00
|
|
|
componentId: PropTypes.string,
|
2017-11-10 13:42:02 +00:00
|
|
|
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: '',
|
2018-11-14 21:42:03 +00:00
|
|
|
invalidEmail: true
|
2017-11-10 13:42:02 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-03-02 21:31:44 +00:00
|
|
|
componentDidMount() {
|
2018-11-14 21:42:03 +00:00
|
|
|
this.timeout = setTimeout(() => {
|
|
|
|
this.emailInput.focus();
|
|
|
|
}, 600);
|
2017-11-10 13:42:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate() {
|
2018-10-23 21:39:48 +00:00
|
|
|
const { login, componentId } = this.props;
|
2017-11-10 13:42:02 +00:00
|
|
|
if (login.success) {
|
2018-10-23 21:39:48 +00:00
|
|
|
Navigation.pop(componentId);
|
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
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-14 21:42:03 +00:00
|
|
|
componentWillUnmount() {
|
|
|
|
if (this.timeout) {
|
|
|
|
clearTimeout(this.timeout);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
2018-09-25 19:28:42 +00:00
|
|
|
const { forgotPasswordRequest } = this.props;
|
2018-04-24 19:34:03 +00:00
|
|
|
if (invalidEmail || !email) {
|
2017-11-10 13:42:02 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-09-25 19:28:42 +00:00
|
|
|
forgotPasswordRequest(email);
|
2017-11-10 13:42:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2018-09-25 19:28:42 +00:00
|
|
|
const { invalidEmail } = this.state;
|
|
|
|
const { login } = this.props;
|
|
|
|
|
2017-11-10 13:42:02 +00:00
|
|
|
return (
|
|
|
|
<KeyboardView
|
2018-11-14 21:42:03 +00:00
|
|
|
contentContainerStyle={sharedStyles.container}
|
2017-11-10 13:42:02 +00:00
|
|
|
keyboardVerticalOffset={128}
|
|
|
|
>
|
2018-11-14 21:42:03 +00:00
|
|
|
<ScrollView {...scrollPersistTaps} contentContainerStyle={sharedStyles.containerScrollView}>
|
|
|
|
<SafeAreaView style={sharedStyles.container} testID='forgot-password-view' forceInset={{ bottom: 'never' }}>
|
|
|
|
<Text style={[sharedStyles.loginTitle, sharedStyles.textBold]}>{I18n.t('Forgot_password')}</Text>
|
|
|
|
<TextInput
|
|
|
|
inputRef={(e) => { this.emailInput = e; }}
|
|
|
|
placeholder={I18n.t('Email')}
|
|
|
|
keyboardType='email-address'
|
|
|
|
iconLeft='mail'
|
|
|
|
returnKeyType='send'
|
|
|
|
onChangeText={email => this.validate(email)}
|
|
|
|
onSubmitEditing={this.resetPassword}
|
|
|
|
testID='forgot-password-view-email'
|
|
|
|
containerStyle={sharedStyles.inputLastChild}
|
|
|
|
/>
|
|
|
|
<Button
|
|
|
|
title={I18n.t('Reset_password')}
|
|
|
|
type='primary'
|
|
|
|
onPress={this.resetPassword}
|
|
|
|
testID='forgot-password-view-submit'
|
|
|
|
loading={login.isFetching}
|
|
|
|
disabled={invalidEmail}
|
|
|
|
/>
|
2018-04-24 19:34:03 +00:00
|
|
|
</SafeAreaView>
|
|
|
|
</ScrollView>
|
2017-11-10 13:42:02 +00:00
|
|
|
</KeyboardView>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|