Chore: Hooks app/views/ForgotPasswordView (#4485)

* Chore: Hooks app/views/ForgotPasswordView

* validating email and using hook forms

* using mode onCHange

* add return

* fix theme

Co-authored-by: Gleidson Daniel <gleidson10daniel@hotmail.com>
This commit is contained in:
Reinaldo Neto 2022-09-06 14:44:31 -03:00 committed by Diego Mello
parent bd117c401f
commit 99d1bb6d8a
2 changed files with 66 additions and 86 deletions

View File

@ -25,7 +25,7 @@ const _OutsideStack = () => {
<Outside.Screen name='NewServerView' component={NewServerView} options={NewServerView.navigationOptions} /> <Outside.Screen name='NewServerView' component={NewServerView} options={NewServerView.navigationOptions} />
<Outside.Screen name='WorkspaceView' component={WorkspaceView} options={WorkspaceView.navigationOptions} /> <Outside.Screen name='WorkspaceView' component={WorkspaceView} options={WorkspaceView.navigationOptions} />
<Outside.Screen name='LoginView' component={LoginView} options={LoginView.navigationOptions} /> <Outside.Screen name='LoginView' component={LoginView} options={LoginView.navigationOptions} />
<Outside.Screen name='ForgotPasswordView' component={ForgotPasswordView} options={ForgotPasswordView.navigationOptions} /> <Outside.Screen name='ForgotPasswordView' component={ForgotPasswordView} />
<Outside.Screen name='SendEmailConfirmationView' component={SendEmailConfirmationView} /> <Outside.Screen name='SendEmailConfirmationView' component={SendEmailConfirmationView} />
<Outside.Screen name='RegisterView' component={RegisterView} options={RegisterView.navigationOptions} /> <Outside.Screen name='RegisterView' component={RegisterView} options={RegisterView.navigationOptions} />
<Outside.Screen name='LegalView' component={LegalView} /> <Outside.Screen name='LegalView' component={LegalView} />

View File

@ -1,75 +1,58 @@
import React from 'react'; import React, { useLayoutEffect, useState } from 'react';
import { Text } from 'react-native'; import { Text } from 'react-native';
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';
import Button from '../containers/Button'; import Button from '../containers/Button';
import FormContainer, { FormContainerInner } from '../containers/FormContainer'; import FormContainer, { FormContainerInner } from '../containers/FormContainer';
import { FormTextInput } from '../containers/TextInput'; import { ControlledFormTextInput } from '../containers/TextInput';
import I18n from '../i18n'; import I18n from '../i18n';
import { themes } from '../lib/constants';
import { Services } from '../lib/services'; import { Services } from '../lib/services';
import { OutsideParamList } from '../stacks/types'; import { OutsideParamList } from '../stacks/types';
import { withTheme } from '../theme'; import { useTheme } from '../theme';
import { showErrorAlert, isValidEmail } from '../lib/methods/helpers'; import { showErrorAlert } from '../lib/methods/helpers';
import { events, logEvent } from '../lib/methods/helpers/log'; import { events, logEvent } from '../lib/methods/helpers/log';
import { IBaseScreen } from '../definitions';
import sharedStyles from './Styles'; import sharedStyles from './Styles';
interface IForgotPasswordViewState { const schema = yup.object().shape({
email: string; email: yup.string().email().required()
invalidEmail: boolean;
isFetching: boolean;
}
type IForgotPasswordViewProps = IBaseScreen<OutsideParamList, 'ForgotPasswordView'>;
class ForgotPasswordView extends React.Component<IForgotPasswordViewProps, IForgotPasswordViewState> {
static navigationOptions = ({ route }: IForgotPasswordViewProps) => ({
title: route.params?.title ?? 'Rocket.Chat'
}); });
state = { interface ISubmit {
email: '', email: string;
invalidEmail: true,
isFetching: false
};
shouldComponentUpdate(nextProps: IForgotPasswordViewProps, nextState: IForgotPasswordViewState) {
const { email, invalidEmail, isFetching } = this.state;
const { theme } = this.props;
if (nextProps.theme !== theme) {
return true;
}
if (nextState.email !== email) {
return true;
}
if (nextState.invalidEmail !== invalidEmail) {
return true;
}
if (nextState.isFetching !== isFetching) {
return true;
}
return false;
} }
validate = (email: string) => { const ForgotPasswordView = (): React.ReactElement => {
if (!isValidEmail(email)) { const {
this.setState({ invalidEmail: true }); control,
return; handleSubmit,
} formState: { isValid }
this.setState({ email, invalidEmail: false }); } = useForm<ISubmit>({ mode: 'onChange', resolver: yupResolver(schema) });
};
resetPassword = async () => { const [isFetching, setIsFetching] = useState(false);
logEvent(events.FP_FORGOT_PASSWORD);
const { email, invalidEmail } = this.state; const navigation = useNavigation<StackNavigationProp<OutsideParamList, 'ForgotPasswordView'>>();
if (invalidEmail || !email) { const { params } = useRoute<RouteProp<OutsideParamList, 'ForgotPasswordView'>>();
const { colors } = useTheme();
useLayoutEffect(() => {
navigation.setOptions({
title: params?.title ?? 'Rocket.Chat'
});
}, [navigation, params?.title]);
const resetPassword = async ({ email }: ISubmit) => {
if (!isValid) {
return; return;
} }
try { try {
this.setState({ isFetching: true }); logEvent(events.FP_FORGOT_PASSWORD);
setIsFetching(true);
const result = await Services.forgotPassword(email); const result = await Services.forgotPassword(email);
if (result.success) { if (result.success) {
const { navigation } = this.props;
navigation.pop(); navigation.pop();
showErrorAlert(I18n.t('Forgot_password_If_this_email_is_registered'), I18n.t('Alert')); showErrorAlert(I18n.t('Forgot_password_If_this_email_is_registered'), I18n.t('Alert'));
} }
@ -78,41 +61,38 @@ class ForgotPasswordView extends React.Component<IForgotPasswordViewProps, IForg
const msg = (e.data && e.data.error) || I18n.t('There_was_an_error_while_action', { action: I18n.t('resetting_password') }); const msg = (e.data && e.data.error) || I18n.t('There_was_an_error_while_action', { action: I18n.t('resetting_password') });
showErrorAlert(msg, I18n.t('Alert')); showErrorAlert(msg, I18n.t('Alert'));
} }
this.setState({ isFetching: false }); setIsFetching(false);
}; };
render() {
const { invalidEmail, isFetching } = this.state;
const { theme } = this.props;
return ( return (
<FormContainer testID='forgot-password-view'> <FormContainer testID='forgot-password-view'>
<FormContainerInner> <FormContainerInner>
<Text style={[sharedStyles.loginTitle, sharedStyles.textBold, { color: themes[theme].titleText }]}> <Text style={[sharedStyles.loginTitle, sharedStyles.textBold, { color: colors.titleText }]}>
{I18n.t('Forgot_password')} {I18n.t('Forgot_password')}
</Text> </Text>
<FormTextInput <ControlledFormTextInput
name='email'
control={control}
autoFocus autoFocus
placeholder={I18n.t('Email')} placeholder={I18n.t('Email')}
keyboardType='email-address' keyboardType='email-address'
returnKeyType='send' returnKeyType='send'
onChangeText={email => this.validate(email)} iconLeft='mail'
onSubmitEditing={this.resetPassword} onSubmitEditing={handleSubmit(resetPassword)}
testID='forgot-password-view-email' testID='forgot-password-view-email'
containerStyle={sharedStyles.inputLastChild} containerStyle={sharedStyles.inputLastChild}
/> />
<Button <Button
title={I18n.t('Reset_password')} title={I18n.t('Reset_password')}
type='primary' type='primary'
onPress={this.resetPassword} onPress={handleSubmit(resetPassword)}
testID='forgot-password-view-submit' testID='forgot-password-view-submit'
loading={isFetching} loading={isFetching}
disabled={invalidEmail} disabled={!isValid}
/> />
</FormContainerInner> </FormContainerInner>
</FormContainer> </FormContainer>
); );
} };
}
export default withTheme(ForgotPasswordView); export default ForgotPasswordView;