Add form control and api logic

This commit is contained in:
Hazem Krimi
2021-04-28 21:29:59 +01:00
parent 7cad9ee520
commit 610726ad1b
+71 -8
View File
@@ -1,9 +1,50 @@
import * as Yup from 'yup';
import { useMutation } from '@apollo/client';
import { useFormik } from 'formik';
import { useState } from 'react';
import { Login as LoginIllustration, Logo } from '../../../assets'; import { Login as LoginIllustration, Logo } from '../../../assets';
import { Box, Button, Input, Link, Text } from '../../../components'; import { Box, Button, Input, Link, Text, Alert } from '../../../components';
import { RESET_PASSWORD } from '../../../graphql/auth.api';
import {
ResetPasswordMutation,
ResetPasswordMutationVariables,
} from '../../../graphql/types';
import { theme } from '../../../themes'; import { theme } from '../../../themes';
import { Wrapper } from './styles'; import { Wrapper } from './styles';
const ForgotPassword = () => { const ForgotPassword = () => {
const [error, setError] = useState<string>('');
const [success, setSuccess] = useState<boolean>(false);
const [resetPassword, { loading }] = useMutation<
ResetPasswordMutation,
ResetPasswordMutationVariables
>(RESET_PASSWORD, {
onCompleted() {
setSuccess(true);
setTimeout(() => setSuccess(false), 3000);
},
onError({ graphQLErrors }) {
setError(graphQLErrors[0]?.extensions?.info);
setTimeout(() => setError(''), 3000);
},
});
const form = useFormik({
initialValues: {
email: '',
password: '',
},
validationSchema: Yup.object().shape({
email: Yup.string()
.required('Email is required')
.email('Email is invalid'),
}),
onSubmit: ({ email }) => {
resetPassword({ variables: { email } });
},
});
return ( return (
<Wrapper> <Wrapper>
<Box <Box
@@ -22,13 +63,25 @@ const ForgotPassword = () => {
<Box marginRight='0.625rem'> <Box marginRight='0.625rem'>
<Logo /> <Logo />
</Box> </Box>
<Text variant='headline' weight='bold'>
astrobuild
</Text>
</Box> </Box>
<Box
display='grid'
gridTemplateColumns='auto 1fr'
columnGap='1rem'
alignItems='center'
>
<Text variant='headline' weight='bold'> <Text variant='headline' weight='bold'>
Forgot Password Forgot Password
</Text> </Text>
{error && <Alert color='error' text={error} />}
{success && (
<Alert
color='success'
text='Check your email to recover your account'
/>
)}
</Box>
<form onSubmit={form.handleSubmit}>
<Box <Box
display='grid' display='grid'
gridTemplateColumns='auto' gridTemplateColumns='auto'
@@ -36,8 +89,15 @@ const ForgotPassword = () => {
rowGap='0.5rem' rowGap='0.5rem'
padding='1.563rem 0rem' padding='1.563rem 0rem'
> >
<Input label='Email' name='email' value='' onChange={() => {}} /> <Input
<Input label='Code' name='code' value='' onChange={() => {}} /> label='Email'
name='email'
value={form.values.email}
onChange={form.handleChange}
onBlur={form.handleBlur}
error={!!form.errors.email}
errorMessage={form.errors.email}
/>
<Box <Box
display='grid' display='grid'
gridTemplateColumns='auto' gridTemplateColumns='auto'
@@ -48,8 +108,10 @@ const ForgotPassword = () => {
variant='primary-action' variant='primary-action'
fullWidth fullWidth
color='client' color='client'
text='Receive Code' type='submit'
onClick={() => {}} text='Send Reset Link'
loading={loading}
disabled={loading}
/> />
</Box> </Box>
<Box display='flex' flexDirection='row'> <Box display='flex' flexDirection='row'>
@@ -64,6 +126,7 @@ const ForgotPassword = () => {
</Link> </Link>
</Box> </Box>
</Box> </Box>
</form>
</Box> </Box>
<Box <Box
background={theme.colors.client.main} background={theme.colors.client.main}