Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display live monitoring button to only State Admin, District Admin and Doctor. #7029

Merged
merged 5 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 userRoles = ["StateAdmin", "DistrictAdmin", "Doctor"];
AshrafMd-1 marked this conversation as resolved.
Show resolved Hide resolved

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>
{userRoles.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 />;
Ashesh3 marked this conversation as resolved.
Show resolved Hide resolved
}
};
Loading