2024-06-10 10:38:00 -07:00
|
|
|
|
import { useState, useEffect } from "react";
|
2024-05-22 17:41:24 +03:30
|
|
|
|
import "./index.css";
|
|
|
|
|
|
import BackgroundPattern from "../../Components/BackgroundPattern/BackgroundPattern";
|
|
|
|
|
|
import Logomark from "../../assets/Images/Logomark.png";
|
|
|
|
|
|
import EmailTextField from "../../Components/TextFields/Email/EmailTextField";
|
|
|
|
|
|
import CheckBox from "../../Components/Checkbox/Checkbox";
|
|
|
|
|
|
import Button from "../../Components/Button";
|
|
|
|
|
|
import Google from "../../assets/Images/Google.png";
|
|
|
|
|
|
import PasswordTextField from "../../Components/TextFields/Password/PasswordTextField";
|
2024-05-06 12:29:36 -04:00
|
|
|
|
|
2024-06-10 10:38:00 -07:00
|
|
|
|
import { loginValidation } from "../../Validation/validation";
|
|
|
|
|
|
import { login } from "../../Features/Auth/authSlice";
|
|
|
|
|
|
import { useDispatch } from "react-redux";
|
|
|
|
|
|
|
2024-05-06 12:29:36 -04:00
|
|
|
|
const Login = () => {
|
2024-06-10 10:38:00 -07:00
|
|
|
|
const dispatch = useDispatch();
|
|
|
|
|
|
|
2024-06-06 17:41:09 -07:00
|
|
|
|
const idMap = {
|
|
|
|
|
|
"login-email-input": "email",
|
|
|
|
|
|
"login-password-input": "password",
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2024-06-10 10:38:00 -07:00
|
|
|
|
const [errors, setErrors] = useState({});
|
|
|
|
|
|
|
2024-06-06 17:41:09 -07:00
|
|
|
|
const [form, setForm] = useState({
|
|
|
|
|
|
email: "",
|
|
|
|
|
|
password: "",
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2024-06-10 10:38:00 -07:00
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
const { error } = loginValidation.validate(form, {
|
|
|
|
|
|
abortEarly: false,
|
2024-06-06 17:41:09 -07:00
|
|
|
|
});
|
2024-06-10 10:38:00 -07:00
|
|
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
|
|
// Creates an error object in the format { field: message }
|
|
|
|
|
|
const validationErrors = error.details.reduce((acc, err) => {
|
|
|
|
|
|
return { ...acc, [err.path[0]]: err.message };
|
|
|
|
|
|
}, {});
|
|
|
|
|
|
setErrors(validationErrors);
|
|
|
|
|
|
}
|
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
|
|
const handleSubmit = async () => {
|
|
|
|
|
|
try {
|
|
|
|
|
|
await loginValidation.validateAsync(form, { abortEarly: false });
|
|
|
|
|
|
dispatch(login(form));
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
console.log(error);
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const handleInput = (e) => {
|
|
|
|
|
|
const newForm = { ...form, [idMap[e.target.id]]: e.target.value };
|
|
|
|
|
|
setForm(newForm);
|
2024-06-06 17:41:09 -07:00
|
|
|
|
};
|
|
|
|
|
|
|
2024-05-22 17:41:24 +03:30
|
|
|
|
return (
|
|
|
|
|
|
<div className="login-page">
|
|
|
|
|
|
<BackgroundPattern></BackgroundPattern>
|
|
|
|
|
|
<div className="login-form">
|
|
|
|
|
|
<div className="login-form-header">
|
|
|
|
|
|
<img
|
|
|
|
|
|
className="login-form-header-logo"
|
|
|
|
|
|
src={Logomark}
|
|
|
|
|
|
alt="Logomark"
|
|
|
|
|
|
/>
|
|
|
|
|
|
<div className="login-form-v-spacing" />
|
|
|
|
|
|
<div className="login-form-heading">Log in to your account</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<div className="login-form-v3-spacing" />
|
|
|
|
|
|
<div className="login-form-inputs">
|
|
|
|
|
|
<EmailTextField
|
2024-06-06 17:41:09 -07:00
|
|
|
|
onChange={handleInput}
|
2024-06-10 10:38:00 -07:00
|
|
|
|
error={errors.email ? true : false}
|
|
|
|
|
|
helperText={errors.email ? errors.email : ""}
|
2024-05-22 17:41:24 +03:30
|
|
|
|
placeholder="Enter your email"
|
|
|
|
|
|
id="login-email-input"
|
|
|
|
|
|
/>
|
|
|
|
|
|
<div className="login-form-v2-spacing" />
|
|
|
|
|
|
<PasswordTextField
|
2024-06-06 17:41:09 -07:00
|
|
|
|
onChange={handleInput}
|
2024-06-10 10:38:00 -07:00
|
|
|
|
error={errors.password ? true : false}
|
|
|
|
|
|
helperText={errors.password ? errors.password : ""}
|
2024-05-22 17:41:24 +03:30
|
|
|
|
placeholder="Password"
|
2024-06-06 17:41:09 -07:00
|
|
|
|
id="login-password-input"
|
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
|
|
|
|
|
|
level="primary"
|
|
|
|
|
|
label="Sign in"
|
|
|
|
|
|
sx={{ width: "100%" }}
|
|
|
|
|
|
onClick={handleSubmit}
|
|
|
|
|
|
/>
|
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 className="new-account-option-span">Sign up</span>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
);
|
|
|
|
|
|
};
|
2024-05-06 12:29:36 -04:00
|
|
|
|
|
2024-05-22 17:41:24 +03:30
|
|
|
|
export default Login;
|