2017-11-10 13:42:02 +00:00
|
|
|
import React from 'react';
|
2020-03-30 19:20:50 +00:00
|
|
|
import { Text } from 'react-native';
|
2021-11-25 14:25:11 +00:00
|
|
|
import { StackNavigationProp } from '@react-navigation/stack';
|
|
|
|
import { RouteProp } from '@react-navigation/native';
|
2018-04-03 16:24:59 +00:00
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
import TextInput from '../containers/TextInput';
|
|
|
|
import Button from '../containers/Button';
|
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-06-01 17:38:13 +00:00
|
|
|
import I18n from '../i18n';
|
2018-12-05 20:52:08 +00:00
|
|
|
import RocketChat from '../lib/rocketchat';
|
2019-12-04 16:39:53 +00:00
|
|
|
import { withTheme } from '../theme';
|
|
|
|
import { themes } from '../constants/colors';
|
2020-03-30 19:20:50 +00:00
|
|
|
import FormContainer, { FormContainerInner } from '../containers/FormContainer';
|
2021-10-01 18:12:19 +00:00
|
|
|
import { events, logEvent } from '../utils/log';
|
|
|
|
import sharedStyles from './Styles';
|
2021-12-24 13:12:49 +00:00
|
|
|
import { OutsideParamList } from '../stacks/types';
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2021-11-25 14:25:11 +00:00
|
|
|
interface IForgotPasswordViewState {
|
|
|
|
email: string;
|
|
|
|
invalidEmail: boolean;
|
|
|
|
isFetching: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface IForgotPasswordViewProps {
|
2021-12-24 13:12:49 +00:00
|
|
|
navigation: StackNavigationProp<OutsideParamList, 'ForgotPasswordView'>;
|
|
|
|
route: RouteProp<OutsideParamList, 'ForgotPasswordView'>;
|
2021-11-25 14:25:11 +00:00
|
|
|
theme: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
class ForgotPasswordView extends React.Component<IForgotPasswordViewProps, IForgotPasswordViewState> {
|
|
|
|
static navigationOptions = ({ route }: Pick<IForgotPasswordViewProps, 'route'>) => ({
|
2020-06-15 14:00:46 +00:00
|
|
|
title: route.params?.title ?? 'Rocket.Chat'
|
2021-10-01 18:12:19 +00:00
|
|
|
});
|
2018-11-14 21:42:03 +00:00
|
|
|
|
2019-05-28 13:03:08 +00:00
|
|
|
state = {
|
|
|
|
email: '',
|
|
|
|
invalidEmail: true,
|
|
|
|
isFetching: false
|
2021-10-01 18:12:19 +00:00
|
|
|
};
|
2017-11-10 13:42:02 +00:00
|
|
|
|
2021-11-25 14:25:11 +00:00
|
|
|
shouldComponentUpdate(nextProps: IForgotPasswordViewProps, nextState: IForgotPasswordViewState) {
|
2018-12-21 10:55:35 +00:00
|
|
|
const { email, invalidEmail, isFetching } = this.state;
|
2019-12-04 16:39:53 +00:00
|
|
|
const { theme } = this.props;
|
|
|
|
if (nextProps.theme !== theme) {
|
|
|
|
return true;
|
|
|
|
}
|
2018-12-21 10:55:35 +00:00
|
|
|
if (nextState.email !== email) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (nextState.invalidEmail !== invalidEmail) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (nextState.isFetching !== isFetching) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-11-25 14:25:11 +00:00
|
|
|
validate = (email: string) => {
|
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 });
|
2021-10-01 18:12:19 +00:00
|
|
|
};
|
2017-11-10 13:42:02 +00:00
|
|
|
|
2021-10-01 18:12:19 +00:00
|
|
|
resetPassword = async () => {
|
2020-07-30 13:26:17 +00:00
|
|
|
logEvent(events.FP_FORGOT_PASSWORD);
|
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) {
|
2019-03-12 16:23:06 +00:00
|
|
|
const { navigation } = this.props;
|
|
|
|
navigation.pop();
|
2018-12-05 20:52:08 +00:00
|
|
|
showErrorAlert(I18n.t('Forgot_password_If_this_email_is_registered'), I18n.t('Alert'));
|
|
|
|
}
|
2021-11-25 14:25:11 +00:00
|
|
|
} catch (e: any) {
|
2020-07-30 13:26:17 +00:00
|
|
|
logEvent(events.FP_FORGOT_PASSWORD_F);
|
2020-02-28 17:25:38 +00:00
|
|
|
const msg = (e.data && e.data.error) || I18n.t('There_was_an_error_while_action', { action: I18n.t('resetting_password') });
|
2018-12-05 20:52:08 +00:00
|
|
|
showErrorAlert(msg, I18n.t('Alert'));
|
|
|
|
}
|
|
|
|
this.setState({ isFetching: false });
|
2021-10-01 18:12:19 +00:00
|
|
|
};
|
2017-11-10 13:42:02 +00:00
|
|
|
|
|
|
|
render() {
|
2018-12-05 20:52:08 +00:00
|
|
|
const { invalidEmail, isFetching } = this.state;
|
2019-12-04 16:39:53 +00:00
|
|
|
const { theme } = this.props;
|
2018-09-25 19:28:42 +00:00
|
|
|
|
2017-11-10 13:42:02 +00:00
|
|
|
return (
|
2022-03-29 18:53:27 +00:00
|
|
|
<FormContainer testID='forgot-password-view'>
|
2020-03-30 19:20:50 +00:00
|
|
|
<FormContainerInner>
|
2021-10-01 18:12:19 +00:00
|
|
|
<Text style={[sharedStyles.loginTitle, sharedStyles.textBold, { color: themes[theme].titleText }]}>
|
|
|
|
{I18n.t('Forgot_password')}
|
|
|
|
</Text>
|
2020-03-30 19:20:50 +00:00
|
|
|
<TextInput
|
|
|
|
autoFocus
|
|
|
|
placeholder={I18n.t('Email')}
|
|
|
|
keyboardType='email-address'
|
|
|
|
returnKeyType='send'
|
|
|
|
onChangeText={email => this.validate(email)}
|
|
|
|
onSubmitEditing={this.resetPassword}
|
|
|
|
testID='forgot-password-view-email'
|
|
|
|
containerStyle={sharedStyles.inputLastChild}
|
|
|
|
theme={theme}
|
|
|
|
/>
|
|
|
|
<Button
|
|
|
|
title={I18n.t('Reset_password')}
|
|
|
|
type='primary'
|
|
|
|
onPress={this.resetPassword}
|
|
|
|
testID='forgot-password-view-submit'
|
|
|
|
loading={isFetching}
|
|
|
|
disabled={invalidEmail}
|
|
|
|
theme={theme}
|
|
|
|
/>
|
|
|
|
</FormContainerInner>
|
|
|
|
</FormContainer>
|
2017-11-10 13:42:02 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-12-04 16:39:53 +00:00
|
|
|
|
|
|
|
export default withTheme(ForgotPasswordView);
|