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";
|
2024-07-15 19:29:02 -04:00
|
|
|
import Logomark from "../../assets/Images/bwl-logo-2.svg?react";
|
2024-05-24 18:32:40 +03:30
|
|
|
import Check from "../../Components/Check/Check";
|
|
|
|
|
import Button from "../../Components/Button";
|
|
|
|
|
import Google from "../../assets/Images/Google.png";
|
2024-07-15 18:47:23 -04:00
|
|
|
import { credentials } 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 14:54:06 -04:00
|
|
|
import Field from "../../Components/Inputs/Field";
|
2024-07-15 18:47:23 -04:00
|
|
|
import { useTheme } from "@emotion/react";
|
2024-07-15 19:29:02 -04:00
|
|
|
import { Divider, Stack, Typography } from "@mui/material";
|
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-07-15 18:47:23 -04:00
|
|
|
const theme = useTheme();
|
2024-06-06 13:10:13 -07:00
|
|
|
|
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-07-15 18:47:23 -04:00
|
|
|
"register-confirm-input": "confirm",
|
2024-06-06 11:41:34 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const [form, setForm] = useState({
|
|
|
|
|
firstname: "",
|
|
|
|
|
lastname: "",
|
|
|
|
|
email: "",
|
|
|
|
|
password: "",
|
2024-07-15 18:47:23 -04:00
|
|
|
confirm: "",
|
2024-07-03 11:21:14 -07:00
|
|
|
role: "",
|
2024-06-06 11:41:34 -07:00
|
|
|
});
|
2024-07-15 18:47:23 -04:00
|
|
|
const [errors, setErrors] = useState({});
|
2024-06-06 11:41:34 -07:00
|
|
|
|
2024-07-15 18:47:23 -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-07-15 18:47:23 -04:00
|
|
|
const handleSubmit = async (e) => {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
|
|
const adminForm = { ...form, role: "admin" };
|
|
|
|
|
const { error } = credentials.validate(adminForm, {
|
2024-06-07 11:19:32 -07:00
|
|
|
abortEarly: false,
|
2024-07-15 18:47:23 -04:00
|
|
|
context: { password: form.password },
|
2024-06-06 11:41:34 -07:00
|
|
|
});
|
2024-06-07 11:19:32 -07:00
|
|
|
|
|
|
|
|
if (error) {
|
2024-07-15 18:47:23 -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:12:30 -07:00
|
|
|
} else {
|
2024-07-15 18:47:23 -04:00
|
|
|
delete adminForm.confirm;
|
2024-07-03 11:21:14 -07:00
|
|
|
const action = await dispatch(register(adminForm));
|
2024-07-15 18:47:23 -04:00
|
|
|
if (action.payload.success) {
|
2024-06-07 16:17:10 -07:00
|
|
|
const token = action.payload.data;
|
|
|
|
|
localStorage.setItem("token", token);
|
|
|
|
|
navigate("/");
|
2024-07-12 14:00:16 -04:00
|
|
|
createToast({
|
|
|
|
|
variant: "info",
|
2024-07-15 18:47:23 -04:00
|
|
|
body: "Welcome! Your account was created successfully.",
|
2024-07-12 14:00:16 -04:00
|
|
|
hasIcon: false,
|
|
|
|
|
});
|
2024-06-07 11:19:32 -07:00
|
|
|
} else {
|
2024-07-15 18:47:23 -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-07 11:19:32 -07:00
|
|
|
}
|
2024-06-06 13:10:13 -07:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2024-07-15 18:47:23 -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-07-13 13:11:03 -04:00
|
|
|
|
2024-05-24 18:32:40 +03:30
|
|
|
return (
|
|
|
|
|
<div className="register-page">
|
|
|
|
|
<BackgroundPattern></BackgroundPattern>
|
2024-07-15 18:47:23 -04:00
|
|
|
<form className="register-form" onSubmit={handleSubmit} noValidate>
|
2024-07-15 19:29:02 -04:00
|
|
|
<Stack gap={theme.gap.large} direction="column">
|
|
|
|
|
<Logomark alt="BlueWave Uptime Icon" />
|
|
|
|
|
<Button
|
|
|
|
|
level="secondary"
|
|
|
|
|
label="Sign up with Google"
|
|
|
|
|
sx={{ fontWeight: 600, mt: theme.gap.xxl }}
|
|
|
|
|
img={<img className="google-enter" src={Google} alt="Google" />}
|
2024-06-12 13:49:38 -07:00
|
|
|
/>
|
2024-07-15 19:29:02 -04:00
|
|
|
<Divider>
|
|
|
|
|
<Typography>or</Typography>
|
|
|
|
|
</Divider>
|
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}
|
2024-07-15 19:29:02 -04:00
|
|
|
placeholder="Daniel"
|
2024-07-13 13:11:03 -04:00
|
|
|
autoComplete="given-name"
|
2024-07-15 18:47:23 -04:00
|
|
|
value={form.firstname}
|
|
|
|
|
onChange={handleChange}
|
2024-07-13 13:11:03 -04:00
|
|
|
error={errors.firstname}
|
2024-06-12 13:49:38 -07:00
|
|
|
/>
|
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}
|
2024-07-15 19:29:02 -04:00
|
|
|
placeholder="Cojocea"
|
2024-07-13 13:11:03 -04:00
|
|
|
autoComplete="family-name"
|
2024-07-15 18:47:23 -04:00
|
|
|
value={form.lastname}
|
|
|
|
|
onChange={handleChange}
|
2024-07-13 13:11:03 -04:00
|
|
|
error={errors.lastname}
|
2024-06-12 13:49:38 -07:00
|
|
|
/>
|
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}
|
2024-07-15 19:29:02 -04:00
|
|
|
placeholder="daniel.cojocea@domain.com"
|
2024-07-13 13:11:03 -04:00
|
|
|
autoComplete="email"
|
2024-07-15 18:47:23 -04:00
|
|
|
value={form.email}
|
|
|
|
|
onChange={handleChange}
|
2024-07-13 13:11:03 -04:00
|
|
|
error={errors.email}
|
2024-06-12 13:49:38 -07:00
|
|
|
/>
|
2024-07-13 13:11:03 -04:00
|
|
|
<Field
|
|
|
|
|
type="password"
|
|
|
|
|
id="register-password-input"
|
|
|
|
|
label="Password"
|
|
|
|
|
isRequired={true}
|
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-15 18:47:23 -04:00
|
|
|
value={form.password}
|
|
|
|
|
onChange={handleChange}
|
2024-07-13 13:11:03 -04:00
|
|
|
error={errors.password}
|
|
|
|
|
/>
|
|
|
|
|
<Field
|
|
|
|
|
type="password"
|
|
|
|
|
id="register-confirm-input"
|
|
|
|
|
label="Confirm password"
|
|
|
|
|
isRequired={true}
|
|
|
|
|
placeholder="Confirm your password"
|
|
|
|
|
autoComplete="current-password"
|
2024-07-15 18:47:23 -04:00
|
|
|
value={form.confirm}
|
|
|
|
|
onChange={handleChange}
|
2024-07-13 13:11:03 -04:00
|
|
|
error={errors.confirm}
|
2024-06-12 13:49:38 -07:00
|
|
|
/>
|
2024-07-15 19:29:02 -04:00
|
|
|
<Stack gap={theme.gap.small}>
|
|
|
|
|
<Check text="Must be at least 8 characters" />
|
|
|
|
|
<Check text="Must contain one special character" />
|
|
|
|
|
<Check text="Must contain at least one upper and lower character" />
|
|
|
|
|
</Stack>
|
2024-06-12 13:49:38 -07:00
|
|
|
<Button
|
|
|
|
|
type="submit"
|
|
|
|
|
level="primary"
|
|
|
|
|
label="Get started"
|
2024-07-15 19:29:02 -04:00
|
|
|
sx={{ marginBottom: theme.gap.large }}
|
2024-06-12 13:49:38 -07:00
|
|
|
/>
|
2024-07-15 19:29:02 -04:00
|
|
|
</Stack>
|
2024-06-07 11:19:32 -07:00
|
|
|
</form>
|
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;
|