From dcb7ccee80744ddd68bc9b669ef4771129c0a09e Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Fri, 19 Jul 2024 10:29:43 -0700 Subject: [PATCH] Add conditional rendering for admin account --- Client/src/HOC/withAdminCheck.jsx | 2 +- Client/src/Pages/Auth/Register/Register.jsx | 13 +++++++------ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/Client/src/HOC/withAdminCheck.jsx b/Client/src/HOC/withAdminCheck.jsx index 2d8812728..5550219f2 100644 --- a/Client/src/HOC/withAdminCheck.jsx +++ b/Client/src/HOC/withAdminCheck.jsx @@ -19,7 +19,7 @@ const withAdminCheck = (WrappedComponent) => { console.log(error); }); }, [navigate]); - return ; + return ; }; const wrappedComponentName = WrappedComponent.displayName || WrappedComponent.name || "Component"; diff --git a/Client/src/Pages/Auth/Register/Register.jsx b/Client/src/Pages/Auth/Register/Register.jsx index dae616d3a..cb68cc349 100644 --- a/Client/src/Pages/Auth/Register/Register.jsx +++ b/Client/src/Pages/Auth/Register/Register.jsx @@ -15,7 +15,8 @@ import Field from "../../../Components/Inputs/Field"; import withAdminCheck from "../../../HOC/withAdminCheck"; import { register } from "../../../Features/Auth/authSlice"; -const Register = () => { +const Register = ({ isAdmin }) => { + console.log(isAdmin); const dispatch = useDispatch(); const navigate = useNavigate(); const theme = useTheme(); @@ -42,8 +43,8 @@ const Register = () => { const handleSubmit = async (e) => { e.preventDefault(); - const adminForm = { ...form, role: "admin" }; - const { error } = credentials.validate(adminForm, { + const form = { ...form, role: isAdmin ? "admin" : "user" }; + const { error } = credentials.validate(form, { abortEarly: false, context: { password: form.password }, }); @@ -62,8 +63,8 @@ const Register = () => { : "Error validating data.", }); } else { - delete adminForm.confirm; - const action = await dispatch(register(adminForm)); + delete form.confirm; + const action = await dispatch(register(form)); if (action.payload.success) { const token = action.payload.data; localStorage.setItem("token", token); @@ -119,7 +120,7 @@ const Register = () => { - Create admin account + Create{isAdmin ? " admin " : " "}account