Conditionally render some components based on admin status

This commit is contained in:
Alex Holliday committed 2024-08-27 12:54:44 -07:00
1 parent eedc6bbed6
commit cd6f55a9c9
6 files changed
+110 -63

No files matched your search

+7 -3
View File
@@ -21,6 +21,7 @@ import ProtectedRoute from "./Components/ProtectedRoute";
import Details from "./Pages/Monitors/Details"; import Details from "./Pages/Monitors/Details";
import Maintenance from "./Pages/Maintenance"; import Maintenance from "./Pages/Maintenance";
import withAdminCheck from "./HOC/withAdminCheck"; import withAdminCheck from "./HOC/withAdminCheck";
import withAdminProp from "./HOC/withAdminProp";
import Configure from "./Pages/Monitors/Configure"; import Configure from "./Pages/Monitors/Configure";
import PageSpeed from "./Pages/PageSpeed"; import PageSpeed from "./Pages/PageSpeed";
import CreatePageSpeed from "./Pages/PageSpeed/CreatePageSpeed"; import CreatePageSpeed from "./Pages/PageSpeed/CreatePageSpeed";
@@ -30,6 +31,9 @@ import PageSpeedConfigure from "./Pages/PageSpeed/Configure";
function App() { function App() {
const AdminCheckedRegister = withAdminCheck(Register); const AdminCheckedRegister = withAdminCheck(Register);
const MonitorsWithAdminProp = withAdminProp(Monitors);
const DetailsWithAdminProp = withAdminProp(Details);
return ( return (
<> <>
<Routes> <Routes>
@@ -37,11 +41,11 @@ function App() {
<Route <Route
exact exact
path="/" path="/"
element={<ProtectedRoute Component={Monitors} />} element={<ProtectedRoute Component={MonitorsWithAdminProp} />}
/> />
<Route <Route
path="/monitors" path="/monitors"
element={<ProtectedRoute Component={Monitors} />} element={<ProtectedRoute Component={MonitorsWithAdminProp} />}
/> />
<Route <Route
path="/monitors/create" path="/monitors/create"
@@ -49,7 +53,7 @@ function App() {
/> />
<Route <Route
path="/monitors/:monitorId/" path="/monitors/:monitorId/"
element={<ProtectedRoute Component={Details} />} element={<ProtectedRoute Component={DetailsWithAdminProp} />}
/> />
<Route <Route
path="/monitors/configure/:monitorId/" path="/monitors/configure/:monitorId/"
+11 -8
View File
@@ -19,7 +19,7 @@ import "./index.css";
* @returns {JSX.Element} The rendered fallback UI. * @returns {JSX.Element} The rendered fallback UI.
*/ */
const Fallback = ({ title, checks, link = "/" }) => { const Fallback = ({ title, checks, link = "/", isAdmin }) => {
const theme = useTheme(); const theme = useTheme();
const navigate = useNavigate(); const navigate = useNavigate();
@@ -36,7 +36,7 @@ const Fallback = ({ title, checks, link = "/" }) => {
/> />
<Stack gap={theme.gap.small} maxWidth={"275px"} zIndex={1}> <Stack gap={theme.gap.small} maxWidth={"275px"} zIndex={1}>
<Typography component="h1" marginY={theme.gap.medium}> <Typography component="h1" marginY={theme.gap.medium}>
A {title} is used to: A {title} monitor is used to:
</Typography> </Typography>
{checks.map((check, index) => ( {checks.map((check, index) => (
<Check <Check
@@ -46,12 +46,14 @@ const Fallback = ({ title, checks, link = "/" }) => {
/> />
))} ))}
</Stack> </Stack>
<Button {isAdmin && (
level="primary" <Button
label={`Let's create your ${title}`} level="primary"
sx={{ alignSelf: "center" }} label={`Let's create your ${title}`}
onClick={() => navigate(link)} sx={{ alignSelf: "center" }}
/> onClick={() => navigate(link)}
/>
)}
</Stack> </Stack>
); );
}; };
@@ -60,6 +62,7 @@ Fallback.propTypes = {
title: PropTypes.string.isRequired, title: PropTypes.string.isRequired,
checks: PropTypes.arrayOf(PropTypes.string).isRequired, checks: PropTypes.arrayOf(PropTypes.string).isRequired,
link: PropTypes.string, link: PropTypes.string,
isAdmin: PropTypes.bool,
}; };
export default Fallback; export default Fallback;
+20
View File
@@ -0,0 +1,20 @@
import { useSelector } from "react-redux";
const withAdminProp = (WrappedComponent) => {
const WithAdminProp = (props) => {
const { user } = useSelector((state) => state.auth);
const isAdmin =
(user?.role?.includes("admin") ?? false) ||
(user?.role?.includes("superadmin") ?? false);
return <WrappedComponent {...props} isAdmin={isAdmin} />;
};
const wrappedComponentName =
WrappedComponent.displayName || WrappedComponent.name || "Component";
WithAdminProp.displayName = `WithAdminProp(${wrappedComponentName})`;
return WithAdminProp;
};
export default withAdminProp;
+29 -24
View File
@@ -110,7 +110,7 @@ const SkeletonLayout = () => {
* Details page component displaying monitor details and related information. * Details page component displaying monitor details and related information.
* @component * @component
*/ */
const DetailsPage = () => { const DetailsPage = ({ isAdmin }) => {
const [monitor, setMonitor] = useState({}); const [monitor, setMonitor] = useState({});
const { monitorId } = useParams(); const { monitorId } = useParams();
const { authToken } = useSelector((state) => state.auth); const { authToken } = useSelector((state) => state.auth);
@@ -196,29 +196,31 @@ const DetailsPage = () => {
Checking every {formatDurationRounded(monitor?.interval)}. Checking every {formatDurationRounded(monitor?.interval)}.
</Typography> </Typography>
</Box> </Box>
<Button {isAdmin && (
level="tertiary" <Button
label="Configure" level="tertiary"
animate="rotate90" label="Configure"
img={ animate="rotate90"
<SettingsIcon img={
style={{ <SettingsIcon
minWidth: theme.gap.mlplus, style={{
minHeight: theme.gap.mlplus, minWidth: theme.gap.mlplus,
}} minHeight: theme.gap.mlplus,
/> }}
} />
onClick={() => navigate(`/monitors/configure/${monitorId}`)} }
sx={{ onClick={() => navigate(`/monitors/configure/${monitorId}`)}
ml: "auto", sx={{
alignSelf: "flex-end", ml: "auto",
backgroundColor: "#f4f4f4", alignSelf: "flex-end",
px: theme.gap.medium, backgroundColor: "#f4f4f4",
"& svg": { px: theme.gap.medium,
mr: "6px", "& svg": {
}, mr: "6px",
}} },
/> }}
/>
)}
</Stack> </Stack>
<Stack <Stack
direction="row" direction="row"
@@ -339,4 +341,7 @@ const DetailsPage = () => {
); );
}; };
DetailsPage.propTypes = {
isAdmin: PropTypes.bool,
};
export default DetailsPage; export default DetailsPage;
+38 -27
View File
@@ -28,13 +28,14 @@ import Settings from "../../assets/icons/settings-bold.svg?react";
import PropTypes from "prop-types"; import PropTypes from "prop-types";
import BarChart from "../../Components/Charts/BarChart"; import BarChart from "../../Components/Charts/BarChart";
const ActionsMenu = ({ monitor }) => { const ActionsMenu = ({ monitor, isAdmin }) => {
const [anchorEl, setAnchorEl] = useState(null); const [anchorEl, setAnchorEl] = useState(null);
const [actions, setActions] = useState({}); const [actions, setActions] = useState({});
const [isOpen, setIsOpen] = useState(false); const [isOpen, setIsOpen] = useState(false);
const dispatch = useDispatch(); const dispatch = useDispatch();
const theme = useTheme(); const theme = useTheme();
const authState = useSelector((state) => state.auth); const authState = useSelector((state) => state.auth);
const handleRemove = async (event) => { const handleRemove = async (event) => {
event.preventDefault(); event.preventDefault();
event.stopPropagation(); event.stopPropagation();
@@ -119,23 +120,27 @@ const ActionsMenu = ({ monitor }) => {
</MenuItem> </MenuItem>
{/* TODO - pass monitor id to Incidents page */} {/* TODO - pass monitor id to Incidents page */}
<MenuItem disabled>Incidents</MenuItem> <MenuItem disabled>Incidents</MenuItem>
<MenuItem {isAdmin && (
onClick={(e) => { <MenuItem
e.stopPropagation(); onClick={(e) => {
e.stopPropagation();
navigate(`/monitors/configure/${actions.id}`); navigate(`/monitors/configure/${actions.id}`);
}} }}
> >
Configure Configure
</MenuItem> </MenuItem>
<MenuItem )}
onClick={(e) => { {isAdmin && (
e.stopPropagation(); <MenuItem
openRemove(e); onClick={(e) => {
}} e.stopPropagation();
> openRemove(e);
Remove }}
</MenuItem> >
Remove
</MenuItem>
)}
</Menu> </Menu>
<Modal <Modal
aria-labelledby="modal-delete-monitor" aria-labelledby="modal-delete-monitor"
@@ -197,6 +202,7 @@ ActionsMenu.propTypes = {
url: PropTypes.string, url: PropTypes.string,
type: PropTypes.string, type: PropTypes.string,
}).isRequired, }).isRequired,
isAdmin: PropTypes.bool,
}; };
/** /**
@@ -261,7 +267,7 @@ const SkeletonLayout = () => {
); );
}; };
const Monitors = () => { const Monitors = ({ isAdmin }) => {
const theme = useTheme(); const theme = useTheme();
const navigate = useNavigate(); const navigate = useNavigate();
const monitorState = useSelector((state) => state.uptimeMonitors); const monitorState = useSelector((state) => state.uptimeMonitors);
@@ -340,7 +346,7 @@ const Monitors = () => {
}, },
{ {
id: idx + 4, id: idx + 4,
data: <ActionsMenu monitor={monitor} />, data: <ActionsMenu monitor={monitor} isAdmin={isAdmin} />,
}, },
], ],
}; };
@@ -390,14 +396,16 @@ const Monitors = () => {
<Typography> <Typography>
It looks like you don’t have any monitors set up yet. It looks like you don’t have any monitors set up yet.
</Typography> </Typography>
<Button {isAdmin && (
level="primary" <Button
label="Create your first monitor" level="primary"
onClick={() => { label="Create your first monitor"
navigate("/monitors/create"); onClick={() => {
}} navigate("/monitors/create");
sx={{ mt: theme.gap.large }} }}
/> sx={{ mt: theme.gap.large }}
/>
)}
</Stack> </Stack>
) : ( ) : (
<> <>
@@ -437,4 +445,7 @@ const Monitors = () => {
); );
}; };
Monitors.propTypes = {
isAdmin: PropTypes.bool,
};
export default Monitors; export default Monitors;
+5 -1
View File
@@ -123,7 +123,7 @@ const SkeletonLayout = () => {
); );
}; };
const PageSpeed = () => { const PageSpeed = ({ isAdmin }) => {
const theme = useTheme(); const theme = useTheme();
const dispatch = useDispatch(); const dispatch = useDispatch();
const navigate = useNavigate(); const navigate = useNavigate();
@@ -178,10 +178,14 @@ const PageSpeed = () => {
"Give suggestions on how the page can be improved", "Give suggestions on how the page can be improved",
]} ]}
link="/pagespeed/create" link="/pagespeed/create"
isAdmin={isAdmin}
/> />
)} )}
</Box> </Box>
); );
}; };
PageSpeed.propTypes = {
isAdmin: PropTypes.bool,
};
export default PageSpeed; export default PageSpeed;