@@ -19,7 +19,7 @@ import { MyButton, ReCaptchaInput } from "components";
1919import { ChangeEvent , useEffect , useRef , useState } from "react" ;
2020import ReCAPTCHA from "react-google-recaptcha" ;
2121import { Helmet } from "react-helmet" ;
22- import { SubmitHandler , useForm } from "react-hook-form" ;
22+ import { SubmitErrorHandler , SubmitHandler , useForm } from "react-hook-form" ;
2323import { useDispatch , useSelector } from "react-redux" ;
2424import { useHistory } from "react-router-dom" ;
2525import { RootState } from "reduxes" ;
@@ -60,6 +60,7 @@ const ForgotPasswordPage = function () {
6060 handleSubmit,
6161 formState : { errors } ,
6262 setValue,
63+ getValues,
6364 trigger,
6465 } = useForm < ForgotPasswordRequestFields > ( { mode : "onChange" } ) ;
6566 const recaptchaRef = useRef < ReCAPTCHA > ( null ) ;
@@ -88,6 +89,22 @@ const ForgotPasswordPage = function () {
8889 }
8990 } ;
9091
92+ const generateReCatpchaToken = async ( ) => {
93+ if ( recaptchaRef && recaptchaRef . current ) {
94+ const token = await recaptchaRef . current . executeAsync ( ) ;
95+ setValue ( "captcha" , token || "" ) ;
96+ onSubmit ( getValues ( ) ) ;
97+ }
98+ } ;
99+
100+ const onInvalidSubmit : SubmitErrorHandler < ForgotPasswordRequestFields > = (
101+ error
102+ ) => {
103+ if ( error . captcha && error . captcha . type === "required" ) {
104+ generateReCatpchaToken ( ) ;
105+ }
106+ } ;
107+
91108 const password = useRef ( { } ) ;
92109 password . current = watch ( "password" , "" ) ;
93110
@@ -170,7 +187,7 @@ const ForgotPasswordPage = function () {
170187 sent to your email to verify.
171188 </ Typography >
172189 < Box sx = { { mt : 3 , py : 0 , px : "14px" } } >
173- < form onSubmit = { handleSubmit ( onSubmit ) } >
190+ < form onSubmit = { handleSubmit ( onSubmit , onInvalidSubmit ) } >
174191 < TextField
175192 variant = "outlined"
176193 required
0 commit comments