2022-09-06 17:44:31 +00:00
|
|
|
import React, { useLayoutEffect, useState } from 'react';
|
2020-03-30 19:20:50 +00:00
|
|
|
import { Text } from 'react-native';
|
2022-09-06 17:44:31 +00:00
|
|
|
import { RouteProp, useNavigation, useRoute } from '@react-navigation/native';
|
|
|
|
import { StackNavigationProp } from '@react-navigation/stack';
|
|
|
|
import { useForm } from 'react-hook-form';
|
|
|
|
import * as yup from 'yup';
|
|
|
|
import { yupResolver } from '@hookform/resolvers/yup';
|
2018-04-03 16:24:59 +00:00
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
import Button from '../containers/Button';
|
2022-04-28 20:37:25 +00:00
|
|
|
import FormContainer, { FormContainerInner } from '../containers/FormContainer';
|
2022-09-06 17:44:31 +00:00
|
|
|
import { ControlledFormTextInput } from '../containers/TextInput';
|
2018-06-01 17:38:13 +00:00
|
|
|
import I18n from '../i18n';
|
2022-04-28 20:37:25 +00:00
|
|
|
import { Services } from '../lib/services';
|
|
|
|
import { OutsideParamList } from '../stacks/types';
|
2022-09-06 17:44:31 +00:00
|
|
|
import { useTheme } from '../theme';
|
|
|
|
import { showErrorAlert } from '../lib/methods/helpers';
|
2022-06-06 14:17:51 +00:00
|
|
|
import { events, logEvent } from '../lib/methods/helpers/log';
|
2021-09-13 20:41:05 +00:00
|
|
|
import sharedStyles from './Styles';
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2022-09-06 17:44:31 +00:00
|
|
|
const schema = yup.object().shape({
|
|
|
|
email: yup.string().email().required()
|
|
|
|
});
|
|
|
|
|
|
|
|
interface ISubmit {
|
2021-11-17 20:06:17 +00:00
|
|
|
email: string;
|
|
|
|
}
|
|
|
|
|
2022-09-06 17:44:31 +00:00
|
|
|
const ForgotPasswordView = (): React.ReactElement => {
|
|
|
|
const {
|
|
|
|
control,
|
|
|
|
handleSubmit,
|
|
|
|
formState: { isValid }
|
|
|
|
} = useForm<ISubmit>({ mode: 'onChange', resolver: yupResolver(schema) });
|
2018-11-14 21:42:03 +00:00
|
|
|
|
2022-09-06 17:44:31 +00:00
|
|
|
const [isFetching, setIsFetching] = useState(false);
|
2017-11-10 13:42:02 +00:00
|
|
|
|
2022-09-06 17:44:31 +00:00
|
|
|
const navigation = useNavigation<StackNavigationProp<OutsideParamList, 'ForgotPasswordView'>>();
|
|
|
|
const { params } = useRoute<RouteProp<OutsideParamList, 'ForgotPasswordView'>>();
|
|
|
|
const { colors } = useTheme();
|
2018-12-21 10:55:35 +00:00
|
|
|
|
2022-09-06 17:44:31 +00:00
|
|
|
useLayoutEffect(() => {
|
|
|
|
navigation.setOptions({
|
|
|
|
title: params?.title ?? 'Rocket.Chat'
|
|
|
|
});
|
|
|
|
}, [navigation, params?.title]);
|
2017-11-10 13:42:02 +00:00
|
|
|
|
2022-09-06 17:44:31 +00:00
|
|
|
const resetPassword = async ({ email }: ISubmit) => {
|
|
|
|
if (!isValid) {
|
2017-11-10 13:42:02 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-12-05 20:52:08 +00:00
|
|
|
try {
|
2022-09-06 17:44:31 +00:00
|
|
|
logEvent(events.FP_FORGOT_PASSWORD);
|
|
|
|
setIsFetching(true);
|
2022-04-28 20:37:25 +00:00
|
|
|
const result = await Services.forgotPassword(email);
|
2018-12-05 20:52:08 +00:00
|
|
|
if (result.success) {
|
2019-03-12 16:23:06 +00:00
|
|
|
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-17 20:06:17 +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'));
|
|
|
|
}
|
2022-09-06 17:44:31 +00:00
|
|
|
setIsFetching(false);
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2017-11-10 13:42:02 +00:00
|
|
|
|
2022-09-06 17:44:31 +00:00
|
|
|
return (
|
|
|
|
<FormContainer testID='forgot-password-view'>
|
|
|
|
<FormContainerInner>
|
|
|
|
<Text style={[sharedStyles.loginTitle, sharedStyles.textBold, { color: colors.titleText }]}>
|
|
|
|
{I18n.t('Forgot_password')}
|
|
|
|
</Text>
|
|
|
|
<ControlledFormTextInput
|
|
|
|
name='email'
|
|
|
|
control={control}
|
|
|
|
autoFocus
|
|
|
|
placeholder={I18n.t('Email')}
|
|
|
|
keyboardType='email-address'
|
|
|
|
returnKeyType='send'
|
|
|
|
iconLeft='mail'
|
|
|
|
onSubmitEditing={handleSubmit(resetPassword)}
|
|
|
|
testID='forgot-password-view-email'
|
|
|
|
containerStyle={sharedStyles.inputLastChild}
|
|
|
|
/>
|
|
|
|
<Button
|
|
|
|
title={I18n.t('Reset_password')}
|
|
|
|
type='primary'
|
|
|
|
onPress={handleSubmit(resetPassword)}
|
|
|
|
testID='forgot-password-view-submit'
|
|
|
|
loading={isFetching}
|
|
|
|
disabled={!isValid}
|
|
|
|
/>
|
|
|
|
</FormContainerInner>
|
|
|
|
</FormContainer>
|
|
|
|
);
|
|
|
|
};
|
2019-12-04 16:39:53 +00:00
|
|
|
|
2022-09-06 17:44:31 +00:00
|
|
|
export default ForgotPasswordView;
|