update admin check HOC
This commit is contained in:
1 parent
fac960b9ac
commit
3ade8b5bd4
1 file changed
+15
-3
@@ -1,4 +1,4 @@
|
|||||||
import { useEffect } from "react";
|
import { useEffect, useState } from "react";
|
||||||
import { useNavigate } from "react-router-dom";
|
import { useNavigate } from "react-router-dom";
|
||||||
|
|
||||||
import { logger } from "../../Utils/Logger";
|
import { logger } from "../../Utils/Logger";
|
||||||
@@ -7,23 +7,35 @@ import { networkService } from "../../main";
|
|||||||
const withAdminCheck = (WrappedComponent) => {
|
const withAdminCheck = (WrappedComponent) => {
|
||||||
const WithAdminCheck = (props) => {
|
const WithAdminCheck = (props) => {
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
|
const [isChecking, setIsChecking] = useState(true);
|
||||||
|
const [superAdminExists, setSuperAdminExists] = useState(false);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
networkService
|
networkService
|
||||||
.doesSuperAdminExist()
|
.doesSuperAdminExist()
|
||||||
.then((response) => {
|
.then((response) => {
|
||||||
if (response.data.data === true) {
|
if (response?.data?.data === true) {
|
||||||
navigate("/login");
|
navigate("/login");
|
||||||
|
} else {
|
||||||
|
setSuperAdminExists(true);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
logger.error(error);
|
logger.error(error);
|
||||||
|
})
|
||||||
|
.finally(() => {
|
||||||
|
setIsChecking(false);
|
||||||
});
|
});
|
||||||
}, [navigate]);
|
}, [navigate]);
|
||||||
|
|
||||||
|
if (isChecking) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<WrappedComponent
|
<WrappedComponent
|
||||||
{...props}
|
{...props}
|
||||||
isSuperAdmin={true}
|
superAdminExists={superAdminExists}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in new issue
Block a user