@@ -9,43 +9,51 @@ import Button from '../../../common/Button';
99import apiClient from '../../../utils/apiClient' ;
1010import useSyncFormTranslations from '../../../common/useSyncFormTranslations' ;
1111
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 } ;
2013
21- // API Validation Function
22- async function asyncValidate ( fieldToValidate , value ) {
14+ function asyncValidate ( fieldToValidate , value ) {
2315 if ( ! value || value . trim ( ) . length === 0 ) {
24- return '' ;
16+ return Promise . resolve ( '' ) ;
2517 }
18+
2619 const queryParams = {
2720 [ fieldToValidate ] : value ,
2821 check_type : fieldToValidate
2922 } ;
3023
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+ } ) ;
3949}
4050
41- const debouncedAsyncValidate = debounce ( asyncValidate , 300 ) ;
42-
4351function validateUsername ( username ) {
44- return debouncedAsyncValidate ( 'username' , username ) ;
52+ return asyncValidate ( 'username' , username ) ;
4553}
4654
4755function validateEmail ( email ) {
48- return debouncedAsyncValidate ( 'email' , email ) ;
56+ return asyncValidate ( 'email' , email ) ;
4957}
5058
5159function SignupForm ( ) {
@@ -62,26 +70,29 @@ function SignupForm() {
6270 setShowConfirmPassword ( ! showConfirmPassword ) ;
6371
6472 function onSubmit ( formProps ) {
73+ console . log ( "it's happening" ) ;
6574 return dispatch ( validateAndSignUpUser ( formProps ) ) ;
6675 }
6776
6877 return (
6978 < Form
7079 fields = { [ 'username' , 'email' , 'password' , 'confirmPassword' ] }
7180 validate = { validateSignup }
72- onSubmit = { onSubmit }
81+ onSubmit = { ( values ) => {
82+ console . log ( 'Form onSubmit triggered' , values ) ;
83+ return onSubmit ( values ) ;
84+ } }
7385 >
7486 { ( { handleSubmit, pristine, submitting, invalid, form } ) => {
7587 formRef . current = form ;
7688 return (
7789 < form className = "form" onSubmit = { handleSubmit } >
78- { /* Username Field */ }
7990 < Field
8091 name = "username"
8192 validate = { validateUsername }
8293 validateFields = { [ ] }
8394 >
84- { ( { input , meta } ) => (
95+ { ( field ) => (
8596 < div className = "form__field" >
8697 < label htmlFor = "username" className = "form__label" >
8798 { t ( 'SignupForm.Title' ) }
@@ -93,20 +104,18 @@ function SignupForm() {
93104 id = "username"
94105 autoComplete = "username"
95106 autoCapitalize = "none"
96- { ...input }
107+ { ...field . input }
97108 />
98- { meta . touched && meta . error && (
109+ { field . meta . touched && field . meta . error && (
99110 < span className = "form-error" aria-live = "polite" >
100- { meta . error }
111+ { field . meta . error }
101112 </ span >
102113 ) }
103114 </ div >
104115 ) }
105116 </ Field >
106-
107- { /* Email Field */ }
108117 < Field name = "email" validate = { validateEmail } validateFields = { [ ] } >
109- { ( { input , meta } ) => (
118+ { ( field ) => (
110119 < div className = "form__field" >
111120 < label htmlFor = "email" className = "form__label" >
112121 { t ( 'SignupForm.Email' ) }
@@ -117,20 +126,18 @@ function SignupForm() {
117126 type = "email"
118127 id = "email"
119128 autoComplete = "email"
120- { ...input }
129+ { ...field . input }
121130 />
122- { meta . touched && meta . error && (
131+ { field . meta . touched && field . meta . error && (
123132 < span className = "form-error" aria-live = "polite" >
124- { meta . error }
133+ { field . meta . error }
125134 </ span >
126135 ) }
127136 </ div >
128137 ) }
129138 </ Field >
130-
131- { /* Password Field */ }
132139 < Field name = "password" >
133- { ( { input , meta } ) => (
140+ { ( field ) => (
134141 < div className = "form__field" >
135142 < label htmlFor = "password" className = "form__label" >
136143 { t ( 'SignupForm.Password' ) }
@@ -142,7 +149,7 @@ function SignupForm() {
142149 type = { showPassword ? 'text' : 'password' }
143150 id = "password"
144151 autoComplete = "new-password"
145- { ...input }
152+ { ...field . input }
146153 />
147154 < button
148155 className = "form__eye__icon"
@@ -157,30 +164,28 @@ function SignupForm() {
157164 ) }
158165 </ button >
159166 </ div >
160- { meta . touched && meta . error && (
167+ { field . meta . touched && field . meta . error && (
161168 < span className = "form-error" aria-live = "polite" >
162- { meta . error }
169+ { field . meta . error }
163170 </ span >
164171 ) }
165172 </ div >
166173 ) }
167174 </ Field >
168-
169- { /* Confirm Password Field */ }
170175 < Field name = "confirmPassword" >
171- { ( { input , meta } ) => (
176+ { ( field ) => (
172177 < div className = "form__field" >
173178 < label htmlFor = "confirmPassword" className = "form__label" >
174179 { t ( 'SignupForm.ConfirmPassword' ) }
175180 </ label >
176181 < div className = "form__field__password" >
177182 < input
178183 className = "form__input"
179- aria-label = { t ( 'SignupForm.ConfirmPasswordARIA' ) }
180184 type = { showConfirmPassword ? 'text' : 'password' }
181- id = "confirmPassword"
185+ aria-label = { t ( 'SignupForm.ConfirmPasswordARIA' ) }
186+ id = "confirmPassword" // Match the id with htmlFor
182187 autoComplete = "new-password"
183- { ...input }
188+ { ...field . input }
184189 />
185190 < button
186191 className = "form__eye__icon"
@@ -195,16 +200,14 @@ function SignupForm() {
195200 ) }
196201 </ button >
197202 </ div >
198- { meta . touched && meta . error && (
203+ { field . meta . touched && field . meta . error && (
199204 < span className = "form-error" aria-live = "polite" >
200- { meta . error }
205+ { field . meta . error }
201206 </ span >
202207 ) }
203208 </ div >
204209 ) }
205210 </ Field >
206-
207- { /* Submit Button */ }
208211 < Button type = "submit" disabled = { submitting || invalid || pristine } >
209212 { t ( 'SignupForm.SubmitSignup' ) }
210213 </ Button >
0 commit comments