From 086eb6d0e72c9662c12e2ba9baa45ba45c0258d6 Mon Sep 17 00:00:00 2001 From: rithviknishad Date: Wed, 22 May 2024 16:06:45 +0530 Subject: [PATCH 1/4] remove logically never used code --- .../Medicine/PrescriptionsTable.tsx | 155 +----------------- 1 file changed, 1 insertion(+), 154 deletions(-) diff --git a/src/Components/Medicine/PrescriptionsTable.tsx b/src/Components/Medicine/PrescriptionsTable.tsx index d61275c42c1..f842ccc654b 100644 --- a/src/Components/Medicine/PrescriptionsTable.tsx +++ b/src/Components/Medicine/PrescriptionsTable.tsx @@ -3,12 +3,8 @@ import ResponsiveMedicineTable from "./ResponsiveMedicineTables"; import { formatDateTime } from "../../Utils/utils"; import { Prescription } from "./models"; import CareIcon from "../../CAREUI/icons/CareIcon"; -import ButtonV2, { Cancel, Submit } from "../Common/components/ButtonV2"; -import SlideOver from "../../CAREUI/interactive/SlideOver"; -import MedicineAdministration from "./MedicineAdministration"; -import DiscontinuePrescription from "./DiscontinuePrescription"; +import { Cancel } from "../Common/components/ButtonV2"; import RecordMeta from "../../CAREUI/display/RecordMeta"; -import AdministerMedicine from "./AdministerMedicine"; import DialogModal from "../Common/Dialog"; import PrescriptionDetailCard from "./PrescriptionDetailCard"; import { useTranslation } from "react-i18next"; @@ -19,21 +15,14 @@ import MedicineRoutes from "./routes"; interface Props { is_prn?: boolean; prescription_type?: Prescription["prescription_type"]; - onChange?: () => void; - readonly?: boolean; } export default function PrescriptionsTable({ is_prn = false, prescription_type = "REGULAR", - onChange, - readonly, }: Props) { const consultation = useSlug("consultation"); const { t } = useTranslation(); - const [showBulkAdminister, setShowBulkAdminister] = useState(false); - const [showDiscontinueFor, setShowDiscontinueFor] = useState(); - const [showAdministerFor, setShowAdministerFor] = useState(); const [detailedViewFor, setDetailedViewFor] = useState(); const { data } = useQuery(MedicineRoutes.listPrescriptions, { @@ -57,42 +46,6 @@ export default function PrescriptionsTable({ return (
- {data?.results && ( - - { - setShowBulkAdminister(false); - onChange?.(); - }} - /> - - )} - {showDiscontinueFor && ( - { - setShowDiscontinueFor(undefined); - if (success) onChange?.(); - }} - key={showDiscontinueFor.id} - /> - )} - {showAdministerFor && ( - { - setShowAdministerFor(undefined); - if (success) onChange?.(); - }} - key={showAdministerFor.id} - /> - )} {detailedViewFor && ( setDetailedViewFor(undefined)} @@ -111,27 +64,6 @@ export default function PrescriptionsTable({ onClick={() => setDetailedViewFor(undefined)} label={t("close")} /> - setShowDiscontinueFor(detailedViewFor)} - > - - {t("discontinue")} - - setShowAdministerFor(detailedViewFor)} - > - - {t("administer")} -
@@ -148,34 +80,6 @@ export default function PrescriptionsTable({ - {prescription_type === "REGULAR" && ( -
- - - {t("edit_prescriptions")} - {t("edit")} - - setShowBulkAdminister(true)} - className="w-full lg:w-auto" - > - - - {t("administer_medicines")} - - {t("administer")} - -
- )}
@@ -209,63 +113,6 @@ export default function PrescriptionsTable({ } objectKeys={Object.values(tkeys)} fieldsToDisplay={[2, 3]} - actions={ - !readonly - ? (med: Prescription) => { - if (med.prescription_type === "DISCHARGE") { - return ( -
- - {t("discharge_prescription")} - -
- ); - } - - if (med.discontinued) { - return ( -
- - {t("discontinued")} -
- ); - } - - return ( -
- { - e.stopPropagation(); - setShowAdministerFor(med); - }} - > - - {t("administer")} - - { - e.stopPropagation(); - setShowDiscontinueFor(med); - }} - > - - {t("discontinue")} - -
- ); - } - : undefined - } /> {data?.results.length === 0 && (
From bc0fb3db899403f7b37f8c923caf0d23eb1778d1 Mon Sep 17 00:00:00 2001 From: rithviknishad Date: Wed, 22 May 2024 16:08:44 +0530 Subject: [PATCH 2/4] Adds reusable component: `AuthorizedForConsultationRelatedActions` to hide buttons based on home facility access --- src/CAREUI/misc/AuthorizedChild.tsx | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/CAREUI/misc/AuthorizedChild.tsx b/src/CAREUI/misc/AuthorizedChild.tsx index ce9ec69d546..935f0c51f3c 100644 --- a/src/CAREUI/misc/AuthorizedChild.tsx +++ b/src/CAREUI/misc/AuthorizedChild.tsx @@ -1,4 +1,7 @@ +import { ReactNode } from "react"; +import useAuthUser from "../../Common/hooks/useAuthUser"; import { useIsAuthorized } from "../../Common/hooks/useIsAuthorized"; +import useSlug from "../../Common/hooks/useSlug"; import { AuthorizedForCB } from "../../Utils/AuthorizeFor"; interface Props { @@ -12,3 +15,20 @@ const AuthorizedChild = (props: Props) => { }; export default AuthorizedChild; + +export const AuthorizedForConsultationRelatedActions = (props: { + children: ReactNode; +}) => { + const me = useAuthUser(); + const facilityId = useSlug("facility"); + + if ( + me.home_facility_object?.id === facilityId || + me.user_type === "DistrictAdmin" || + me.user_type === "StateAdmin" + ) { + return props.children; + } + + return null; +}; From 55a70d464cc9b18fd130cafae9d27b27a96904a1 Mon Sep 17 00:00:00 2001 From: rithviknishad Date: Wed, 22 May 2024 16:09:16 +0530 Subject: [PATCH 3/4] Disallow unauthorized view for button related to prescriptions --- .../ConsultationUpdatesTab.tsx | 2 - .../AdministrationTableRow.tsx | 109 +++++++++--------- .../MedicineAdministrationSheet/index.tsx | 5 +- .../Medicine/PrescriptionBuilder.tsx | 36 +++--- .../Medicine/PrescriptionDetailCard.tsx | 57 ++++----- 5 files changed, 112 insertions(+), 97 deletions(-) diff --git a/src/Components/Facility/ConsultationDetails/ConsultationUpdatesTab.tsx b/src/Components/Facility/ConsultationDetails/ConsultationUpdatesTab.tsx index e9e658cee4f..a7e52257e28 100644 --- a/src/Components/Facility/ConsultationDetails/ConsultationUpdatesTab.tsx +++ b/src/Components/Facility/ConsultationDetails/ConsultationUpdatesTab.tsx @@ -249,7 +249,6 @@ export const ConsultationUpdatesTab = (props: ConsultationTabProps) => {
@@ -257,7 +256,6 @@ export const ConsultationUpdatesTab = (props: ConsultationTabProps) => {
diff --git a/src/Components/Medicine/MedicineAdministrationSheet/AdministrationTableRow.tsx b/src/Components/Medicine/MedicineAdministrationSheet/AdministrationTableRow.tsx index 4fc7a97f56b..13f5191f429 100644 --- a/src/Components/Medicine/MedicineAdministrationSheet/AdministrationTableRow.tsx +++ b/src/Components/Medicine/MedicineAdministrationSheet/AdministrationTableRow.tsx @@ -14,6 +14,7 @@ import CareIcon from "../../../CAREUI/icons/CareIcon"; import EditPrescriptionForm from "../EditPrescriptionForm"; import AdministrationEventSeperator from "./AdministrationEventSeperator"; import AdministrationEventCell from "./AdministrationEventCell"; +import { AuthorizedForConsultationRelatedActions } from "../../../CAREUI/misc/AuthorizedChild"; interface Props { prescription: Prescription; @@ -93,46 +94,48 @@ export default function MedicineAdministrationTableRow({ onClick={() => setShowDetails(false)} label={t("close")} /> - {!props.readonly && ( - <> - setShowDiscontinue(true)} - > - - {t("discontinue")} - - { - setShowDetails(false); - setShowEdit(true); - }} - > - - {t("edit")} - - setShowAdminister(true)} - > - - {t("administer")} - - - )} + + {!props.readonly && ( + <> + setShowDiscontinue(true)} + > + + {t("discontinue")} + + { + setShowDetails(false); + setShowEdit(true); + }} + > + + {t("edit")} + + setShowAdminister(true)} + > + + {t("administer")} + + + )} +
@@ -252,18 +255,20 @@ export default function MedicineAdministrationTableRow({ {/* Action Buttons */} - {!props.readonly && ( - setShowAdminister(true)} - > - {t("administer")} - - )} + + {!props.readonly && ( + setShowAdminister(true)} + > + {t("administer")} + + )} + diff --git a/src/Components/Medicine/MedicineAdministrationSheet/index.tsx b/src/Components/Medicine/MedicineAdministrationSheet/index.tsx index c1917cef41d..59d3976d763 100644 --- a/src/Components/Medicine/MedicineAdministrationSheet/index.tsx +++ b/src/Components/Medicine/MedicineAdministrationSheet/index.tsx @@ -13,6 +13,7 @@ import useRangePagination from "../../../Common/hooks/useRangePagination"; import MedicineAdministrationTable from "./AdministrationTable"; import Loading from "../../Common/Loading"; import ScrollOverlay from "../../../CAREUI/interactive/ScrollOverlay"; +import { AuthorizedForConsultationRelatedActions } from "../../../CAREUI/misc/AuthorizedChild"; interface Props { readonly?: boolean; @@ -89,7 +90,7 @@ const MedicineAdministrationSheet = ({ readonly, is_prn }: Props) => { options={ !readonly && !!data?.results && ( - <> + { prescriptions={data.results} onDone={() => refetch()} /> - + ) } /> diff --git a/src/Components/Medicine/PrescriptionBuilder.tsx b/src/Components/Medicine/PrescriptionBuilder.tsx index d5388dfa44a..972a74159d2 100644 --- a/src/Components/Medicine/PrescriptionBuilder.tsx +++ b/src/Components/Medicine/PrescriptionBuilder.tsx @@ -11,6 +11,7 @@ import { useTranslation } from "react-i18next"; import useQuery from "../../Utils/request/useQuery"; import MedicineRoutes from "./routes"; import useSlug from "../../Common/hooks/useSlug"; +import { AuthorizedForConsultationRelatedActions } from "../../CAREUI/misc/AuthorizedChild"; interface Props { prescription_type?: Prescription["prescription_type"]; @@ -75,20 +76,27 @@ export default function PrescriptionBuilder({ /> ))}
- setShowCreate(true)} - variant="secondary" - className="mt-4 w-full bg-gray-200 text-gray-700 hover:bg-gray-300 hover:text-gray-900 focus:bg-gray-100 focus:text-gray-900" - disabled={disabled} - > -
- - - {t(is_prn ? "add_prn_prescription" : "add_prescription_medication")} - -
-
+ + setShowCreate(true)} + variant="secondary" + className="mt-4 w-full bg-gray-200 text-gray-700 hover:bg-gray-300 hover:text-gray-900 focus:bg-gray-100 focus:text-gray-900" + disabled={disabled} + > +
+ + + {t( + is_prn ? "add_prn_prescription" : "add_prescription_medication", + )} + +
+
+
{showCreate && ( setShowCreate(false)} diff --git a/src/Components/Medicine/PrescriptionDetailCard.tsx b/src/Components/Medicine/PrescriptionDetailCard.tsx index 166408e6292..630ab324662 100644 --- a/src/Components/Medicine/PrescriptionDetailCard.tsx +++ b/src/Components/Medicine/PrescriptionDetailCard.tsx @@ -5,6 +5,7 @@ import ReadMore from "../Common/components/Readmore"; import ButtonV2 from "../Common/components/ButtonV2"; import { useTranslation } from "react-i18next"; import RecordMeta from "../../CAREUI/display/RecordMeta"; +import { AuthorizedForConsultationRelatedActions } from "../../CAREUI/misc/AuthorizedChild"; export default function PrescriptionDetailCard({ prescription, @@ -56,33 +57,35 @@ export default function PrescriptionDetailCard({ {!props.readonly && prescription.prescription_type !== "DISCHARGE" && ( -
- - - {t("administer")} - - - - {t("discontinue")} - -
+ +
+ + + {t("administer")} + + + + {t("discontinue")} + +
+
)} From b0ec95f2bd73f7e87b02f5f963037a4f3680b647 Mon Sep 17 00:00:00 2001 From: rithviknishad Date: Wed, 22 May 2024 16:17:30 +0530 Subject: [PATCH 4/4] wrap archive action with authzn. wrapper --- .../Medicine/PrescrpitionTimeline.tsx | 21 +++++++++++-------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/Components/Medicine/PrescrpitionTimeline.tsx b/src/Components/Medicine/PrescrpitionTimeline.tsx index 9422d349b3a..f26957f63c6 100644 --- a/src/Components/Medicine/PrescrpitionTimeline.tsx +++ b/src/Components/Medicine/PrescrpitionTimeline.tsx @@ -15,6 +15,7 @@ import ConfirmDialog from "../Common/ConfirmDialog"; import request from "../../Utils/request/request"; import RecordMeta from "../../CAREUI/display/RecordMeta"; import CareIcon from "../../CAREUI/icons/CareIcon"; +import { AuthorizedForConsultationRelatedActions } from "../../CAREUI/misc/AuthorizedChild"; interface MedicineAdministeredEvent extends TimelineEvent<"administered"> { administration: MedicineAdministrationRecord; @@ -131,15 +132,17 @@ const MedicineAdministeredNode = ({ actions={ !event.cancelled && !hideArchive && ( - setShowArchiveConfirmation(true)} - > - Archive - + + setShowArchiveConfirmation(true)} + > + Archive + + ) } isLast={isLastNode}