Skip to content

Commit

Permalink
Display live monitoring button to only State Admin, District Admin an…
Browse files Browse the repository at this point in the history
…d Doctor. (#7029)

* display live monitoring to specified users

* add authorize route component

* Update src/Utils/AuthorizeFor.tsx

* change to a more descriptive variable name

---------

Co-authored-by: Rithvik Nishad <[email protected]>
  • Loading branch information
AshrafMd-1 and rithviknishad authored Jan 17, 2024
1 parent 51b02c2 commit b9bd889
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
18 changes: 12 additions & 6 deletions src/Components/Facility/FacilityHome.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -475,15 +475,21 @@ export const FacilityHome = (props: any) => {
const LiveMonitoringButton = () => {
const facilityId = useSlug("facility");
const [location, setLocation] = useState<string>();
const authUser = useAuthUser();

const permittedUserTypes = ["StateAdmin", "DistrictAdmin", "Doctor"];

return (
<Popover className="relative">
<Popover.Button className="mt-2 w-full">
<ButtonV2 variant="primary" ghost border className="w-full">
<CareIcon icon="l-video" className="text-lg" />
<span>Live Monitoring</span>
</ButtonV2>
</Popover.Button>
{permittedUserTypes.includes(authUser.user_type) && (
<Popover.Button className="mt-2 w-full">
<ButtonV2 variant="primary" ghost border className="w-full">
<CareIcon icon="l-video" className="text-lg" />
<span>Live Monitoring</span>
</ButtonV2>
</Popover.Button>
)}

<Transition
as={Fragment}
enter="transition ease-out duration-200"
Expand Down
5 changes: 4 additions & 1 deletion src/Routers/routes/FacilityLocationRoutes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { AddLocationForm } from "../../Components/Facility/AddLocationForm";
import { BedManagement } from "../../Components/Facility/BedManagement";
import LocationManagement from "../../Components/Facility/LocationManagement";
import CentralLiveMonitoring from "../../Components/CameraFeed/CentralLiveMonitoring";
import { AuthorizeUserRoute } from "../../Utils/AuthorizeFor";

export default {
"/facility/:facilityId/location": ({ facilityId }: any) => (
Expand Down Expand Up @@ -37,6 +38,8 @@ export default {
<AddBedForm facilityId={facilityId} locationId={locationId} bedId={bedId} />
),
"/facility/:facilityId/live-monitoring": (props: any) => (
<CentralLiveMonitoring {...props} />
<AuthorizeUserRoute userTypes={["StateAdmin", "DistrictAdmin", "Doctor"]}>
<CentralLiveMonitoring {...props} />
</AuthorizeUserRoute>
),
};
21 changes: 21 additions & 0 deletions src/Utils/AuthorizeFor.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import { UserRole } from "../Common/constants";
import React from "react";
import useAuthUser from "../Common/hooks/useAuthUser";
import Error404 from "../Components/ErrorPages/404";

export type AuthorizedForCB = (userType: UserRole) => boolean;

interface AuthorizeUserRouteProps {
userTypes: UserRole[];
children: React.ReactNode;
}

export type AuthorizedElementProps = {
/**
* Restrict access of this button to specific roles.
Expand All @@ -26,3 +35,15 @@ export const Anyone = () => true;
export default function (userTypes: UserRole[]) {
return (userType: UserRole) => userTypes.includes(userType);
}

export const AuthorizeUserRoute: React.FC<AuthorizeUserRouteProps> = ({
userTypes,
children,
}) => {
const authUser = useAuthUser();
if (userTypes.includes(authUser.user_type)) {
return <>{children}</>;
} else {
return <Error404 />;
}
};

0 comments on commit b9bd889

Please sign in to comment.