1
+ /* eslint-disable no-unused-vars */
1
2
/* eslint-disable max-len */
2
3
/* eslint-disable import/extensions */
3
4
/* eslint-disable jsx-a11y/label-has-associated-control */
4
5
/* eslint-disable react/function-component-definition */
5
6
/* 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' ;
8
10
import useFormLogin from './useFormLogin' ;
9
11
import validateLoginInfo from './validateLoginInfo.js' ;
10
12
import {
@@ -20,12 +22,57 @@ import {
20
22
FormH2 ,
21
23
} from './styles' ;
22
24
import { signup } from '../../../config/content' ;
25
+ import LOGIN from '../../../graphql/mutation/login' ;
26
+ import { toErrorMap } from '../../../utils/toErrorMap' ;
23
27
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
+ } ;
29
76
30
77
return (
31
78
< FormContentRight >
@@ -34,25 +81,11 @@ const FormLogin = ({ submitForm }) => {
34
81
< FormH2 > { signup . head2 } </ FormH2 >
35
82
< FormInputs >
36
83
< 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' />
45
85
</ FormInputs >
46
86
< FormInputs >
47
87
< 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' />
56
89
</ FormInputs >
57
90
< ButtonContainer >
58
91
< FormInputBtn type = 'submit' > { signup . buttonLogin } </ FormInputBtn >
0 commit comments