1+ /* eslint-disable no-unused-vars */
12/* eslint-disable max-len */
23/* eslint-disable import/extensions */
34/* eslint-disable jsx-a11y/label-has-associated-control */
45/* eslint-disable react/function-component-definition */
56/* eslint-disable jsx-a11y/anchor-is-valid */
6- import React from 'react' ;
7- import { Link } from 'react-router-dom' ;
7+ import React , { useState } from 'react' ;
8+ import { Link , useHistory } from 'react-router-dom' ;
9+ import { useMutation } from '@apollo/client' ;
810import useFormLogin from './useFormLogin' ;
911import validateLoginInfo from './validateLoginInfo.js' ;
1012import {
@@ -20,12 +22,57 @@ import {
2022 FormH2 ,
2123} from './styles' ;
2224import { signup } from '../../../config/content' ;
25+ import LOGIN from '../../../graphql/mutation/login' ;
26+ import { toErrorMap } from '../../../utils/toErrorMap' ;
2327
24- const FormLogin = ( { submitForm } ) => {
25- const { handleChange, values, handleSubmit, errors } = useFormLogin (
26- submitForm ,
27- validateLoginInfo ,
28- ) ;
28+ const FormLogin = ( ) => {
29+ const [ login , { loading, error } ] = useMutation ( LOGIN ) ;
30+ const history = useHistory ( ) ;
31+
32+ const [ isUsernameOrEmailError , setIsUsernameOrEmailError ] = useState ( false ) ;
33+ const [ isPasswordError , setIsPasswordError ] = useState ( false ) ;
34+ const [ usernameOrEmailErrorMessage , setUsernameOrEmailErrorMessage ] = useState ( '' ) ;
35+ const [ passwordErrorMessage , setPasswordErrorMessage ] = useState ( '' ) ;
36+
37+ const resetErrorStateValues = ( ) => {
38+ setIsUsernameOrEmailError ( false ) ;
39+ setIsPasswordError ( false ) ;
40+ setUsernameOrEmailErrorMessage ( '' ) ;
41+ setPasswordErrorMessage ( '' ) ;
42+ } ;
43+
44+ const handleSubmit = async ( event ) => {
45+ event . preventDefault ( ) ;
46+ resetErrorStateValues ( ) ;
47+ const data = new FormData ( event . currentTarget ) ;
48+
49+ const usernameOrEmail = data . get ( 'email' ) ;
50+ const password = data . get ( 'password' ) ;
51+
52+ const response = await login ( {
53+ variables : {
54+ userInfo : { usernameOrEmail, password } ,
55+ } ,
56+ } ) ;
57+
58+ if ( response . data ?. login . errors ) {
59+ const errorMapped = toErrorMap ( response . data . login . errors ) ;
60+ // console.log(toErrorMap(response.data.login.errors));
61+ if ( errorMapped . usernameOrEmail ) {
62+ setUsernameOrEmailErrorMessage ( errorMapped . usernameOrEmail ) ;
63+ setIsUsernameOrEmailError ( true ) ;
64+ }
65+
66+ if ( errorMapped . password ) {
67+ setPasswordErrorMessage ( errorMapped . password ) ;
68+ setIsPasswordError ( true ) ;
69+ }
70+ } else if ( response . data ?. login . user ) {
71+ // console.log('Login Successful');
72+ resetErrorStateValues ( ) ;
73+ history . push ( '/journal' ) ;
74+ }
75+ } ;
2976
3077 return (
3178 < FormContentRight >
@@ -34,25 +81,11 @@ const FormLogin = ({ submitForm }) => {
3481 < FormH2 > { signup . head2 } </ FormH2 >
3582 < FormInputs >
3683 < FormLabel htmlFor = 'email' > { signup . labelEmail } </ FormLabel >
37- < FormInput
38- id = 'email'
39- type = 'email'
40- name = 'email'
41- value = { values . email }
42- onChange = { handleChange }
43- />
44- { errors . email && < FormInputsP > { errors . email } </ FormInputsP > }
84+ < FormInput id = 'email' label = 'Email Address' type = 'email' name = 'email' />
4585 </ FormInputs >
4686 < FormInputs >
4787 < FormLabel htmlFor = 'password' > { signup . labelPassword } </ FormLabel >
48- < FormInput
49- id = 'password'
50- type = 'password'
51- name = 'password'
52- value = { values . password }
53- onChange = { handleChange }
54- />
55- { errors . password && < FormInputsP > { errors . password } </ FormInputsP > }
88+ < FormInput id = 'password' label = 'Password' type = 'password' name = 'password' />
5689 </ FormInputs >
5790 < ButtonContainer >
5891 < FormInputBtn type = 'submit' > { signup . buttonLogin } </ FormInputBtn >
0 commit comments