@@ -9,43 +9,51 @@ import Button from '../../../common/Button';
9
9
import apiClient from '../../../utils/apiClient' ;
10
10
import useSyncFormTranslations from '../../../common/useSyncFormTranslations' ;
11
11
12
- const debounce = ( func , delay ) => {
13
- let timer ;
14
- return ( ...args ) =>
15
- new Promise ( ( resolve ) => {
16
- clearTimeout ( timer ) ;
17
- timer = setTimeout ( ( ) => resolve ( func ( ...args ) ) , delay ) ;
18
- } ) ;
19
- } ;
12
+ const timeoutRef = { current : null } ;
20
13
21
- // API Validation Function
22
- async function asyncValidate ( fieldToValidate , value ) {
14
+ function asyncValidate ( fieldToValidate , value ) {
23
15
if ( ! value || value . trim ( ) . length === 0 ) {
24
- return '' ;
16
+ return Promise . resolve ( '' ) ;
25
17
}
18
+
26
19
const queryParams = {
27
20
[ fieldToValidate ] : value ,
28
21
check_type : fieldToValidate
29
22
} ;
30
23
31
- try {
32
- const response = await apiClient . get ( '/signup/duplicate_check' , {
33
- params : queryParams
34
- } ) ;
35
- return response . data . exists ? response . data . message : '' ;
36
- } catch ( error ) {
37
- return 'Error validating field.' ;
38
- }
24
+ return new Promise ( ( resolve ) => {
25
+ if ( timeoutRef . current ) {
26
+ timeoutRef . current ( ) ;
27
+ }
28
+
29
+ const timerId = setTimeout ( ( ) => {
30
+ apiClient
31
+ . get ( '/signup/duplicate_check' , { params : queryParams } )
32
+ . then ( ( response ) => {
33
+ if ( response . data . exists ) {
34
+ resolve ( response . data . message ) ;
35
+ } else {
36
+ resolve ( '' ) ;
37
+ }
38
+ } )
39
+ . catch ( ( ) => {
40
+ resolve ( 'An error occurred while validating' ) ;
41
+ } ) ;
42
+ } , 300 ) ;
43
+
44
+ timeoutRef . current = ( ) => {
45
+ clearTimeout ( timerId ) ;
46
+ resolve ( '' ) ;
47
+ } ;
48
+ } ) ;
39
49
}
40
50
41
- const debouncedAsyncValidate = debounce ( asyncValidate , 300 ) ;
42
-
43
51
function validateUsername ( username ) {
44
- return debouncedAsyncValidate ( 'username' , username ) ;
52
+ return asyncValidate ( 'username' , username ) ;
45
53
}
46
54
47
55
function validateEmail ( email ) {
48
- return debouncedAsyncValidate ( 'email' , email ) ;
56
+ return asyncValidate ( 'email' , email ) ;
49
57
}
50
58
51
59
function SignupForm ( ) {
@@ -62,26 +70,29 @@ function SignupForm() {
62
70
setShowConfirmPassword ( ! showConfirmPassword ) ;
63
71
64
72
function onSubmit ( formProps ) {
73
+ console . log ( "it's happening" ) ;
65
74
return dispatch ( validateAndSignUpUser ( formProps ) ) ;
66
75
}
67
76
68
77
return (
69
78
< Form
70
79
fields = { [ 'username' , 'email' , 'password' , 'confirmPassword' ] }
71
80
validate = { validateSignup }
72
- onSubmit = { onSubmit }
81
+ onSubmit = { ( values ) => {
82
+ console . log ( 'Form onSubmit triggered' , values ) ;
83
+ return onSubmit ( values ) ;
84
+ } }
73
85
>
74
86
{ ( { handleSubmit, pristine, submitting, invalid, form } ) => {
75
87
formRef . current = form ;
76
88
return (
77
89
< form className = "form" onSubmit = { handleSubmit } >
78
- { /* Username Field */ }
79
90
< Field
80
91
name = "username"
81
92
validate = { validateUsername }
82
93
validateFields = { [ ] }
83
94
>
84
- { ( { input , meta } ) => (
95
+ { ( field ) => (
85
96
< div className = "form__field" >
86
97
< label htmlFor = "username" className = "form__label" >
87
98
{ t ( 'SignupForm.Title' ) }
@@ -93,20 +104,18 @@ function SignupForm() {
93
104
id = "username"
94
105
autoComplete = "username"
95
106
autoCapitalize = "none"
96
- { ...input }
107
+ { ...field . input }
97
108
/>
98
- { meta . touched && meta . error && (
109
+ { field . meta . touched && field . meta . error && (
99
110
< span className = "form-error" aria-live = "polite" >
100
- { meta . error }
111
+ { field . meta . error }
101
112
</ span >
102
113
) }
103
114
</ div >
104
115
) }
105
116
</ Field >
106
-
107
- { /* Email Field */ }
108
117
< Field name = "email" validate = { validateEmail } validateFields = { [ ] } >
109
- { ( { input , meta } ) => (
118
+ { ( field ) => (
110
119
< div className = "form__field" >
111
120
< label htmlFor = "email" className = "form__label" >
112
121
{ t ( 'SignupForm.Email' ) }
@@ -117,20 +126,18 @@ function SignupForm() {
117
126
type = "email"
118
127
id = "email"
119
128
autoComplete = "email"
120
- { ...input }
129
+ { ...field . input }
121
130
/>
122
- { meta . touched && meta . error && (
131
+ { field . meta . touched && field . meta . error && (
123
132
< span className = "form-error" aria-live = "polite" >
124
- { meta . error }
133
+ { field . meta . error }
125
134
</ span >
126
135
) }
127
136
</ div >
128
137
) }
129
138
</ Field >
130
-
131
- { /* Password Field */ }
132
139
< Field name = "password" >
133
- { ( { input , meta } ) => (
140
+ { ( field ) => (
134
141
< div className = "form__field" >
135
142
< label htmlFor = "password" className = "form__label" >
136
143
{ t ( 'SignupForm.Password' ) }
@@ -142,7 +149,7 @@ function SignupForm() {
142
149
type = { showPassword ? 'text' : 'password' }
143
150
id = "password"
144
151
autoComplete = "new-password"
145
- { ...input }
152
+ { ...field . input }
146
153
/>
147
154
< button
148
155
className = "form__eye__icon"
@@ -157,30 +164,28 @@ function SignupForm() {
157
164
) }
158
165
</ button >
159
166
</ div >
160
- { meta . touched && meta . error && (
167
+ { field . meta . touched && field . meta . error && (
161
168
< span className = "form-error" aria-live = "polite" >
162
- { meta . error }
169
+ { field . meta . error }
163
170
</ span >
164
171
) }
165
172
</ div >
166
173
) }
167
174
</ Field >
168
-
169
- { /* Confirm Password Field */ }
170
175
< Field name = "confirmPassword" >
171
- { ( { input , meta } ) => (
176
+ { ( field ) => (
172
177
< div className = "form__field" >
173
178
< label htmlFor = "confirmPassword" className = "form__label" >
174
179
{ t ( 'SignupForm.ConfirmPassword' ) }
175
180
</ label >
176
181
< div className = "form__field__password" >
177
182
< input
178
183
className = "form__input"
179
- aria-label = { t ( 'SignupForm.ConfirmPasswordARIA' ) }
180
184
type = { showConfirmPassword ? 'text' : 'password' }
181
- id = "confirmPassword"
185
+ aria-label = { t ( 'SignupForm.ConfirmPasswordARIA' ) }
186
+ id = "confirmPassword" // Match the id with htmlFor
182
187
autoComplete = "new-password"
183
- { ...input }
188
+ { ...field . input }
184
189
/>
185
190
< button
186
191
className = "form__eye__icon"
@@ -195,16 +200,14 @@ function SignupForm() {
195
200
) }
196
201
</ button >
197
202
</ div >
198
- { meta . touched && meta . error && (
203
+ { field . meta . touched && field . meta . error && (
199
204
< span className = "form-error" aria-live = "polite" >
200
- { meta . error }
205
+ { field . meta . error }
201
206
</ span >
202
207
) }
203
208
</ div >
204
209
) }
205
210
</ Field >
206
-
207
- { /* Submit Button */ }
208
211
< Button type = "submit" disabled = { submitting || invalid || pristine } >
209
212
{ t ( 'SignupForm.SubmitSignup' ) }
210
213
</ Button >
0 commit comments