11import { useFormik } from "formik" ;
2- import { useNavigate } from "react-router-dom" ;
2+ import { useNavigate , Link } from "react-router-dom" ;
33import { reset_password , AppDispatch } from "../../services/actions/auth" ;
44import { connect , useDispatch } from "react-redux" ;
55import { RootState } from "../../services/actions/types" ;
66import { useEffect , useState } from "react" ;
7+ import axios from "axios" ;
8+ import { AUTH_ENDPOINTS } from "../../api/endpoints" ;
79import Layout from "../Layout/Layout" ;
810
911interface ResetPasswordProps {
@@ -14,6 +16,8 @@ function ResetPassword(props: ResetPasswordProps) {
1416 const { isAuthenticated } = props ;
1517 const dispatch = useDispatch < AppDispatch > ( ) ;
1618 const [ requestSent , setRequestSent ] = useState ( false ) ;
19+ const [ submittedEmail , setSubmittedEmail ] = useState ( "" ) ;
20+ const [ resendStatus , setResendStatus ] = useState < "idle" | "sent" | "error" > ( "idle" ) ;
1721
1822 const navigate = useNavigate ( ) ;
1923
@@ -29,58 +33,92 @@ function ResetPassword(props: ResetPasswordProps) {
2933 } ,
3034 onSubmit : ( values ) => {
3135 dispatch ( reset_password ( values . email ) ) ;
36+ setSubmittedEmail ( values . email ) ;
3237 setRequestSent ( true ) ;
3338 } ,
3439 } ) ;
3540
41+ const handleResend = async ( ) => {
42+ try {
43+ await axios . post ( AUTH_ENDPOINTS . RESET_PASSWORD , { email : submittedEmail } ) ;
44+ setResendStatus ( "sent" ) ;
45+ } catch {
46+ setResendStatus ( "error" ) ;
47+ }
48+ } ;
49+
3650 if ( requestSent ) {
37- navigate ( "/" ) ;
38- }
39- return (
40- < >
51+ return (
4152 < Layout >
42- < section className = "mx-auto mt-36 w-full max-w-xs" >
43- < h2 className = "blue_gradient mb-6 font-satoshi text-xl font-bold text-gray-600" >
44- Reset Password
45- </ h2 >
46- < form
47- onSubmit = { handleSubmit }
48- className = "mb-4 rounded bg-white px-8 pb-8 pt-6 shadow-md"
49- >
50- < div className = "mb-4" >
51- < label
52- htmlFor = "email"
53- className = "mb-2 block text-sm font-bold text-gray-700"
54- >
55- Email
56- </ label >
57- < input
58- id = "login-email"
59- name = "email"
60- type = "email"
61- onChange = { handleChange }
62- value = { values . email }
63- className = "focus:shadow-outline w-full appearance-none rounded border px-3 py-2 leading-tight text-gray-700 shadow focus:outline-none"
64- />
65- </ div >
66- < div className = "flex items-center justify-between" >
67- < button className = "black_btn" type = "submit" >
68- Reset Password
53+ < section className = "mx-auto mt-24 w-[20rem] md:mt-48 md:w-[32rem] text-center" >
54+ < div className = "mb-4 rounded-md bg-white px-3 pb-12 pt-6 shadow-md ring-1 md:px-12" >
55+ < h2 className = "blue_gradient mb-4 font-satoshi text-3xl font-bold text-gray-600" >
56+ Check your email
57+ </ h2 >
58+ < p className = "text-gray-600 mb-6" >
59+ If an account exists for < strong > { submittedEmail } </ strong > , you'll receive a password reset link shortly.
60+ </ p >
61+ < div className = "flex flex-col gap-3" >
62+ < Link to = "/login" className = "btnBlue w-full text-lg text-center block" >
63+ Back to log in
64+ </ Link >
65+ < button onClick = { handleResend } className = "text-sm text-blue-600 hover:underline" type = "button" >
66+ { resendStatus === "sent"
67+ ? "Email resent!"
68+ : resendStatus === "error"
69+ ? "Failed to resend. Try again."
70+ : "Resend email" }
6971 </ button >
7072 </ div >
71- </ form >
73+ </ div >
7274 </ section >
7375 </ Layout >
74- </ >
76+ ) ;
77+ }
78+
79+ return (
80+ < Layout >
81+ < section className = "mx-auto mt-24 w-[20rem] md:mt-48 md:w-[32rem]" >
82+ < form
83+ onSubmit = { handleSubmit }
84+ className = "mb-4 rounded-md bg-white px-3 pb-12 pt-6 shadow-md ring-1 md:px-12"
85+ >
86+ < h2 className = "blue_gradient mb-6 font-satoshi text-3xl font-bold text-gray-600 text-center" >
87+ Reset password
88+ </ h2 >
89+ < div className = "mb-4" >
90+ < label
91+ htmlFor = "email"
92+ className = "mb-2 block text-lg font-bold text-gray-700"
93+ >
94+ Email
95+ </ label >
96+ < input
97+ id = "login-email"
98+ name = "email"
99+ type = "email"
100+ onChange = { handleChange }
101+ value = { values . email }
102+ className = "focus:shadow-outline w-full appearance-none rounded border px-3 py-3 leading-tight text-gray-700 shadow focus:outline-none"
103+ />
104+ </ div >
105+ < button className = "btnBlue w-full text-lg" type = "submit" >
106+ Send reset link
107+ </ button >
108+ < div className = "mt-4 text-center" >
109+ < Link to = "/login" className = "text-sm text-blue-600 hover:underline" >
110+ Back to log in
111+ </ Link >
112+ </ div >
113+ </ form >
114+ </ section >
115+ </ Layout >
75116 ) ;
76117}
77118
78119const mapStateToProps = ( state : RootState ) => ( {
79120 isAuthenticated : state . auth . isAuthenticated ,
80121} ) ;
81122
82- // Assign the connected component to a named constant
83123const ConnectedResetPassword = connect ( mapStateToProps ) ( ResetPassword ) ;
84-
85- // Export the named constant
86124export default ConnectedResetPassword ;
0 commit comments