From 06eefdb58309c2853dff93328db2ec77fdf6e389 Mon Sep 17 00:00:00 2001 From: Hazem Krimi Date: Thu, 29 Apr 2021 19:11:28 +0100 Subject: [PATCH] Hide google signup and complete signup logic --- src/pages/Auth/Signup/index.tsx | 43 ++++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 12 deletions(-) diff --git a/src/pages/Auth/Signup/index.tsx b/src/pages/Auth/Signup/index.tsx index cf6bd22..f99af50 100644 --- a/src/pages/Auth/Signup/index.tsx +++ b/src/pages/Auth/Signup/index.tsx @@ -2,15 +2,39 @@ import * as Yup from 'yup'; import { useFormik } from 'formik'; import { useState } from 'react'; import { useHistory } from 'react-router-dom'; -import { Google, Signup as SignupIllustration, Logo } from '../../../assets'; +import { useMutation } from '@apollo/client'; +import { Signup as SignupIllustration, Logo } from '../../../assets'; import { Box, Button, Input, Link, Text, Alert } from '../../../components'; import { theme } from '../../../themes'; import { Wrapper } from './styles'; +import { + SignupMutation, + SignupMutationVariables, +} from '../../../graphql/types'; +import { SIGNUP } from '../../../graphql/auth.api'; +import { roleVar, tokenVar, userVar } from '../../../graphql/state'; const Signup = () => { const history = useHistory(); const [error, setError] = useState(''); + const [signup, { loading }] = useMutation< + SignupMutation, + SignupMutationVariables + >(SIGNUP, { + onCompleted({ signup: { token, user } }) { + roleVar('client'); + tokenVar(token); + userVar(user); + localStorage.setItem('token', token); + history.push('/additional-info'); + }, + onError({ graphQLErrors }) { + setError(graphQLErrors[0]?.extensions?.info); + setTimeout(() => setError(''), 3000); + }, + }); + const form = useFormik({ initialValues: { email: '', @@ -24,15 +48,8 @@ const Signup = () => { .required('Password is required') .min(6, 'Password is 6 characters minimum'), }), - onSubmit: ({ email, password }, { resetForm }) => { - try { - history.push(`/additional-info?email=${email}&password=${password}`); - } catch (err) { - setError(err.message); - setTimeout(() => setError(''), 3000); - } finally { - resetForm(); - } + onSubmit: ({ email, password }) => { + signup({ variables: { email, password } }); }, }); @@ -105,14 +122,16 @@ const Signup = () => { type='submit' color='client' text='Signup' + loading={loading} + disabled={loading} /> -