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';
|
2018-10-23 21:39:48 +00:00
|
|
|
import SafeAreaView from 'react-native-safe-area-view';
|
2018-04-03 16:24:59 +00:00
|
|
|
|
2019-01-31 16:08:38 +00:00
|
|
|
import Navigation from '../lib/Navigation';
|
2018-04-03 16:24:59 +00:00
|
|
|
import LoggedView from './View';
|
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-12-05 20:52:08 +00:00
|
|
|
import isValidEmail from '../utils/isValidEmail';
|
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-12-05 20:52:08 +00:00
|
|
|
import RocketChat from '../lib/rocketchat';
|
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-12-05 20:52:08 +00:00
|
|
|
componentId: PropTypes.string
|
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-12-05 20:52:08 +00:00
|
|
|
invalidEmail: true,
|
|
|
|
isFetching: false
|
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
|
|
|
}
|
|
|
|
|
2018-12-21 10:55:35 +00:00
|
|
|
shouldComponentUpdate(nextProps, nextState) {
|
|
|
|
const { email, invalidEmail, isFetching } = this.state;
|
|
|
|
if (nextState.email !== email) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (nextState.invalidEmail !== invalidEmail) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (nextState.isFetching !== isFetching) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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) => {
|
2018-12-05 20:52:08 +00:00
|
|
|
if (!isValidEmail(email)) {
|
2017-11-10 13:42:02 +00:00
|
|
|
this.setState({ invalidEmail: true });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.setState({ email, invalidEmail: false });
|
|
|
|
}
|
|
|
|
|
2018-12-05 20:52:08 +00:00
|
|
|
resetPassword = async() => {
|
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-12-05 20:52:08 +00:00
|
|
|
try {
|
|
|
|
this.setState({ isFetching: true });
|
|
|
|
const result = await RocketChat.forgotPassword(email);
|
|
|
|
if (result.success) {
|
|
|
|
const { componentId } = this.props;
|
|
|
|
Navigation.pop(componentId);
|
|
|
|
showErrorAlert(I18n.t('Forgot_password_If_this_email_is_registered'), I18n.t('Alert'));
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
const msg = (e.data && e.data.error) || I18n.t('There_was_an_error_while_action', I18n.t('resetting_password'));
|
|
|
|
showErrorAlert(msg, I18n.t('Alert'));
|
|
|
|
}
|
|
|
|
this.setState({ isFetching: false });
|
2017-11-10 13:42:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2018-12-05 20:52:08 +00:00
|
|
|
const { invalidEmail, isFetching } = this.state;
|
2018-09-25 19:28:42 +00:00
|
|
|
|
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'
|
2018-12-05 20:52:08 +00:00
|
|
|
loading={isFetching}
|
2018-11-14 21:42:03 +00:00
|
|
|
disabled={invalidEmail}
|
|
|
|
/>
|
2018-04-24 19:34:03 +00:00
|
|
|
</SafeAreaView>
|
|
|
|
</ScrollView>
|
2017-11-10 13:42:02 +00:00
|
|
|
</KeyboardView>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|