Files
Checkmate/client/src/Components/HOC/withAdminCheck.jsx
T

49 lines
1.1 KiB
React
Raw Normal View History

2025-06-11 12:32:16 +08:00
import { useEffect, useState } from "react";
2024-07-19 10:24:01 -07:00
import { useNavigate } from "react-router-dom";
2024-08-19 19:02:30 -07:00
import { logger } from "../../Utils/Logger";
import { networkService } from "../../main";
2024-07-19 10:24:01 -07:00
const withAdminCheck = (WrappedComponent) => {
const WithAdminCheck = (props) => {
const navigate = useNavigate();
2025-06-11 12:32:16 +08:00
const [isChecking, setIsChecking] = useState(true);
const [superAdminExists, setSuperAdminExists] = useState(false);
2024-07-19 10:24:01 -07:00
useEffect(() => {
networkService
.doesSuperAdminExist()
.then((response) => {
2025-06-11 12:32:16 +08:00
if (response?.data?.data === true) {
navigate("/login");
2025-06-11 12:32:16 +08:00
} else {
2025-06-11 12:41:58 +08:00
setSuperAdminExists(false);
}
})
.catch((error) => {
logger.error(error);
2025-06-11 12:32:16 +08:00
})
.finally(() => {
setIsChecking(false);
});
}, [navigate]);
2025-06-11 12:32:16 +08:00
if (isChecking) {
return null;
}
return (
<WrappedComponent
{...props}
2025-06-11 12:32:16 +08:00
superAdminExists={superAdminExists}
/>
);
};
const wrappedComponentName =
WrappedComponent.displayName || WrappedComponent.name || "Component";
WithAdminCheck.displayName = `WithAdminCheck(${wrappedComponentName})`;
2024-07-19 10:24:01 -07:00
return WithAdminCheck;
2024-07-19 10:24:01 -07:00
};
export default withAdminCheck;