@@ -14,13 +14,15 @@ import { toast } from "react-toastify";
14
14
import {
15
15
PASSWORD_REQUIRED_ERROR_MESSAGE ,
16
16
PASSWORD_MISMATCH_ERROR_MESSAGE ,
17
+ TOKEN_REQUIRED_ERROR_MESSAGE ,
17
18
} from "../../utils/constants" ;
18
19
19
20
const ForgetPassword : React . FC = ( ) => {
20
21
const navigate = useNavigate ( ) ;
21
22
22
23
const [ email , setEmail ] = useState < string > ( "" ) ;
23
24
const [ hasSentEmail , setHasSentEmail ] = useState < boolean > ( false ) ;
25
+ const [ isLoading , setisLoading ] = useState < boolean > ( false ) ;
24
26
25
27
const {
26
28
register : registerEmail ,
@@ -38,6 +40,7 @@ const ForgetPassword: React.FC = () => {
38
40
} ) ;
39
41
40
42
const handleSendEmail = async ( email : string ) => {
43
+ setisLoading ( true ) ;
41
44
userClient
42
45
. post ( "/users/send-reset-password-email" , { email } )
43
46
. then ( ( res ) => {
@@ -47,10 +50,14 @@ const ForgetPassword: React.FC = () => {
47
50
} )
48
51
. catch ( ( err ) => {
49
52
toast . error ( err . response ?. data . message || err . message ) ;
53
+ } )
54
+ . finally ( ( ) => {
55
+ setisLoading ( false ) ;
50
56
} ) ;
51
57
} ;
52
58
53
59
const handleResetPassword = async ( password : string , token : string ) => {
60
+ setisLoading ( true ) ;
54
61
userClient
55
62
. post ( `/users/reset-password` , { email, token, password } )
56
63
. then ( ( res ) => {
@@ -59,6 +66,9 @@ const ForgetPassword: React.FC = () => {
59
66
} )
60
67
. catch ( ( err ) => {
61
68
toast . error ( err . response ?. data . message || err . message ) ;
69
+ } )
70
+ . finally ( ( ) => {
71
+ setisLoading ( false ) ;
62
72
} ) ;
63
73
} ;
64
74
@@ -99,6 +109,7 @@ const ForgetPassword: React.FC = () => {
99
109
sx = { ( theme ) => ( { marginTop : theme . spacing ( 1 ) } ) }
100
110
{ ...registerPassword ( "token" , {
101
111
setValueAs : ( value : string ) => value . trim ( ) ,
112
+ required : TOKEN_REQUIRED_ERROR_MESSAGE ,
102
113
} ) }
103
114
error = { ! ! errorsPassword . token }
104
115
helperText = { errorsPassword . token ?. message }
@@ -148,10 +159,16 @@ const ForgetPassword: React.FC = () => {
148
159
onClick = { ( ) => {
149
160
handleSendEmail ( email ) ;
150
161
} }
162
+ disabled = { isLoading }
151
163
>
152
164
Resend
153
165
</ Button >
154
- < Button fullWidth variant = "contained" type = "submit" >
166
+ < Button
167
+ fullWidth
168
+ variant = "contained"
169
+ type = "submit"
170
+ disabled = { isLoading }
171
+ >
155
172
Change Password
156
173
</ Button >
157
174
</ Stack >
@@ -220,6 +237,7 @@ const ForgetPassword: React.FC = () => {
220
237
type = "submit"
221
238
variant = "contained"
222
239
sx = { ( theme ) => ( { margin : theme . spacing ( 2 , 0 ) } ) }
240
+ disabled = { isLoading }
223
241
>
224
242
Send Reset Link
225
243
</ Button >
0 commit comments