6
6
/* eslint-disable jsx-a11y/label-has-associated-control */
7
7
/* eslint-disable react/function-component-definition */
8
8
/* eslint-disable jsx-a11y/anchor-is-valid */
9
- import React , { useState , useReducer } from 'react' ;
10
- import { Link , useHistory } from 'react-router-dom' ;
11
- import { gql , useMutation } from '@apollo/client' ;
12
- import useForm from './useForm' ;
13
- import validate from './validateInfo' ;
9
+ import React , { useState } from 'react' ;
10
+ import { useHistory } from 'react-router-dom' ;
11
+ import { useMutation } from '@apollo/client' ;
14
12
import {
15
13
FormContentRight ,
16
14
FormDiv ,
@@ -26,136 +24,107 @@ import { signup } from '../../../config/content';
26
24
import REGISTER from '../../../graphql/mutation/register' ;
27
25
import { toErrorMap } from '../../../utils/toErrorMap' ;
28
26
29
- const ACTIONS = {
30
- EMAIL_ERROR : 'emailError' ,
31
- USERNAME_ERROR : 'usernameError' ,
32
- PASSWORD_ERROR : 'passwordError' ,
33
- RESET_FORM : 'resetForm' ,
34
- } ;
35
-
36
- const initialState = {
37
- isUsernameError : false ,
38
- isEmailError : false ,
39
- isPasswordError : false ,
40
- usernameErrorMessage : '' ,
41
- emailErrorMessage : '' ,
42
- passwordErrorMessage : '' ,
43
- } ;
44
-
45
- const reducer = ( state , action ) => {
46
- switch ( action . type ) {
47
- case ACTIONS . EMAIL_ERROR :
48
- return {
49
- ...state ,
50
- isEmailError : true ,
51
- emailErrorMessage : action . payload . errorMessage ,
52
- } ;
53
-
54
- case ACTIONS . USERNAME_ERROR :
55
- return {
56
- ...state ,
57
- isUsernameError : true ,
58
- usernameErrorMessage : action . payload . errorMessage ,
59
- } ;
60
-
61
- case ACTIONS . PASSWORD_ERROR :
62
- return {
63
- ...state ,
64
- isPasswordError : true ,
65
- passwordErrorMessage : action . payload . errorMessage ,
66
- } ;
67
-
68
- case ACTIONS . RESET_FORM :
69
- return {
70
- isUsernameError : false ,
71
- isEmailError : false ,
72
- isPasswordError : false ,
73
- usernameErrorMessage : '' ,
74
- emailErrorMessage : '' ,
75
- passwordErrorMessage : '' ,
76
- } ;
77
- }
78
- } ;
79
-
80
- const FormSignup = ( { submitForm } ) => {
27
+ const FormSignup = ( ) => {
81
28
const [ register , { loading, error } ] = useMutation ( REGISTER ) ;
82
-
83
- const [ state , dispatch ] = useReducer ( reducer , initialState ) ;
84
-
85
29
const history = useHistory ( ) ;
86
30
87
- const handleSubmit = async ( event ) => {
88
- event . preventDefault ( ) ;
31
+ const [ isFullNameError , setIsFullNameError ] = useState ( false ) ;
32
+ const [ isUsernameError , setIsUsernameError ] = useState ( false ) ;
33
+ const [ isEmailError , setIsEmailError ] = useState ( false ) ;
34
+ const [ isPasswordError , setIsPasswordError ] = useState ( false ) ;
35
+ const [ fullNameErrorMessage , setIsFullNameErrorMessage ] = useState ( '' ) ;
36
+ const [ usernameErrorMessage , setUsernameErrorMessage ] = useState ( '' ) ;
37
+ const [ emailErrorMessage , setEmailErrorMessage ] = useState ( '' ) ;
38
+ const [ passwordErrorMessage , setPasswordErrorMessage ] = useState ( '' ) ;
39
+
40
+ const resetErrorStateValues = ( ) => {
41
+ setIsFullNameError ( false ) ;
42
+ setIsUsernameError ( false ) ;
43
+ setIsEmailError ( false ) ;
44
+ setIsPasswordError ( false ) ;
45
+ setIsFullNameErrorMessage ( '' ) ;
46
+ setUsernameErrorMessage ( '' ) ;
47
+ setEmailErrorMessage ( '' ) ;
48
+ setPasswordErrorMessage ( '' ) ;
49
+ } ;
89
50
90
- dispatch ( { type : ACTIONS . RESET_FORM } ) ;
51
+ const handleSubmit1 = async ( event ) => {
52
+ event . preventDefault ( ) ;
53
+ resetErrorStateValues ( ) ;
91
54
const data = new FormData ( event . currentTarget ) ;
92
55
56
+ const fullName = data . get ( 'fullName' ) ;
93
57
const username = data . get ( 'username' ) ;
94
58
const email = data . get ( 'email' ) ;
95
59
const password = data . get ( 'password' ) ;
96
60
97
61
const response = await register ( {
98
62
variables : {
99
- userInfo : { username, email, password, fullName : 'fullNameRegister' } ,
63
+ userInfo : { fullName , username, email, password } ,
100
64
} ,
101
65
} ) ;
102
66
103
67
if ( response . data ?. register . errors ) {
104
68
const errorMapped = toErrorMap ( response . data . register . errors ) ;
105
-
69
+ if ( errorMapped . fullName ) {
70
+ setIsFullNameErrorMessage ( errorMapped . fullName ) ;
71
+ setIsFullNameError ( true ) ;
72
+ }
73
+ if ( errorMapped . username ) {
74
+ setUsernameErrorMessage ( errorMapped . username ) ;
75
+ setIsUsernameError ( true ) ;
76
+ }
106
77
if ( errorMapped . email ) {
107
- dispatch ( {
108
- type : ACTIONS . EMAIL_ERROR ,
109
- payload : { errorMessage : errorMapped . email } ,
110
- } ) ;
78
+ setEmailErrorMessage ( errorMapped . email ) ;
79
+ setIsEmailError ( true ) ;
80
+ }
81
+ if ( ! / \S + @ \S + \. \S + / . test ( email ) ) {
82
+ setEmailErrorMessage ( 'Invalid email' ) ;
83
+ setIsEmailError ( true ) ;
111
84
}
112
-
113
85
if ( errorMapped . password ) {
114
- dispatch ( {
115
- type : ACTIONS . PASSWORD_ERROR ,
116
- payload : { errorMessage : errorMapped . password } ,
117
- } ) ;
86
+ setPasswordErrorMessage ( errorMapped . password ) ;
87
+ setIsPasswordError ( true ) ;
118
88
}
119
-
120
- if ( errorMapped . username ) {
121
- dispatch ( {
122
- type : ACTIONS . USERNAME_ERROR ,
123
- payload : { errorMessage : errorMapped . username } ,
124
- } ) ;
89
+ if ( ! password ) {
90
+ setPasswordErrorMessage ( 'Password required' ) ;
91
+ setIsPasswordError ( true ) ;
92
+ }
93
+ if ( password . length < 6 ) {
94
+ setPasswordErrorMessage ( 'Password needs to be 6 characters or more' ) ;
95
+ setIsPasswordError ( true ) ;
125
96
}
126
- } else if ( response . data ? .register . user ) {
127
- dispatch ( { type : ACTIONS . RESET_FORM } ) ;
128
- history . push ( '/login ' ) ;
97
+ } else if ( response . data . register . user ) {
98
+ resetErrorStateValues ( ) ;
99
+ history . push ( '/journal ' ) ;
129
100
}
130
101
} ;
131
102
132
103
return (
133
104
< FormContentRight >
134
- < FormDiv onSubmit = { handleSubmit } >
105
+ < FormDiv onSubmit = { handleSubmit1 } >
135
106
< FormH1 > { signup . head } </ FormH1 >
107
+ < FormInputs >
108
+ < FormLabel htmlFor = 'fullName' > Enter Full Name</ FormLabel >
109
+ < FormInput id = 'fullName' label = 'Username' type = 'text' name = 'fullName' required />
110
+ { isFullNameError && < FormInputsP > { fullNameErrorMessage } </ FormInputsP > }
111
+ </ FormInputs >
136
112
< FormInputs >
137
113
< FormLabel htmlFor = 'username' > { signup . labelUsername } </ FormLabel >
138
- < FormInput id = 'username' label = 'Username' name = 'username' required />
139
- { state . isUsernameError && < FormInputsP > { state . usernameErrorMessage } </ FormInputsP > }
114
+ < FormInput id = 'username' label = 'Username' type = 'text' name = 'username' required />
115
+ { isUsernameError && < FormInputsP > { usernameErrorMessage } </ FormInputsP > }
140
116
</ FormInputs >
141
117
< FormInputs >
142
118
< FormLabel htmlFor = 'email' > { signup . labelEmail } </ FormLabel >
143
- < FormInput id = 'email' label = 'Email Address' autoComplete = 'email' name = 'email' required />
144
- { state . isEmailError && < FormInputsP > { state . emailErrorMessage } </ FormInputsP > }
119
+ < FormInput id = 'email' label = 'Email Address' type = 'email' name = 'email' required />
120
+ { isEmailError && < FormInputsP > { emailErrorMessage } </ FormInputsP > }
145
121
</ FormInputs >
146
122
< FormInputs >
147
123
< FormLabel htmlFor = 'password' > { signup . labelPassword } </ FormLabel >
148
- < FormInput id = 'password' label = 'Password' name = 'password' type = 'password' required />
149
- { state . isPasswordError && < FormInputsP > { state . passwordErrorMessage } </ FormInputsP > }
150
- </ FormInputs >
151
- < FormInputs >
152
- < FormLabel htmlFor = 'password2' > { signup . labelPassword2 } </ FormLabel >
153
- < FormInput id = 'password2' type = 'password' name = 'password2' required />
154
- { state . isPasswordError && < FormInputsP > { state . passwordErrorMessage } </ FormInputsP > }
124
+ < FormInput id = 'password' label = 'Password' type = 'password' name = 'password' required />
125
+ { isPasswordError || < FormInputsP > { passwordErrorMessage } </ FormInputsP > }
155
126
</ FormInputs >
156
- < FormInputBtn signup type = 'submit' >
157
- { signup . button }
158
- </ FormInputBtn >
127
+ < FormInputBtn type = 'submit' > { signup . button } </ FormInputBtn >
159
128
< FormInputLogin >
160
129
{ signup . login } { ' ' }
161
130
< a href = '/login' style = { { color : 'orange' } } >
0 commit comments