diff --git a/client/modules/User/components/SignupForm.jsx b/client/modules/User/components/SignupForm.jsx index fbb5c49b80..95181fe76d 100644 --- a/client/modules/User/components/SignupForm.jsx +++ b/client/modules/User/components/SignupForm.jsx @@ -9,49 +9,62 @@ import Button from '../../../common/Button'; import apiClient from '../../../utils/apiClient'; import useSyncFormTranslations from '../../../common/useSyncFormTranslations'; -function asyncValidate(fieldToValidate, value) { +const debounce = (func, delay) => { + let timer; + return (...args) => + new Promise((resolve) => { + clearTimeout(timer); + timer = setTimeout(() => resolve(func(...args)), delay); + }); +}; + +// API Validation Function +async function asyncValidate(fieldToValidate, value) { if (!value || value.trim().length === 0) { return ''; } - const queryParams = {}; - queryParams[fieldToValidate] = value; - queryParams.check_type = fieldToValidate; - return apiClient - .get('/signup/duplicate_check', { params: queryParams }) - .then((response) => { - if (response.data.exists) { - return response.data.message; - } - return ''; + const queryParams = { + [fieldToValidate]: value, + check_type: fieldToValidate + }; + + try { + const response = await apiClient.get('/signup/duplicate_check', { + params: queryParams }); + return response.data.exists ? response.data.message : ''; + } catch (error) { + return 'Error validating field.'; + } } +const debouncedAsyncValidate = debounce(asyncValidate, 300); + function validateUsername(username) { - return asyncValidate('username', username); + return debouncedAsyncValidate('username', username); } function validateEmail(email) { - return asyncValidate('email', email); + return debouncedAsyncValidate('email', email); } function SignupForm() { const { t, i18n } = useTranslation(); - const dispatch = useDispatch(); - function onSubmit(formProps) { - return dispatch(validateAndSignUpUser(formProps)); - } + const formRef = useRef(null); const [showPassword, setShowPassword] = useState(false); const [showConfirmPassword, setShowConfirmPassword] = useState(false); - const formRef = useRef(null); - const handleVisibility = () => { - setShowPassword(!showPassword); - }; - const handleConfirmVisibility = () => { - setShowConfirmPassword(!showConfirmPassword); - }; + useSyncFormTranslations(formRef, i18n.language); + const handleVisibility = () => setShowPassword(!showPassword); + const handleConfirmVisibility = () => + setShowConfirmPassword(!showConfirmPassword); + + function onSubmit(formProps) { + return dispatch(validateAndSignUpUser(formProps)); + } + return (
+ {/* Username Field */} - {(field) => ( + {({ input, meta }) => (
)}
+ + {/* Email Field */} - {(field) => ( + {({ input, meta }) => (
)}
+ + {/* Password Field */} - {(field) => ( + {({ input, meta }) => (
- {field.meta.touched && field.meta.error && ( + {meta.touched && meta.error && ( - {field.meta.error} + {meta.error} )} )}
+ + {/* Confirm Password Field */} - {(field) => ( + {({ input, meta }) => (
)}
+ + {/* Submit Button */}