2024-05-28 21:28:15 +03:30
|
|
|
|
import BackgroundPattern from "../../Components/BackgroundPattern/BackgroundPattern";
|
|
|
|
|
|
import "./index.css";
|
|
|
|
|
|
import React from "react";
|
|
|
|
|
|
import Logomark from "../../assets/Images/key-password.png";
|
|
|
|
|
|
import Button from "../../Components/Button";
|
|
|
|
|
|
import LeftArrow from "../../assets/Images/arrow-left.png";
|
2024-06-11 12:58:21 -07:00
|
|
|
|
import { useState, useEffect } from "react";
|
|
|
|
|
|
import { recoveryValidation } from "../../Validation/validation";
|
2024-07-09 11:45:14 -07:00
|
|
|
|
import axiosInstance from "../../Utils/axiosConfig";
|
2024-06-12 09:48:46 -07:00
|
|
|
|
import { useNavigate } from "react-router-dom";
|
2024-07-13 14:54:06 -04:00
|
|
|
|
import Field from "../../Components/Inputs/Field";
|
2024-06-11 12:58:21 -07:00
|
|
|
|
|
2024-05-28 21:28:15 +03:30
|
|
|
|
const ForgotPassword = () => {
|
2024-06-12 09:48:46 -07:00
|
|
|
|
const navigate = useNavigate();
|
|
|
|
|
|
|
2024-06-11 14:18:20 -07:00
|
|
|
|
const [isLoading, setIsLoading] = useState(false); // Used to disable the button while loading so user doesn't submit multiple times
|
2024-06-11 12:58:21 -07:00
|
|
|
|
const [errors, setErrors] = useState({});
|
|
|
|
|
|
const [form, setForm] = useState({
|
|
|
|
|
|
email: "",
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
const idMap = {
|
|
|
|
|
|
"forgot-password-email-input": "email",
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
const { error } = recoveryValidation.validate(form);
|
|
|
|
|
|
if (error === undefined) {
|
|
|
|
|
|
setErrors({});
|
|
|
|
|
|
} else {
|
|
|
|
|
|
const validationErrors = error.details.reduce((acc, err) => {
|
|
|
|
|
|
return { ...acc, [err.path[0]]: err.message };
|
|
|
|
|
|
}, {});
|
|
|
|
|
|
setErrors(validationErrors);
|
|
|
|
|
|
}
|
|
|
|
|
|
}, [form]);
|
|
|
|
|
|
|
|
|
|
|
|
const handleInput = (e) => {
|
|
|
|
|
|
const fieldName = idMap[e.target.id];
|
|
|
|
|
|
setForm({ ...form, [fieldName]: e.target.value });
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const handleSubmit = async () => {
|
2024-06-11 14:18:20 -07:00
|
|
|
|
//TODO show loading spinner
|
|
|
|
|
|
setIsLoading(true);
|
|
|
|
|
|
try {
|
|
|
|
|
|
const { error } = recoveryValidation.validate(form);
|
|
|
|
|
|
if (error !== undefined) {
|
|
|
|
|
|
throw error;
|
2024-06-11 12:58:21 -07:00
|
|
|
|
}
|
2024-07-09 11:45:14 -07:00
|
|
|
|
await axiosInstance.post(`/auth/recovery/request`, form);
|
2024-06-12 09:48:46 -07:00
|
|
|
|
navigate("/check-email");
|
2024-06-11 14:18:20 -07:00
|
|
|
|
} catch (error) {
|
|
|
|
|
|
//TODO display error (Toast?)
|
2024-06-11 12:58:21 -07:00
|
|
|
|
alert(error);
|
2024-06-11 14:18:20 -07:00
|
|
|
|
} finally {
|
|
|
|
|
|
setIsLoading(false);
|
2024-06-11 12:58:21 -07:00
|
|
|
|
}
|
|
|
|
|
|
};
|
2024-05-28 21:28:15 +03:30
|
|
|
|
return (
|
|
|
|
|
|
<div className="forgot-password-page">
|
|
|
|
|
|
<BackgroundPattern></BackgroundPattern>
|
|
|
|
|
|
<div className="forgot-password-form">
|
|
|
|
|
|
<div className="forgot-password-form-header">
|
|
|
|
|
|
<img
|
|
|
|
|
|
className="forgot-password-form-header-logo"
|
|
|
|
|
|
src={Logomark}
|
|
|
|
|
|
alt="Logomark"
|
|
|
|
|
|
/>
|
|
|
|
|
|
<div className="forgot-password-v-gap-medium"></div>
|
|
|
|
|
|
<div className="forgot-password-form-heading">Forgot password?</div>
|
|
|
|
|
|
<div className="forgot-password-v-gap-small"></div>
|
|
|
|
|
|
<div className="forgot-password-form-subheading">
|
|
|
|
|
|
No worries, we’ll send you reset instructions.
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<div className="forgot-password-v-gap-large"></div>
|
|
|
|
|
|
<div className="forgot-password-body">
|
2024-07-13 14:54:06 -04:00
|
|
|
|
<Field
|
|
|
|
|
|
type="email"
|
2024-05-28 21:28:15 +03:30
|
|
|
|
id="forgot-password-email-input"
|
2024-07-13 14:54:06 -04:00
|
|
|
|
label="Email"
|
|
|
|
|
|
isRequired={true}
|
|
|
|
|
|
placeholder="Enter your email"
|
|
|
|
|
|
onChange={handleInput}
|
|
|
|
|
|
error={errors.email}
|
2024-05-28 21:28:15 +03:30
|
|
|
|
/>
|
|
|
|
|
|
<div className="forgot-password-v-gap-medium"></div>
|
|
|
|
|
|
<Button
|
2024-06-11 14:18:20 -07:00
|
|
|
|
disabled={errors.email !== undefined || isLoading === true}
|
2024-06-11 12:58:21 -07:00
|
|
|
|
onClick={handleSubmit}
|
2024-05-28 21:28:15 +03:30
|
|
|
|
level="primary"
|
|
|
|
|
|
label="Reset password"
|
|
|
|
|
|
sx={{
|
|
|
|
|
|
width: "100%",
|
|
|
|
|
|
fontSize: "13px",
|
|
|
|
|
|
fontWeight: "200",
|
|
|
|
|
|
height: "44px",
|
|
|
|
|
|
}}
|
|
|
|
|
|
/>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<div className="forgot-password-v-gap-large"></div>
|
|
|
|
|
|
<div className="forgot-password-back-button">
|
|
|
|
|
|
<img
|
|
|
|
|
|
className="forgot-password-back-button-img"
|
|
|
|
|
|
src={LeftArrow}
|
|
|
|
|
|
alt="LeftArrow"
|
|
|
|
|
|
/>
|
|
|
|
|
|
<div className="forgot-password-back-button-text">Back to log in</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
export default ForgotPassword;
|