2024-06-07 11:19:32 -07:00
|
|
|
import { useState, useEffect } from "react";
|
2024-06-06 13:10:13 -07:00
|
|
|
import { useNavigate } from "react-router-dom";
|
|
|
|
|
|
2024-05-24 18:32:40 +03:30
|
|
|
import "./index.css";
|
|
|
|
|
import BackgroundPattern from "../../Components/BackgroundPattern/BackgroundPattern";
|
|
|
|
|
import Logomark from "../../assets/Images/Logomark.png";
|
|
|
|
|
import Check from "../../Components/Check/Check";
|
|
|
|
|
import Button from "../../Components/Button";
|
|
|
|
|
import Google from "../../assets/Images/Google.png";
|
2024-06-07 11:19:32 -07:00
|
|
|
import { registerValidation } from "../../Validation/validation";
|
2024-07-09 11:45:14 -07:00
|
|
|
import axiosInstance from "../../Utils/axiosConfig";
|
2024-06-07 16:17:10 -07:00
|
|
|
import { useDispatch } from "react-redux";
|
|
|
|
|
import { register } from "../../Features/Auth/authSlice";
|
2024-07-12 14:00:16 -04:00
|
|
|
import { createToast } from "../../Utils/toastUtils";
|
2024-07-13 13:11:03 -04:00
|
|
|
import Field from "../../Components/TextFields/Field";
|
2024-06-06 13:10:13 -07:00
|
|
|
|
2024-05-06 12:29:36 -04:00
|
|
|
const Register = () => {
|
2024-06-07 16:17:10 -07:00
|
|
|
const dispatch = useDispatch();
|
2024-06-06 13:10:13 -07:00
|
|
|
const navigate = useNavigate();
|
|
|
|
|
|
2024-06-07 11:19:32 -07:00
|
|
|
// TODO If possible, change the IDs of these fields to match the backend
|
2024-06-06 11:41:34 -07:00
|
|
|
const idMap = {
|
|
|
|
|
"register-firstname-input": "firstname",
|
|
|
|
|
"register-lastname-input": "lastname",
|
|
|
|
|
"register-email-input": "email",
|
|
|
|
|
"register-password-input": "password",
|
|
|
|
|
};
|
|
|
|
|
|
2024-06-07 11:19:32 -07:00
|
|
|
const [errors, setErrors] = useState({});
|
|
|
|
|
|
2024-06-06 11:41:34 -07:00
|
|
|
const [form, setForm] = useState({
|
|
|
|
|
firstname: "",
|
|
|
|
|
lastname: "",
|
|
|
|
|
email: "",
|
|
|
|
|
password: "",
|
2024-07-03 11:21:14 -07:00
|
|
|
role: "",
|
2024-06-06 11:41:34 -07:00
|
|
|
});
|
|
|
|
|
|
2024-07-13 13:11:03 -04:00
|
|
|
// useEffect(() => {
|
|
|
|
|
// axiosInstance
|
|
|
|
|
// .get("/auth/users/admin")
|
|
|
|
|
// .then((response) => {
|
|
|
|
|
// if (response.data.data === true) {
|
|
|
|
|
// navigate("/login");
|
|
|
|
|
// }
|
|
|
|
|
// })
|
|
|
|
|
// .catch((error) => {
|
|
|
|
|
// console.log(error);
|
|
|
|
|
// });
|
|
|
|
|
// }, [form, navigate]);
|
2024-07-02 15:54:49 -07:00
|
|
|
|
2024-06-07 11:19:32 -07:00
|
|
|
useEffect(() => {
|
|
|
|
|
const { error } = registerValidation.validate(form, {
|
|
|
|
|
abortEarly: false,
|
2024-06-06 11:41:34 -07:00
|
|
|
});
|
2024-06-07 11:19:32 -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);
|
2024-06-10 11:12:30 -07:00
|
|
|
} else {
|
|
|
|
|
setErrors({});
|
2024-06-07 11:19:32 -07:00
|
|
|
}
|
2024-06-12 13:30:56 -07:00
|
|
|
}, [form]);
|
2024-06-07 11:19:32 -07:00
|
|
|
|
|
|
|
|
const handleInput = (e) => {
|
|
|
|
|
const newForm = { ...form, [idMap[e.target.id]]: e.target.value };
|
|
|
|
|
setForm(newForm);
|
2024-06-06 11:41:34 -07:00
|
|
|
};
|
|
|
|
|
|
2024-06-12 13:49:38 -07:00
|
|
|
const handleSubmit = async (e) => {
|
|
|
|
|
e.preventDefault();
|
2024-06-06 13:10:13 -07:00
|
|
|
try {
|
2024-07-03 11:21:14 -07:00
|
|
|
const adminForm = { ...form, role: "admin" };
|
|
|
|
|
await registerValidation.validateAsync(adminForm, { abortEarly: false });
|
|
|
|
|
const action = await dispatch(register(adminForm));
|
2024-06-07 16:17:10 -07:00
|
|
|
|
|
|
|
|
if (action.meta.requestStatus === "fulfilled") {
|
|
|
|
|
const token = action.payload.data;
|
|
|
|
|
localStorage.setItem("token", token);
|
|
|
|
|
navigate("/");
|
|
|
|
|
}
|
2024-06-07 11:19:32 -07:00
|
|
|
|
2024-06-07 16:17:10 -07:00
|
|
|
if (action.meta.requestStatus === "rejected") {
|
|
|
|
|
const error = new Error("Request rejected");
|
|
|
|
|
error.response = action.payload;
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
2024-06-06 13:10:13 -07:00
|
|
|
} catch (error) {
|
2024-06-07 11:19:32 -07:00
|
|
|
if (error.name === "ValidationError") {
|
2024-07-12 14:00:16 -04:00
|
|
|
// validation errors
|
|
|
|
|
createToast({
|
|
|
|
|
variant: "info",
|
2024-07-12 14:08:30 -04:00
|
|
|
body:
|
|
|
|
|
error && error.details && error.details.length > 0
|
|
|
|
|
? error.details[0].message
|
|
|
|
|
: "Error validating data.",
|
2024-07-12 14:00:16 -04:00
|
|
|
hasIcon: false,
|
|
|
|
|
});
|
2024-06-07 11:19:32 -07:00
|
|
|
} else if (error.response) {
|
2024-07-12 14:00:16 -04:00
|
|
|
// dispatch errors
|
|
|
|
|
createToast({
|
|
|
|
|
variant: "info",
|
|
|
|
|
body: error.response.msg,
|
|
|
|
|
hasIcon: false,
|
|
|
|
|
});
|
2024-06-07 11:19:32 -07:00
|
|
|
} else {
|
2024-07-12 14:00:16 -04:00
|
|
|
// unknown errors
|
|
|
|
|
createToast({
|
|
|
|
|
variant: "info",
|
|
|
|
|
body: "Unknown error.",
|
|
|
|
|
hasIcon: false,
|
|
|
|
|
});
|
2024-06-07 11:19:32 -07:00
|
|
|
}
|
2024-06-06 13:10:13 -07:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2024-07-13 13:11:03 -04:00
|
|
|
const [visibility, setVisibility] = useState(false);
|
|
|
|
|
|
2024-05-24 18:32:40 +03:30
|
|
|
return (
|
|
|
|
|
<div className="register-page">
|
|
|
|
|
<BackgroundPattern></BackgroundPattern>
|
2024-06-12 13:49:38 -07:00
|
|
|
<form className="register-form" onSubmit={handleSubmit}>
|
|
|
|
|
<div className="register-form-header">
|
|
|
|
|
<img
|
|
|
|
|
className="register-form-header-logo"
|
|
|
|
|
src={Logomark}
|
|
|
|
|
alt="Logomark"
|
|
|
|
|
/>
|
|
|
|
|
<div className="register-form-v-spacing-large" />
|
2024-07-12 11:21:33 -07:00
|
|
|
<div className="register-form-heading">
|
|
|
|
|
Create Uptime Manager admin account
|
|
|
|
|
</div>
|
2024-06-12 13:49:38 -07:00
|
|
|
<div className="register-form-v-spacing-large"></div>
|
|
|
|
|
</div>
|
|
|
|
|
<div className="register-form-v-spacing-40px" />
|
|
|
|
|
<div className="register-form-inputs">
|
2024-07-13 13:11:03 -04:00
|
|
|
<Field
|
2024-06-12 13:49:38 -07:00
|
|
|
id="register-firstname-input"
|
2024-07-13 13:11:03 -04:00
|
|
|
label="Name"
|
|
|
|
|
isRequired={true}
|
|
|
|
|
placeholder="Talha"
|
|
|
|
|
autoComplete="given-name"
|
|
|
|
|
onChange={handleInput}
|
|
|
|
|
error={errors.firstname}
|
2024-06-12 13:49:38 -07:00
|
|
|
/>
|
|
|
|
|
<div className="login-form-v2-spacing" />
|
2024-07-13 13:11:03 -04:00
|
|
|
<Field
|
2024-06-12 13:49:38 -07:00
|
|
|
id="register-lastname-input"
|
2024-07-13 13:11:03 -04:00
|
|
|
label="Surname"
|
|
|
|
|
isRequired={true}
|
|
|
|
|
placeholder="Bolat"
|
|
|
|
|
autoComplete="family-name"
|
|
|
|
|
onChange={handleInput}
|
|
|
|
|
error={errors.lastname}
|
2024-06-12 13:49:38 -07:00
|
|
|
/>
|
2024-05-24 18:32:40 +03:30
|
|
|
<div className="login-form-v2-spacing" />
|
2024-07-13 13:11:03 -04:00
|
|
|
<Field
|
|
|
|
|
type="email"
|
2024-06-12 13:49:38 -07:00
|
|
|
id="register-email-input"
|
2024-07-13 13:11:03 -04:00
|
|
|
label="Email"
|
|
|
|
|
isRequired={true}
|
|
|
|
|
placeholder="name.surname@companyname.com"
|
|
|
|
|
autoComplete="email"
|
|
|
|
|
onChange={handleInput}
|
|
|
|
|
error={errors.email}
|
2024-06-12 13:49:38 -07:00
|
|
|
/>
|
2024-05-24 18:32:40 +03:30
|
|
|
<div className="login-form-v2-spacing" />
|
2024-07-13 13:11:03 -04:00
|
|
|
<Field
|
|
|
|
|
type="password"
|
|
|
|
|
id="register-password-input"
|
|
|
|
|
label="Password"
|
|
|
|
|
isRequired={true}
|
|
|
|
|
isVisible={visibility}
|
|
|
|
|
setVisibility={setVisibility}
|
2024-06-12 13:49:38 -07:00
|
|
|
placeholder="Create a password"
|
2024-06-12 13:51:28 -07:00
|
|
|
autoComplete="current-password"
|
2024-07-13 13:11:03 -04:00
|
|
|
error={errors.password}
|
|
|
|
|
onChange={handleInput}
|
|
|
|
|
/>
|
|
|
|
|
<div className="login-form-v2-spacing" />
|
|
|
|
|
{/* TODO - hook up to form state and run checks */}
|
|
|
|
|
<Field
|
|
|
|
|
type="password"
|
|
|
|
|
id="register-confirm-input"
|
|
|
|
|
label="Confirm password"
|
|
|
|
|
isRequired={true}
|
|
|
|
|
isVisible={visibility}
|
|
|
|
|
setVisibility={setVisibility}
|
|
|
|
|
placeholder="Confirm your password"
|
|
|
|
|
autoComplete="current-password"
|
|
|
|
|
error={errors.confirm}
|
|
|
|
|
onChange={handleInput}
|
2024-06-12 13:49:38 -07:00
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
<div className="login-form-v2-spacing" />
|
|
|
|
|
<div className="register-form-checks">
|
|
|
|
|
<Check text="Must be at least 8 characters" />
|
|
|
|
|
<div className="register-form-v-spacing-small"></div>
|
|
|
|
|
<Check text="Must contain one special character" />
|
|
|
|
|
</div>
|
|
|
|
|
<div className="login-form-v2-spacing" />
|
|
|
|
|
<div className="register-form-actions">
|
|
|
|
|
<Button
|
|
|
|
|
type="submit"
|
|
|
|
|
level="primary"
|
|
|
|
|
label="Get started"
|
|
|
|
|
sx={{ width: "100%" }}
|
|
|
|
|
/>
|
|
|
|
|
<div className="login-form-v-spacing" />
|
|
|
|
|
<Button
|
|
|
|
|
disabled={true}
|
|
|
|
|
level="secondary"
|
|
|
|
|
label="Sign up with Google"
|
|
|
|
|
sx={{ width: "100%", color: "#344054", fontWeight: "700" }}
|
|
|
|
|
img={<img className="google-enter" src={Google} alt="Google" />}
|
|
|
|
|
/>
|
2024-05-24 18:32:40 +03:30
|
|
|
</div>
|
2024-06-07 11:19:32 -07:00
|
|
|
</form>
|
2024-06-12 13:49:38 -07:00
|
|
|
<div className="register-bottom-spacing"></div>
|
2024-05-24 18:32:40 +03:30
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|
2024-05-06 12:29:36 -04:00
|
|
|
|
2024-05-24 18:32:40 +03:30
|
|
|
export default Register;
|