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 2 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
21 changes: 16 additions & 5 deletions src/Components/Facility/BedManagement.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
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 @@
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) ||
authUser.is_superuser;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need not do super user checks explicitly as all super users are state admins.


const handleDeleteConfirm = async () => {
const { res } = await request(routes.deleteFacilityBed, {
pathParams: { external_id: id },
Expand Down Expand Up @@ -93,7 +98,7 @@
{LOCATION_BED_TYPES.find((item) => item.id === bedType) && (
<p className="mb-1 inline-flex w-fit items-center rounded-md bg-blue-100 px-2.5 py-0.5 text-sm font-medium capitalize leading-5 text-blue-800">
{LOCATION_BED_TYPES.find(
(item) => item.id === bedType,
(item) => item.id === bedType

Check failure on line 101 in src/Components/Facility/BedManagement.tsx

View workflow job for this annotation

GitHub Actions / lint

Insert `,`
)?.name?.slice(0, 25) + (bedType.length > 25 ? "..." : "")}
</p>
)}
Expand Down Expand Up @@ -131,9 +136,15 @@
border
ghost
className="w-full lg:w-auto"
disabled={isOccupied}
tooltip={isOccupied ? "Bed is occupied" : undefined}
tooltipClassName="w-full lg:w-auto"
disabled={!allowedUser || isOccupied}
Copy link
Member

@rithviknishad rithviknishad May 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of doing that, we could make use of the authorizeFor prop present in the button component

tooltip={
!allowedUser
? "Contact your admin to delete the bed"
: isOccupied
? "Bed is occupied"

Check failure on line 144 in src/Components/Facility/BedManagement.tsx

View workflow job for this annotation

GitHub Actions / lint

Insert `··`
: undefined

Check failure on line 145 in src/Components/Facility/BedManagement.tsx

View workflow job for this annotation

GitHub Actions / lint

Insert `··`
}
tooltipClassName=" text-xs w-full lg:w-auto"
>
<CareIcon icon="l-trash-alt" className="text-lg" />
Delete
Expand Down
14 changes: 14 additions & 0 deletions src/Components/Facility/LocationManagement.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
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 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,13 @@
setShowDeletePopup={setShowDeletePopup}
facilityId={facilityId}
{...item}
disabled={
["DistrictAdmin", "StateAdmin"].includes(
authUser.user_type

Check failure on line 123 in src/Components/Facility/LocationManagement.tsx

View workflow job for this annotation

GitHub Actions / lint

Insert `,`
) || authUser.is_superuser
? false
: true
}
/>
)}
</PaginatedList.Items>
Expand Down Expand Up @@ -213,6 +223,7 @@
created_date,
modified_date,
id,
disabled,
setShowDeletePopup,
facilityId,
}: LocationProps) => (
Expand Down Expand Up @@ -290,6 +301,9 @@
variant="secondary"
border
className="w-full"
tooltip={disabled ? "Contact your admin to delete the location" : ""}
disabled={disabled ? true : false}
tooltipClassName=" text-xs w-full lg:w-auto"
onClick={() =>
setShowDeletePopup({ open: true, name: name ?? "", id: id ?? "" })
}
Expand Down
Loading