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 LeftArrow from "../../assets/Images/arrow-left.png";
|
2024-07-16 12:17:18 -04:00
|
|
|
|
import { useState } from "react";
|
|
|
|
|
|
import { credentials } from "../../Validation/validation";
|
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-07-16 12:17:18 -04:00
|
|
|
|
import { createToast } from "../../Utils/toastUtils";
|
|
|
|
|
|
import { useDispatch, useSelector } from "react-redux";
|
|
|
|
|
|
import { forgotPassword } from "../../Features/Auth/authSlice";
|
|
|
|
|
|
import ButtonSpinner from "../../Components/ButtonSpinner";
|
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-07-16 12:17:18 -04:00
|
|
|
|
const dispatch = useDispatch();
|
2024-06-12 09:48:46 -07:00
|
|
|
|
|
2024-07-16 12:17:18 -04:00
|
|
|
|
const { isLoading } = useSelector((state) => state.auth);
|
2024-06-11 12:58:21 -07:00
|
|
|
|
const [errors, setErrors] = useState({});
|
|
|
|
|
|
const [form, setForm] = useState({
|
|
|
|
|
|
email: "",
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
const idMap = {
|
|
|
|
|
|
"forgot-password-email-input": "email",
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2024-07-16 12:17:18 -04:00
|
|
|
|
const handleSubmit = async (event) => {
|
|
|
|
|
|
event.preventDefault();
|
2024-06-11 12:58:21 -07:00
|
|
|
|
|
2024-07-16 12:17:18 -04:00
|
|
|
|
const { error } = credentials.validate(form, { abortEarly: false });
|
2024-06-11 12:58:21 -07:00
|
|
|
|
|
2024-07-16 12:17:18 -04:00
|
|
|
|
if (error) {
|
|
|
|
|
|
// validation errors
|
|
|
|
|
|
const err =
|
|
|
|
|
|
error.details && error.details.length > 0
|
|
|
|
|
|
? error.details[0].message
|
|
|
|
|
|
: "Error validating data.";
|
|
|
|
|
|
setErrors(err);
|
|
|
|
|
|
createToast({
|
|
|
|
|
|
variant: "info",
|
|
|
|
|
|
body: err,
|
|
|
|
|
|
hasIcon: false,
|
|
|
|
|
|
});
|
|
|
|
|
|
} else {
|
|
|
|
|
|
const action = await dispatch(forgotPassword(form));
|
|
|
|
|
|
if (action.payload.success) {
|
|
|
|
|
|
navigate("/check-email");
|
|
|
|
|
|
createToast({
|
|
|
|
|
|
variant: "info",
|
|
|
|
|
|
body: `Instructions sent to ${form.email}.`,
|
|
|
|
|
|
hasIcon: false,
|
|
|
|
|
|
});
|
|
|
|
|
|
} else {
|
|
|
|
|
|
if (action.payload) {
|
|
|
|
|
|
// dispatch errors
|
|
|
|
|
|
createToast({
|
|
|
|
|
|
variant: "info",
|
|
|
|
|
|
body: action.payload.msg,
|
|
|
|
|
|
hasIcon: false,
|
|
|
|
|
|
});
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// unknown errors
|
|
|
|
|
|
createToast({
|
|
|
|
|
|
variant: "info",
|
|
|
|
|
|
body: "Unknown error.",
|
|
|
|
|
|
hasIcon: false,
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
2024-06-11 12:58:21 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
2024-07-16 12:17:18 -04:00
|
|
|
|
|
|
|
|
|
|
const handleChange = (event) => {
|
|
|
|
|
|
const { value, id } = event.target;
|
|
|
|
|
|
const name = idMap[id];
|
|
|
|
|
|
setForm((prev) => ({
|
|
|
|
|
|
...prev,
|
|
|
|
|
|
[name]: value,
|
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
|
|
const { error } = credentials.validate(
|
|
|
|
|
|
{ [name]: value },
|
|
|
|
|
|
{ abortEarly: false }
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
setErrors((prev) => {
|
|
|
|
|
|
const prevErrors = { ...prev };
|
|
|
|
|
|
if (error) prevErrors[name] = error.details[0].message;
|
|
|
|
|
|
else delete prevErrors[name];
|
|
|
|
|
|
return prevErrors;
|
|
|
|
|
|
});
|
|
|
|
|
|
};
|
|
|
|
|
|
|
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"
|
2024-07-16 12:17:18 -04:00
|
|
|
|
value={form.email}
|
|
|
|
|
|
onChange={handleChange}
|
2024-07-13 14:54:06 -04:00
|
|
|
|
error={errors.email}
|
2024-05-28 21:28:15 +03:30
|
|
|
|
/>
|
|
|
|
|
|
<div className="forgot-password-v-gap-medium"></div>
|
2024-07-16 12:17:18 -04:00
|
|
|
|
<ButtonSpinner
|
|
|
|
|
|
disabled={errors.email !== undefined}
|
2024-06-11 12:58:21 -07:00
|
|
|
|
onClick={handleSubmit}
|
2024-07-16 12:17:18 -04:00
|
|
|
|
isLoading={isLoading}
|
2024-05-28 21:28:15 +03:30
|
|
|
|
level="primary"
|
|
|
|
|
|
label="Reset password"
|
|
|
|
|
|
sx={{
|
|
|
|
|
|
width: "100%",
|
|
|
|
|
|
fontSize: "13px",
|
|
|
|
|
|
}}
|
|
|
|
|
|
/>
|
|
|
|
|
|
</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;
|