Add form control and api logic

This commit is contained in:
Hazem Krimi
2021-04-28 21:29:38 +01:00
parent 246e965fd3
commit 7cad9ee520
+88 -9
View File
@@ -1,9 +1,69 @@
import * as Yup from 'yup';
import { useFormik } from 'formik';
import { useMutation } from '@apollo/client';
import { useHistory } from 'react-router';
import { useState } from 'react';
import { tokenVar, roleVar, userVar } from '../../../graphql/state';
import { Google, Login as LoginIllustration, Logo } from '../../../assets'; import { Google, Login as LoginIllustration, Logo } from '../../../assets';
import { Box, Button, Input, Link, Text } from '../../../components'; import { Alert, Box, Button, Input, Link, Text } from '../../../components';
import { theme } from '../../../themes'; import { theme } from '../../../themes';
import { Wrapper } from './styles'; import { Wrapper } from './styles';
import { LOGIN } from '../../../graphql/auth.api';
import { LoginMutation, LoginMutationVariables } from '../../../graphql/types';
const Login = () => { const Login = () => {
const history = useHistory();
const [error, setError] = useState<string>('');
const [login, { loading }] = useMutation<
LoginMutation,
LoginMutationVariables
>(LOGIN, {
onCompleted({ login: { user, token } }) {
switch (user.role) {
case 'Client':
roleVar('client');
break;
case 'ProductOwner':
roleVar('productOwner');
break;
case 'Developer':
roleVar('developer');
break;
case 'Admin':
roleVar('admin');
break;
default:
break;
}
tokenVar(token);
userVar(user);
history.push('/');
},
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'),
password: Yup.string()
.required('Password is required')
.min(6, 'Password is 6 characters minimum'),
}),
onSubmit: ({ email, password }) => {
login({ variables: { email, password } });
},
});
return ( return (
<Wrapper> <Wrapper>
<Box <Box
@@ -22,13 +82,19 @@ const Login = () => {
<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'>
Login Login
</Text> </Text>
{error && <Alert color='error' text={error} />}
</Box>
<form onSubmit={form.handleSubmit}>
<Box <Box
display='grid' display='grid'
gridTemplateColumns='auto' gridTemplateColumns='auto'
@@ -36,13 +102,24 @@ const Login = () => {
rowGap='0.5rem' rowGap='0.5rem'
padding='1.563rem 0rem' padding='1.563rem 0rem'
> >
<Input label='Email' name='email' value='' onChange={() => {}} /> <Input
label='Email'
name='email'
value={form.values.email}
onChange={form.handleChange}
onBlur={form.handleBlur}
error={!!form.errors.email}
errorMessage={form.errors.email}
/>
<Input <Input
label='Password' label='Password'
name='password' name='password'
type='password' type='password'
value='' value={form.values.password}
onChange={() => {}} onChange={form.handleChange}
onBlur={form.handleBlur}
error={!!form.errors.password}
errorMessage={form.errors.password}
/> />
<Box textAlign='right'> <Box textAlign='right'>
<Link href='/forgot-password'>Forgot Password</Link> <Link href='/forgot-password'>Forgot Password</Link>
@@ -53,7 +130,9 @@ const Login = () => {
fullWidth fullWidth
color='client' color='client'
text='Login' text='Login'
onClick={() => {}} type='submit'
loading={loading}
disabled={loading}
/> />
<Button <Button
variant='secondary-action' variant='secondary-action'
@@ -61,7 +140,6 @@ const Login = () => {
color='client' color='client'
text='Login with Google' text='Login with Google'
iconLeft={<Google />} iconLeft={<Google />}
onClick={() => {}}
/> />
</Box> </Box>
<Box display='flex' flexDirection='row'> <Box display='flex' flexDirection='row'>
@@ -76,6 +154,7 @@ const Login = () => {
</Link> </Link>
</Box> </Box>
</Box> </Box>
</form>
</Box> </Box>
<Box <Box
background={theme.colors.client.main} background={theme.colors.client.main}