Files
Checkmate/Client/src/Pages/Auth/ForgotPassword.jsx
T

174 lines
5.1 KiB
React
Raw Normal View History

2024-08-12 13:58:18 -04:00
import { Box, Stack, Typography } from "@mui/material";
2024-08-12 13:16:02 -04:00
import { useTheme } from "@emotion/react";
import { createToast } from "../../Utils/toastUtils";
import { useDispatch, useSelector } from "react-redux";
import { forgotPassword } from "../../Features/Auth/authSlice";
2024-08-12 14:06:01 -04:00
import { useEffect, useState } from "react";
import { credentials } from "../../Validation/validation";
import { useNavigate } from "react-router-dom";
import Field from "../../Components/Inputs/Field";
import ButtonSpinner from "../../Components/ButtonSpinner";
2024-08-12 13:16:02 -04:00
import Logo from "../../assets/icons/bwu-icon.svg?react";
import Key from "../../assets/icons/key.svg?react";
import background from "../../assets/Images/background_pattern_decorative.png";
import "./index.css";
2024-05-28 21:28:15 +03:30
const ForgotPassword = () => {
const navigate = useNavigate();
const dispatch = useDispatch();
2024-07-16 12:55:08 -04:00
const theme = useTheme();
const { isLoading } = useSelector((state) => state.auth);
const [errors, setErrors] = useState({});
const [form, setForm] = useState({
email: "",
});
2024-08-12 14:06:01 -04:00
useEffect(() => {
const email = sessionStorage.getItem("email");
email && setForm({ email: sessionStorage.getItem("email") });
}, []);
const handleSubmit = async (event) => {
event.preventDefault();
const { error } = credentials.validate(form, { abortEarly: false });
if (error) {
// validation errors
const err =
error.details && error.details.length > 0
? error.details[0].message
: "Error validating data.";
2024-08-12 13:58:18 -04:00
setErrors({ email: err });
createToast({
body: err,
});
} else {
const action = await dispatch(forgotPassword(form));
if (action.payload.success) {
2024-07-16 14:29:59 -04:00
sessionStorage.setItem("email", form.email);
navigate("/check-email");
createToast({
body: `Instructions sent to ${form.email}.`,
});
} else {
if (action.payload) {
// dispatch errors
createToast({
body: action.payload.msg,
});
} else {
// unknown errors
createToast({
body: "Unknown error.",
});
}
}
}
};
const handleChange = (event) => {
2024-08-12 13:58:18 -04:00
const { value } = event.target;
setForm({ email: value });
const { error } = credentials.validate(
2024-08-12 13:58:18 -04:00
{ email: value },
{ abortEarly: false }
);
2024-08-12 13:58:18 -04:00
if (error) setErrors({ email: error.details[0].message });
else delete errors.email;
};
2024-05-28 21:28:15 +03:30
return (
2024-08-12 13:16:02 -04:00
<Stack className="forgot-password-page auth" overflow="hidden">
2024-07-17 17:44:29 -04:00
<img
className="background-pattern-svg"
src={background}
alt="background pattern"
/>
2024-08-12 13:16:02 -04:00
<Stack
direction="row"
alignItems="center"
px={theme.gap.large}
gap={theme.gap.small}
>
<Logo style={{ borderRadius: theme.shape.borderRadius }} />
<Typography sx={{ userSelect: "none" }}>BlueWave Uptime</Typography>
</Stack>
<Stack
width="100%"
maxWidth={600}
flex={1}
justifyContent="center"
p={theme.gap.xl}
pb={theme.gap.triplexl}
mx="auto"
sx={{
"& > .MuiStack-root": {
border: 1,
borderRadius: theme.shape.borderRadius,
borderColor: theme.palette.otherColors.graishWhite,
backgroundColor: theme.palette.otherColors.white,
padding: {
xs: theme.gap.large,
sm: theme.gap.xl,
},
},
}}
2024-08-12 13:58:18 -04:00
>
<Stack gap={theme.gap.large} alignItems="center" textAlign="center">
<Box>
<Key />
<Typography component="h1">Forgot password?</Typography>
<Typography>
No worries, we'll send you reset instructions.
</Typography>
</Box>
<Box width="100%" maxWidth={400} textAlign="left">
<form noValidate spellCheck={false} onSubmit={handleSubmit}>
<Field
type="email"
id="forgot-password-email-input"
label="Email"
isRequired={true}
placeholder="Enter your email"
value={form.email}
onChange={handleChange}
error={errors.email}
/>
<ButtonSpinner
disabled={errors.email !== undefined}
onClick={handleSubmit}
isLoading={isLoading}
level="primary"
label="Send instructions"
sx={{
width: "100%",
fontWeight: 400,
mt: theme.gap.mlplus,
}}
/>
</form>
</Box>
2024-07-16 12:55:08 -04:00
</Stack>
2024-08-12 13:58:18 -04:00
</Stack>
<Box textAlign="center" p={theme.gap.large}>
<Typography display="inline-block">Go back to —</Typography>
<Typography
component="span"
ml={theme.gap.xs}
onClick={() => {
navigate("/login");
}}
sx={{ userSelect: "none" }}
>
Log In
</Typography>
</Box>
2024-08-12 13:16:02 -04:00
</Stack>
2024-05-28 21:28:15 +03:30
);
};
export default ForgotPassword;