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

Allow only admins to delete a location or a bed #7671

Merged
Show file tree
Hide file tree
Changes from all 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
22 changes: 16 additions & 6 deletions src/Components/Facility/BedManagement.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import { ReactElement } from "react";
import * as Notification from "../../Utils/Notifications.js";
import { LOCATION_BED_TYPES } from "../../Common/constants";
import BedDeleteDialog from "./BedDeleteDialog";
import { NonReadOnlyUsers } from "../../Utils/AuthorizeFor";
import AuthorizeFor, { NonReadOnlyUsers } from "../../Utils/AuthorizeFor";
import CareIcon from "../../CAREUI/icons/CareIcon";
import Page from "../Common/components/Page";
import request from "../../Utils/request/request";
import routes from "../../Redux/api";
import useQuery from "../../Utils/request/useQuery";
import useFilters from "../../Common/hooks/useFilters";
import useAuthUser from "../../Common/hooks/useAuthUser";
const Loading = lazy(() => import("../Common/Loading"));

interface BedManagementProps {
Expand Down Expand Up @@ -45,14 +46,18 @@ const BedRow = (props: BedRowProps) => {
show: boolean;
name: string;
}>({ show: false, name: "" });

const authUser = useAuthUser();
const handleDelete = (name: string, _id: string) => {
setBedData({
show: true,
name,
});
};

const allowedUser = ["DistrictAdmin", "StateAdmin"].includes(
authUser.user_type,
);

const handleDeleteConfirm = async () => {
const { res } = await request(routes.deleteFacilityBed, {
pathParams: { external_id: id },
Expand Down Expand Up @@ -126,14 +131,19 @@ const BedRow = (props: BedRowProps) => {
<ButtonV2
id="delete-bed-button"
onClick={() => handleDelete(name, id)}
authorizeFor={NonReadOnlyUsers}
authorizeFor={AuthorizeFor(["DistrictAdmin", "StateAdmin"])}
variant="danger"
border
ghost
className="w-full lg:w-auto"
disabled={isOccupied}
tooltip={isOccupied ? "Bed is occupied" : undefined}
tooltipClassName="w-full lg:w-auto"
tooltip={
!allowedUser
? "Contact your admin to delete the bed"
: isOccupied
? "Bed is occupied"
: undefined
}
tooltipClassName=" text-xs w-full lg:w-auto"
>
<CareIcon icon="l-trash-alt" className="text-lg" />
Delete
Expand Down
15 changes: 13 additions & 2 deletions src/Components/Facility/LocationManagement.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { lazy, useState } from "react";
import ButtonV2, { Cancel } from "../Common/components/ButtonV2";
import { NonReadOnlyUsers } from "../../Utils/AuthorizeFor";
import AuthorizeFor, { NonReadOnlyUsers } from "../../Utils/AuthorizeFor";
import CareIcon from "../../CAREUI/icons/CareIcon";
import Page from "../Common/components/Page";
import routes from "../../Redux/api";
Expand All @@ -12,6 +12,7 @@ import * as Notification from "../../Utils/Notifications.js";
import ConfirmDialog from "../Common/ConfirmDialog";
import DialogModal from "../Common/Dialog";
import Uptime from "../Common/Uptime";
import useAuthUser from "../../Common/hooks/useAuthUser";

const Loading = lazy(() => import("../Common/Loading"));

Expand All @@ -21,10 +22,12 @@ interface Props {

interface LocationProps extends LocationModel {
facilityId: string;
disabled: boolean;
setShowDeletePopup: (e: { open: boolean; name: string; id: string }) => void;
}

export default function LocationManagement({ facilityId }: Props) {
const authUser = useAuthUser();
const [showDeleteFailModal, setShowDeleteFailModal] = useState({
open: false,
id: "",
Expand Down Expand Up @@ -115,6 +118,11 @@ export default function LocationManagement({ facilityId }: Props) {
setShowDeletePopup={setShowDeletePopup}
facilityId={facilityId}
{...item}
disabled={
["DistrictAdmin", "StateAdmin"].includes(authUser.user_type)
? false
: true
}
/>
)}
</PaginatedList.Items>
Expand Down Expand Up @@ -213,6 +221,7 @@ const Location = ({
created_date,
modified_date,
id,
disabled,
setShowDeletePopup,
facilityId,
}: LocationProps) => (
Expand Down Expand Up @@ -286,14 +295,16 @@ const Location = ({
</div>
<div className="w-full md:w-1/2">
<ButtonV2
authorizeFor={AuthorizeFor(["DistrictAdmin", "StateAdmin"])}
id="delete-location-button"
variant="secondary"
border
className="w-full"
tooltip={disabled ? "Contact your admin to delete the location" : ""}
tooltipClassName=" text-xs w-full lg:w-auto"
onClick={() =>
setShowDeletePopup({ open: true, name: name ?? "", id: id ?? "" })
}
authorizeFor={NonReadOnlyUsers}
>
<CareIcon icon="l-trash" className="text-lg" />
Delete
Expand Down
Loading