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

Hides prescription related action buttons for non-home facility users + Adds reusable component to hide child component if authzn. requirements not met #7880

Merged
merged 4 commits into from
May 22, 2024
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
20 changes: 20 additions & 0 deletions src/CAREUI/misc/AuthorizedChild.tsx
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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;
};
Original file line number Diff line number Diff line change
Expand Up @@ -249,15 +249,13 @@ export const ConsultationUpdatesTab = (props: ConsultationTabProps) => {
<div className="overflow-x-auto overflow-y-hidden">
<PrescriptionsTable
is_prn={false}
readonly
prescription_type="DISCHARGE"
/>
</div>
<hr className="my-2 border border-gray-300"></hr>
<div className="overflow-x-auto overflow-y-hidden">
<PrescriptionsTable
is_prn
readonly
prescription_type="DISCHARGE"
/>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -93,46 +94,48 @@ export default function MedicineAdministrationTableRow({
onClick={() => setShowDetails(false)}
label={t("close")}
/>
{!props.readonly && (
<>
<Submit
disabled={
prescription.discontinued ||
prescription.prescription_type === "DISCHARGE"
}
variant="danger"
onClick={() => setShowDiscontinue(true)}
>
<CareIcon icon="l-ban" className="text-lg" />
{t("discontinue")}
</Submit>
<Submit
disabled={
prescription.discontinued ||
prescription.prescription_type === "DISCHARGE"
}
variant="secondary"
border
onClick={() => {
setShowDetails(false);
setShowEdit(true);
}}
>
<CareIcon icon="l-pen" className="text-lg" />
{t("edit")}
</Submit>
<Submit
disabled={
prescription.discontinued ||
prescription.prescription_type === "DISCHARGE"
}
onClick={() => setShowAdminister(true)}
>
<CareIcon icon="l-syringe" className="text-lg" />
{t("administer")}
</Submit>
</>
)}
<AuthorizedForConsultationRelatedActions>
{!props.readonly && (
<>
<Submit
disabled={
prescription.discontinued ||
prescription.prescription_type === "DISCHARGE"
}
variant="danger"
onClick={() => setShowDiscontinue(true)}
>
<CareIcon icon="l-ban" className="text-lg" />
{t("discontinue")}
</Submit>
<Submit
disabled={
prescription.discontinued ||
prescription.prescription_type === "DISCHARGE"
}
variant="secondary"
border
onClick={() => {
setShowDetails(false);
setShowEdit(true);
}}
>
<CareIcon icon="l-pen" className="text-lg" />
{t("edit")}
</Submit>
<Submit
disabled={
prescription.discontinued ||
prescription.prescription_type === "DISCHARGE"
}
onClick={() => setShowAdminister(true)}
>
<CareIcon icon="l-syringe" className="text-lg" />
{t("administer")}
</Submit>
</>
)}
</AuthorizedForConsultationRelatedActions>
</div>
</div>
</DialogModal>
Expand Down Expand Up @@ -252,18 +255,20 @@ export default function MedicineAdministrationTableRow({

{/* Action Buttons */}
<td className="space-x-1 pr-2 text-right">
{!props.readonly && (
<ButtonV2
type="button"
size="small"
disabled={prescription.discontinued}
ghost
border
onClick={() => setShowAdminister(true)}
>
{t("administer")}
</ButtonV2>
)}
<AuthorizedForConsultationRelatedActions>
{!props.readonly && (
<ButtonV2
type="button"
size="small"
disabled={prescription.discontinued}
ghost
border
onClick={() => setShowAdminister(true)}
>
{t("administer")}
</ButtonV2>
)}
</AuthorizedForConsultationRelatedActions>
</td>
</tr>
</>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -89,7 +90,7 @@ const MedicineAdministrationSheet = ({ readonly, is_prn }: Props) => {
options={
!readonly &&
!!data?.results && (
<>
<AuthorizedForConsultationRelatedActions>
<ButtonV2
id="edit-prescription"
variant="secondary"
Expand All @@ -107,7 +108,7 @@ const MedicineAdministrationSheet = ({ readonly, is_prn }: Props) => {
prescriptions={data.results}
onDone={() => refetch()}
/>
</>
</AuthorizedForConsultationRelatedActions>
)
}
/>
Expand Down
36 changes: 22 additions & 14 deletions src/Components/Medicine/PrescriptionBuilder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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"];
Expand Down Expand Up @@ -75,20 +76,27 @@ export default function PrescriptionBuilder({
/>
))}
</div>
<ButtonV2
type="button"
onClick={() => 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}
>
<div className="flex w-full justify-start gap-2" id="add-prescription">
<CareIcon icon="l-plus" className="text-lg" />
<span className="font-bold">
{t(is_prn ? "add_prn_prescription" : "add_prescription_medication")}
</span>
</div>
</ButtonV2>
<AuthorizedForConsultationRelatedActions>
<ButtonV2
type="button"
onClick={() => 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}
>
<div
className="flex w-full justify-start gap-2"
id="add-prescription"
>
<CareIcon icon="l-plus" className="text-lg" />
<span className="font-bold">
{t(
is_prn ? "add_prn_prescription" : "add_prescription_medication",
)}
</span>
</div>
</ButtonV2>
</AuthorizedForConsultationRelatedActions>
{showCreate && (
<DialogModal
onClose={() => setShowCreate(false)}
Expand Down
57 changes: 30 additions & 27 deletions src/Components/Medicine/PrescriptionDetailCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -56,33 +57,35 @@ export default function PrescriptionDetailCard({

{!props.readonly &&
prescription.prescription_type !== "DISCHARGE" && (
<div className="flex flex-col-reverse items-end gap-2 sm:flex-row">
<ButtonV2
id="administer-medicine"
disabled={prescription.discontinued}
onClick={props.onAdministerClick}
type="button"
size="small"
variant="secondary"
ghost
border
>
<CareIcon icon="l-syringe" className="text-base" />
{t("administer")}
</ButtonV2>
<ButtonV2
disabled={prescription.discontinued}
type="button"
size="small"
variant="danger"
ghost
border
onClick={props.onDiscontinueClick}
>
<CareIcon icon="l-ban" className="text-base" />
{t("discontinue")}
</ButtonV2>
</div>
<AuthorizedForConsultationRelatedActions>
<div className="flex flex-col-reverse items-end gap-2 sm:flex-row">
<ButtonV2
id="administer-medicine"
disabled={prescription.discontinued}
onClick={props.onAdministerClick}
type="button"
size="small"
variant="secondary"
ghost
border
>
<CareIcon icon="l-syringe" className="text-base" />
{t("administer")}
</ButtonV2>
<ButtonV2
disabled={prescription.discontinued}
type="button"
size="small"
variant="danger"
ghost
border
onClick={props.onDiscontinueClick}
>
<CareIcon icon="l-ban" className="text-base" />
{t("discontinue")}
</ButtonV2>
</div>
</AuthorizedForConsultationRelatedActions>
)}
</div>
</div>
Expand Down
Loading
Loading