From c9ce52958c5c9eb3c195a7ae3885491c2b09a2ed Mon Sep 17 00:00:00 2001 From: Reinaldo Neto <47038980+reinaldonetof@users.noreply.github.com> Date: Tue, 6 Sep 2022 14:44:31 -0300 Subject: [PATCH] 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 --- app/stacks/OutsideStack.tsx | 2 +- app/views/ForgotPasswordView.tsx | 150 ++++++++++++++----------------- 2 files changed, 66 insertions(+), 86 deletions(-) diff --git a/app/stacks/OutsideStack.tsx b/app/stacks/OutsideStack.tsx index 1ae5e42f9..8ffc8ae85 100644 --- a/app/stacks/OutsideStack.tsx +++ b/app/stacks/OutsideStack.tsx @@ -25,7 +25,7 @@ const _OutsideStack = () => { - + diff --git a/app/views/ForgotPasswordView.tsx b/app/views/ForgotPasswordView.tsx index 4602526a5..ab7b94f76 100644 --- a/app/views/ForgotPasswordView.tsx +++ b/app/views/ForgotPasswordView.tsx @@ -1,75 +1,58 @@ -import React from 'react'; +import React, { useLayoutEffect, useState } from 'react'; 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 FormContainer, { FormContainerInner } from '../containers/FormContainer'; -import { FormTextInput } from '../containers/TextInput'; +import { ControlledFormTextInput } from '../containers/TextInput'; import I18n from '../i18n'; -import { themes } from '../lib/constants'; import { Services } from '../lib/services'; import { OutsideParamList } from '../stacks/types'; -import { withTheme } from '../theme'; -import { showErrorAlert, isValidEmail } from '../lib/methods/helpers'; +import { useTheme } from '../theme'; +import { showErrorAlert } from '../lib/methods/helpers'; import { events, logEvent } from '../lib/methods/helpers/log'; -import { IBaseScreen } from '../definitions'; import sharedStyles from './Styles'; -interface IForgotPasswordViewState { +const schema = yup.object().shape({ + email: yup.string().email().required() +}); + +interface ISubmit { email: string; - invalidEmail: boolean; - isFetching: boolean; } -type IForgotPasswordViewProps = IBaseScreen; +const ForgotPasswordView = (): React.ReactElement => { + const { + control, + handleSubmit, + formState: { isValid } + } = useForm({ mode: 'onChange', resolver: yupResolver(schema) }); -class ForgotPasswordView extends React.Component { - static navigationOptions = ({ route }: IForgotPasswordViewProps) => ({ - title: route.params?.title ?? 'Rocket.Chat' - }); + const [isFetching, setIsFetching] = useState(false); - state = { - email: '', - invalidEmail: true, - isFetching: false - }; + const navigation = useNavigation>(); + const { params } = useRoute>(); + const { colors } = useTheme(); - 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; - } + useLayoutEffect(() => { + navigation.setOptions({ + title: params?.title ?? 'Rocket.Chat' + }); + }, [navigation, params?.title]); - validate = (email: string) => { - if (!isValidEmail(email)) { - this.setState({ invalidEmail: true }); - return; - } - this.setState({ email, invalidEmail: false }); - }; - - resetPassword = async () => { - logEvent(events.FP_FORGOT_PASSWORD); - const { email, invalidEmail } = this.state; - if (invalidEmail || !email) { + const resetPassword = async ({ email }: ISubmit) => { + if (!isValid) { return; } try { - this.setState({ isFetching: true }); + logEvent(events.FP_FORGOT_PASSWORD); + setIsFetching(true); const result = await Services.forgotPassword(email); if (result.success) { - const { navigation } = this.props; navigation.pop(); showErrorAlert(I18n.t('Forgot_password_If_this_email_is_registered'), I18n.t('Alert')); } @@ -78,41 +61,38 @@ class ForgotPasswordView extends React.Component + + + {I18n.t('Forgot_password')} + + +