From 246e965fd30fd09d6fff0628e7a8da18c87fde74 Mon Sep 17 00:00:00 2001 From: Hazem Krimi Date: Wed, 28 Apr 2021 21:29:27 +0100 Subject: [PATCH] Add form control and api logic --- src/pages/Auth/RecoverAccount/index.tsx | 126 ++++++++++++++++++------ 1 file changed, 95 insertions(+), 31 deletions(-) diff --git a/src/pages/Auth/RecoverAccount/index.tsx b/src/pages/Auth/RecoverAccount/index.tsx index fa102d1..c2a80c8 100644 --- a/src/pages/Auth/RecoverAccount/index.tsx +++ b/src/pages/Auth/RecoverAccount/index.tsx @@ -1,9 +1,58 @@ +import * as Yup from 'yup'; +import { useMutation } from '@apollo/client'; +import { useFormik } from 'formik'; +import { useState } from 'react'; +import { useHistory } from 'react-router-dom'; import { Login as LoginIllustration, Logo } from '../../../assets'; -import { Box, Button, Input, Text } from '../../../components'; +import { Box, Button, Input, Text, Alert } from '../../../components'; +import { CONFIRM_USER_RESET_PASSWORD } from '../../../graphql/auth.api'; +import { + ConfirmUserResetPasswordMutation, + ConfirmUserResetPasswordMutationVariables, +} from '../../../graphql/types'; import { theme } from '../../../themes'; import { Wrapper } from './styles'; const RecoverAccount = () => { + const params = new URLSearchParams(window.location.search); + const history = useHistory(); + const [error, setError] = useState(''); + + const [confirmResetPassword, { loading }] = useMutation< + ConfirmUserResetPasswordMutation, + ConfirmUserResetPasswordMutationVariables + >(CONFIRM_USER_RESET_PASSWORD, { + onCompleted() { + history.push('/login'); + }, + onError({ graphQLErrors }) { + setError(graphQLErrors[0]?.extensions?.info); + setTimeout(() => setError(''), 3000); + }, + }); + + const form = useFormik({ + initialValues: { + newPassword: '', + confirmNewPassword: '', + }, + validationSchema: Yup.object().shape({ + newPassword: Yup.string() + .required('New password is required') + .min(6, 'new Password is 6 characters minimum'), + confirmNewPassword: Yup.string().oneOf( + [Yup.ref('newPassword')], + "Passwords don't match" + ), + }), + onSubmit: ({ newPassword }) => { + confirmResetPassword({ + // eslint-disable-next-line + variables: { id: params.get('code')!, password: newPassword }, + }); + }, + }); + return ( { - - astrobuild - - - Recover Account - - {}} - /> - {}} - /> + + Recover Account + + {error && } + +
-