Files
Checkmate/Client/src/Pages/Login/index.jsx
T

189 lines
5.4 KiB
React
Raw Normal View History

2024-06-10 10:38:00 -07:00
import { useState, useEffect } from "react";
2024-06-10 11:53:10 -07:00
import { useNavigate } from "react-router-dom";
2024-05-22 17:41:24 +03:30
import "./index.css";
import BackgroundPattern from "../../Components/BackgroundPattern/BackgroundPattern";
2024-07-13 10:52:50 +03:30
import Logomark from "../../assets/Images/bwl-logo-2.svg?react";
2024-05-22 17:41:24 +03:30
import CheckBox from "../../Components/Checkbox/Checkbox";
import Button from "../../Components/Button";
import Google from "../../assets/Images/Google.png";
import axiosInstance from "../../Utils/axiosConfig";
2024-07-15 16:32:24 -04:00
import { loginValidation, credentials } from "../../Validation/validation";
2024-06-10 10:38:00 -07:00
import { login } from "../../Features/Auth/authSlice";
import { useDispatch } from "react-redux";
2024-07-12 14:00:16 -04:00
import { createToast } from "../../Utils/toastUtils";
import Field from "../../Components/Inputs/Field";
2024-06-10 10:38:00 -07:00
2024-05-06 12:29:36 -04:00
const Login = () => {
2024-06-10 10:38:00 -07:00
const dispatch = useDispatch();
2024-06-10 11:53:10 -07:00
const navigate = useNavigate();
2024-06-10 10:38:00 -07:00
2024-06-06 17:41:09 -07:00
const idMap = {
"login-email-input": "email",
"login-password-input": "password",
};
const [form, setForm] = useState({
email: "",
password: "",
});
2024-07-15 16:32:24 -04:00
const [errors, setErrors] = useState({});
2024-06-06 17:41:09 -07:00
useEffect(() => {
axiosInstance
.get("/auth/users/admin")
.then((response) => {
if (response.data.data === false) {
navigate("/register");
}
})
.catch((error) => {
console.log(error);
});
}, [navigate]);
2024-07-15 16:32:24 -04:00
const handleSubmit = async (e) => {
e.preventDefault();
const { error } = credentials.validate(form, { abortEarly: false });
2024-06-10 10:38:00 -07:00
if (error) {
2024-07-15 16:32:24 -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-10 11:53:10 -07:00
} else {
const action = await dispatch(login(form));
2024-07-15 16:32:24 -04:00
if (action.payload.success) {
navigate("/monitors");
2024-07-12 14:00:16 -04:00
createToast({
variant: "info",
2024-07-15 16:32:24 -04:00
body: "Welcome back! You're successfully logged in.",
2024-07-12 14:00:16 -04:00
hasIcon: false,
});
2024-06-10 11:53:10 -07:00
} else {
2024-07-15 16:32:24 -04:00
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-10 11:53:10 -07:00
}
2024-06-10 10:38:00 -07:00
}
};
2024-07-15 16:32:24 -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 }
);
2024-06-06 17:41:09 -07:00
2024-07-15 16:32:24 -04:00
setErrors((prev) => {
const prevErrors = { ...prev };
if (error) prevErrors[name] = error.details[0].message;
else delete prevErrors[name];
return prevErrors;
});
};
2024-05-22 17:41:24 +03:30
return (
<div className="login-page">
<BackgroundPattern></BackgroundPattern>
2024-06-12 13:49:38 -07:00
<form className="login-form" onSubmit={handleSubmit}>
2024-05-22 17:41:24 +03:30
<div className="login-form-header">
2024-07-13 10:52:50 +03:30
<Logomark id="login-form-header-logo" alt="Logomark" />
2024-05-22 17:41:24 +03:30
<div className="login-form-v-spacing" />
<div className="login-form-heading">Log in to your account</div>
</div>
<div className="login-form-v3-spacing" />
2024-05-22 17:41:24 +03:30
<div className="login-form-inputs">
<Field
type="email"
id="login-email-input"
label="Email"
2024-07-15 16:32:24 -04:00
isRequired={true}
2024-05-22 17:41:24 +03:30
placeholder="Enter your email"
2024-06-12 13:51:28 -07:00
autoComplete="email"
2024-07-15 16:32:24 -04:00
value={form.email}
onChange={handleChange}
error={errors.email}
2024-05-22 17:41:24 +03:30
/>
<div className="login-form-v2-spacing" />
<Field
type="password"
2024-06-06 17:41:09 -07:00
id="login-password-input"
label="Password"
2024-07-15 16:32:24 -04:00
isRequired={true}
placeholder="Enter your password"
autoComplete="current-password"
2024-07-15 16:32:24 -04:00
value={form.password}
onChange={handleChange}
error={errors.password}
2024-05-22 17:41:24 +03:30
/>
</div>
<div className="login-form-v3-spacing" />
<div className="login-form-password-options">
<CheckBox />
<div className="login-form-forgot-password">Forgot password</div>
</div>
<div className="login-form-v3-spacing" />
<div className="login-form-actions">
2024-06-10 10:38:00 -07:00
<Button
2024-06-12 13:49:38 -07:00
type="submit"
2024-06-10 10:38:00 -07:00
level="primary"
label="Sign in"
sx={{ width: "100%" }}
/>
2024-05-22 17:41:24 +03:30
<div className="login-form-v-spacing" />
<Button
level="secondary"
label="Sign in with Google"
sx={{ width: "100%", color: "#344054", fontWeight: "700" }}
img={<img className="google-enter" src={Google} alt="Google" />}
/>
</div>
<div className="login-form-v3-spacing" />
<div className="new-account-option">
Don’t have an account?
<span
onClick={() => {
navigate("/register");
}}
className="new-account-option-span"
>
Sign up
</span>
2024-05-22 17:41:24 +03:30
</div>
2024-06-12 13:49:38 -07:00
</form>
2024-05-22 17:41:24 +03:30
</div>
);
};
2024-05-06 12:29:36 -04:00
2024-05-22 17:41:24 +03:30
export default Login;