2024-05-29 22:11:16 +03:30
|
|
|
import BackgroundPattern from "../../Components/BackgroundPattern/BackgroundPattern";
|
|
|
|
|
import "./index.css";
|
|
|
|
|
import LockIcon from "../../assets/Images/lock-icon.png";
|
|
|
|
|
import Check from "../../Components/Check/Check";
|
2024-07-16 16:17:13 -04:00
|
|
|
import ButtonSpinner from "../../Components/ButtonSpinner";
|
2024-05-29 22:11:16 +03:30
|
|
|
import LeftArrow from "../../assets/Images/arrow-left.png";
|
2024-06-11 11:19:50 -07:00
|
|
|
import { useParams } from "react-router-dom";
|
2024-07-16 16:17:13 -04:00
|
|
|
import { useState } from "react";
|
|
|
|
|
import { credentials } from "../../Validation/validation";
|
2024-06-11 14:18:20 -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 16:17:13 -04:00
|
|
|
import { useDispatch, useSelector } from "react-redux";
|
|
|
|
|
import { setNewPassword } from "../../Features/Auth/authSlice";
|
|
|
|
|
import { createToast } from "../../Utils/toastUtils";
|
2024-05-29 22:11:16 +03:30
|
|
|
|
|
|
|
|
const SetNewPassword = () => {
|
2024-06-11 14:18:20 -07:00
|
|
|
const navigate = useNavigate();
|
2024-07-16 16:17:13 -04:00
|
|
|
const dispatch = useDispatch();
|
|
|
|
|
|
2024-06-11 14:18:20 -07:00
|
|
|
const [errors, setErrors] = useState({});
|
2024-07-16 16:17:13 -04:00
|
|
|
const [form, setForm] = useState({
|
|
|
|
|
password: "",
|
|
|
|
|
confirm: "",
|
|
|
|
|
});
|
2024-06-11 14:18:20 -07:00
|
|
|
|
|
|
|
|
const idMap = {
|
|
|
|
|
"register-password-input": "password",
|
|
|
|
|
"confirm-password-input": "confirm",
|
|
|
|
|
};
|
|
|
|
|
|
2024-07-16 16:17:13 -04:00
|
|
|
const { isLoading } = useSelector((state) => state.auth);
|
|
|
|
|
const { token } = useParams();
|
2024-06-11 14:18:20 -07:00
|
|
|
|
2024-07-16 16:17:13 -04:00
|
|
|
const handleSubmit = async (e) => {
|
|
|
|
|
e.preventDefault();
|
2024-06-11 14:18:20 -07:00
|
|
|
|
2024-07-16 16:17:13 -04:00
|
|
|
const passwordForm = { ...form };
|
|
|
|
|
const { error } = credentials.validate(passwordForm, {
|
2024-06-11 14:18:20 -07:00
|
|
|
abortEarly: false,
|
2024-07-16 16:17:13 -04:00
|
|
|
context: { password: form.password },
|
2024-06-11 14:18:20 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (error) {
|
2024-07-16 16:17:13 -04:00
|
|
|
// validation errors
|
|
|
|
|
const newErrors = {};
|
|
|
|
|
error.details.forEach((err) => {
|
|
|
|
|
newErrors[err.path[0]] = err.message;
|
|
|
|
|
});
|
|
|
|
|
setErrors(newErrors);
|
|
|
|
|
createToast({
|
|
|
|
|
variant: "info",
|
|
|
|
|
body:
|
|
|
|
|
error.details && error.details.length > 0
|
|
|
|
|
? error.details[0].message
|
|
|
|
|
: "Error validating data.",
|
|
|
|
|
hasIcon: false,
|
|
|
|
|
});
|
2024-06-11 14:18:20 -07:00
|
|
|
} else {
|
2024-07-16 16:17:13 -04:00
|
|
|
delete passwordForm.confirm;
|
|
|
|
|
const action = await dispatch(
|
|
|
|
|
setNewPassword({ token: token, form: passwordForm })
|
|
|
|
|
);
|
|
|
|
|
if (action.payload.success) {
|
|
|
|
|
navigate("/new-password-confirmed");
|
|
|
|
|
createToast({
|
|
|
|
|
variant: "info",
|
|
|
|
|
body: "Your password was reset successfully.",
|
|
|
|
|
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 14:18:20 -07:00
|
|
|
}
|
2024-07-16 16:17:13 -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, context: { password: form.password } }
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
setErrors((prev) => {
|
|
|
|
|
const prevErrors = { ...prev };
|
|
|
|
|
if (error) prevErrors[name] = error.details[0].message;
|
|
|
|
|
else delete prevErrors[name];
|
|
|
|
|
return prevErrors;
|
|
|
|
|
});
|
|
|
|
|
};
|
2024-06-11 14:18:20 -07:00
|
|
|
|
2024-05-29 22:11:16 +03:30
|
|
|
return (
|
|
|
|
|
<div className="set-new-password-page">
|
|
|
|
|
<BackgroundPattern />
|
2024-07-16 16:17:13 -04:00
|
|
|
<form className="set-new-password-form" onSubmit={handleSubmit}>
|
2024-05-29 22:11:16 +03:30
|
|
|
<div className="set-new-password-form-header">
|
|
|
|
|
<img
|
|
|
|
|
className="set-new-password-form-header-logo"
|
|
|
|
|
src={LockIcon}
|
|
|
|
|
alt="LockIcon"
|
|
|
|
|
/>
|
|
|
|
|
<div className="set-new-password-form-gap-medium" />
|
|
|
|
|
<div className="set-new-password-form-heading">Set new password</div>
|
|
|
|
|
<div className="set-new-password-form-gap-small" />
|
|
|
|
|
<div className="set-new-password-form-subheading">
|
|
|
|
|
Your new password must be different to previously used passwords.
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
<div className="set-new-password-form-gap-large"></div>
|
|
|
|
|
<div className="set-new-password-form-content">
|
2024-07-13 14:54:06 -04:00
|
|
|
<Field
|
|
|
|
|
type="password"
|
|
|
|
|
id="register-password-input"
|
2024-05-29 22:11:16 +03:30
|
|
|
label="Password"
|
2024-07-13 14:54:06 -04:00
|
|
|
isRequired={true}
|
2024-05-29 22:11:16 +03:30
|
|
|
placeholder="••••••••"
|
2024-07-16 16:17:13 -04:00
|
|
|
value={form.password}
|
|
|
|
|
onChange={handleChange}
|
2024-07-13 14:54:06 -04:00
|
|
|
error={errors.password}
|
2024-05-29 22:11:16 +03:30
|
|
|
/>
|
2024-06-02 12:08:35 +03:30
|
|
|
<div className="set-new-password-form-gap-medium"></div>
|
2024-07-13 14:54:06 -04:00
|
|
|
<Field
|
|
|
|
|
type="password"
|
|
|
|
|
id="confirm-password-input"
|
2024-05-29 22:11:16 +03:30
|
|
|
label="Confirm password"
|
2024-07-13 14:54:06 -04:00
|
|
|
isRequired={true}
|
2024-05-29 22:11:16 +03:30
|
|
|
placeholder="••••••••"
|
2024-07-16 16:17:13 -04:00
|
|
|
value={form.confirm}
|
|
|
|
|
onChange={handleChange}
|
2024-07-13 14:54:06 -04:00
|
|
|
error={errors.confirm}
|
2024-05-29 22:11:16 +03:30
|
|
|
/>
|
2024-06-02 12:08:35 +03:30
|
|
|
<div className="set-new-password-form-gap-medium"></div>
|
2024-05-29 22:11:16 +03:30
|
|
|
<div className="set-new-password-form-checks">
|
|
|
|
|
<Check text="Must be at least 8 characters" />
|
|
|
|
|
<div className="set-new-password-form-gap-small"></div>
|
|
|
|
|
<Check text="Must contain one special character" />
|
|
|
|
|
</div>
|
2024-06-02 12:08:35 +03:30
|
|
|
<div className="set-new-password-form-gap-medium"></div>
|
2024-07-16 16:17:13 -04:00
|
|
|
<ButtonSpinner
|
|
|
|
|
disabled={Object.keys(errors).length !== 0}
|
|
|
|
|
isLoading={isLoading}
|
2024-06-11 14:18:20 -07:00
|
|
|
onClick={handleSubmit}
|
2024-05-29 22:11:16 +03:30
|
|
|
level="primary"
|
2024-06-11 14:18:20 -07:00
|
|
|
label="Reset password"
|
2024-05-29 22:11:16 +03:30
|
|
|
sx={{
|
|
|
|
|
width: "100%",
|
|
|
|
|
fontSize: "13px",
|
|
|
|
|
fontWeight: "200",
|
|
|
|
|
height: "35px",
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
<div className="set-new-password-form-gap-medium"></div>
|
|
|
|
|
<div className="set-new-password-back-button">
|
|
|
|
|
<img
|
|
|
|
|
className="set-new-password-back-button-img"
|
|
|
|
|
src={LeftArrow}
|
|
|
|
|
alt="LeftArrow"
|
|
|
|
|
/>
|
|
|
|
|
<div className="set-new-password-back-button-text">
|
|
|
|
|
Back to log in
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
2024-07-16 16:17:13 -04:00
|
|
|
</form>
|
2024-05-29 22:11:16 +03:30
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default SetNewPassword;
|