Skip to content

Commit

Permalink
replace useDispatch with useQuery and request in FacilityCard, Facili…
Browse files Browse the repository at this point in the history
…lyHome and FacilityUsers (#6575)

* replace useDispatch with useQuery and request in facilityCard, facilityHome and facilityUsers

* remove delete model and let useQuery handel delete response

* remove unused useStates variables in FacilityUsers

* replace useState variables with useQuery variables and also replaced useEffect with useQuery

* handle loading states

* Implemented a dedicated component for the triage table in FacilityHome

* Implemented a dedicated component for the doctor list in FacilityHome

* implemeted a dedicated component for bed capacity in facility home

* remove trailing slash

* Removed unused fire requests

* Fix deleting bed and doctor capacity bug and remove redundant code

* fix doctor capacity total count bug

---------

Co-authored-by: Rithvik Nishad <[email protected]>
  • Loading branch information
sriharsh05 and rithviknishad authored Nov 29, 2023
1 parent 5cde457 commit 4da37ba
Show file tree
Hide file tree
Showing 9 changed files with 529 additions and 528 deletions.
119 changes: 119 additions & 0 deletions src/Components/Facility/FacilityBedCapacity.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import { useState } from "react";
import { getBedTypes } from "../../Common/constants";
import routes from "../../Redux/api";
import { NonReadOnlyUsers } from "../../Utils/AuthorizeFor";
import useQuery from "../../Utils/request/useQuery";
import DialogModal from "../Common/Dialog";
import ButtonV2 from "../Common/components/ButtonV2";
import { BedCapacity } from "./BedCapacity";
import BedTypeCard from "./BedTypeCard";
import useConfig from "../../Common/hooks/useConfig";

export const FacilityBedCapacity = (props: any) => {
const [bedCapacityModalOpen, setBedCapacityModalOpen] = useState(false);
const config = useConfig();

const capacityQuery = useQuery(routes.getCapacity, {
pathParams: { facilityId: props.facilityId },
});

let capacityList: any = null;
if (!capacityQuery.data || !capacityQuery.data.results.length) {
capacityList = (
<h5 className="mt-4 flex w-full items-center justify-center rounded-lg bg-white p-4 text-xl font-bold text-gray-500 shadow">
No Bed Types Found
</h5>
);
} else {
const totalBedCount = capacityQuery.data.results.reduce(
(acc, x) => acc + (x.total_capacity ? x.total_capacity : 0),
0
);
const totalOccupiedBedCount = capacityQuery.data.results.reduce(
(acc, x) => acc + (x.current_capacity ? x.current_capacity : 0),
0
);

capacityList = (
<div className="mt-4 grid w-full gap-7 lg:grid-cols-2 xl:grid-cols-3 2xl:grid-cols-4">
<BedTypeCard
label="Total Beds"
used={totalOccupiedBedCount}
total={totalBedCount}
handleUpdate={() => {
return;
}}
/>
{getBedTypes(config).map((x) => {
const res = capacityQuery.data?.results.find((data) => {
return data.room_type === x.id;
});
if (
res &&
res.current_capacity !== undefined &&
res.total_capacity !== undefined
) {
const removeCurrentBedType = (bedTypeId: number | undefined) => {
if (capacityQuery.data !== undefined) {
capacityQuery.data.results.filter((i) => i.id !== bedTypeId);
capacityQuery.refetch();
}
};
return (
<BedTypeCard
facilityId={props.facilityId}
bedCapacityId={res.id}
key={`bed_${res.id}`}
room_type={res.room_type}
label={x.text}
used={res.current_capacity}
total={res.total_capacity}
lastUpdated={res.modified_date}
removeBedType={removeCurrentBedType}
handleUpdate={() => {
capacityQuery.refetch();
}}
/>
);
}
})}
</div>
);
}

return (
<div>
<div className="mt-5 rounded bg-white p-3 shadow-sm md:p-6">
<div className="justify-between md:flex md:border-b md:pb-2">
<div className="mb-2 text-xl font-semibold">Bed Capacity</div>
<ButtonV2
className="w-full md:w-auto"
onClick={() => setBedCapacityModalOpen(true)}
authorizeFor={NonReadOnlyUsers}
>
<i className="fas fa-bed mr-2 text-white" />
Add More Bed Types
</ButtonV2>
</div>
<div>{capacityList}</div>
</div>

{bedCapacityModalOpen && (
<DialogModal
show={bedCapacityModalOpen}
onClose={() => setBedCapacityModalOpen(false)}
title="Add Bed Capacity"
className="max-w-md md:min-w-[600px]"
>
<BedCapacity
facilityId={props.facilityId}
handleClose={() => setBedCapacityModalOpen(false)}
handleUpdate={async () => {
capacityQuery.refetch();
}}
/>
</DialogModal>
)}
</div>
);
};
21 changes: 10 additions & 11 deletions src/Components/Facility/FacilityCard.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { useState } from "react";
import { useDispatch } from "react-redux";
import { Link } from "raviger";
import { useTranslation } from "react-i18next";

import { sendNotificationMessages } from "../../Redux/actions";
import { FACILITY_FEATURE_TYPES } from "../../Common/constants";
import ButtonV2, { Cancel, Submit } from "../Common/components/ButtonV2";
import * as Notification from "../../Utils/Notifications.js";
Expand All @@ -14,26 +11,28 @@ import DialogModal from "../Common/Dialog";
import TextAreaFormField from "../Form/FormFields/TextAreaFormField";
import useConfig from "../../Common/hooks/useConfig";
import { classNames } from "../../Utils/utils";
import request from "../../Utils/request/request";
import routes from "../../Redux/api";

export const FacilityCard = (props: { facility: any; userType: any }) => {
const { facility, userType } = props;
const { kasp_string } = useConfig();

const { t } = useTranslation();
const dispatchAction: any = useDispatch();
const [notifyModalFor, setNotifyModalFor] = useState(undefined);
const [notifyMessage, setNotifyMessage] = useState("");
const [notifyError, setNotifyError] = useState("");

const handleNotifySubmit = async (id: any) => {
const data = {
facility: id,
message: notifyMessage,
};
if (data.message.trim().length >= 1) {
if (notifyMessage.trim().length >= 1) {
setNotifyError("");
const res = await dispatchAction(sendNotificationMessages(data));
if (res && res.status == 204) {
const { res } = await request(routes.sendNotificationMessages, {
body: {
facility: id,
message: notifyMessage,
},
});
if (res?.ok) {
Notification.Success({
msg: "Facility Notified",
});
Expand Down
121 changes: 121 additions & 0 deletions src/Components/Facility/FacilityDoctorList.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import { useState } from "react";
import { DOCTOR_SPECIALIZATION } from "../../Common/constants";
import { NonReadOnlyUsers } from "../../Utils/AuthorizeFor";
import ButtonV2 from "../Common/components/ButtonV2";
import DialogModal from "../Common/Dialog";
import { DoctorCapacity } from "./DoctorCapacity";
import useQuery from "../../Utils/request/useQuery";
import routes from "../../Redux/api";
import { DoctorModal } from "./models";
import DoctorsCountCard from "./DoctorsCountCard";
import { DoctorIcon } from "../TeleIcu/Icons/DoctorIcon";

export const FacilityDoctorList = (props: any) => {
const [doctorCapacityModalOpen, setDoctorCapacityModalOpen] = useState(false);
const [totalDoctors, setTotalDoctors] = useState(0);

const doctorQuery = useQuery(routes.listDoctor, {
pathParams: { facilityId: props.facilityId },
onResponse: ({ res, data }) => {
if (res?.ok && data) {
let totalCount = 0;
data.results.map((doctor: DoctorModal) => {
if (doctor.count) {
totalCount += doctor.count;
}
});
setTotalDoctors(totalCount);
}
},
});

let doctorList: any = null;
if (!doctorQuery.data || !doctorQuery.data.results.length) {
doctorList = (
<h5 className="flex w-full items-center justify-center rounded-lg bg-white p-4 text-xl font-bold text-gray-500 shadow">
No Doctors Found
</h5>
);
} else {
doctorList = (
<div className="mt-4 grid gap-6 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4">
{/* Total Doctors Count Card */}
<div className="w-full">
<div className="flex h-full flex-col rounded-sm border border-primary-500 bg-primary-100 shadow-sm">
<div className="flex flex-1 items-center justify-start gap-3 px-4 py-6">
<div className="rounded-full bg-primary-500 p-4">
<DoctorIcon className="h-5 w-5 fill-current text-white" />
</div>
<div>
<div className="text-sm font-medium text-[#808080]">
Total Doctors
</div>
<h2 className="mt-2 text-xl font-bold">{totalDoctors}</h2>
</div>
</div>
</div>
</div>

{doctorQuery.data.results.map((data: DoctorModal) => {
const removeCurrentDoctorData = (doctorId: number | undefined) => {
if (doctorQuery.data !== undefined) {
doctorQuery.data?.results.filter(
(i: DoctorModal) => i.id !== doctorId
);
doctorQuery.refetch();
}
};

return (
<DoctorsCountCard
facilityId={props.facilityId}
key={`bed_${data.id}`}
handleUpdate={async () => {
doctorQuery.refetch();
}}
{...data}
removeDoctor={removeCurrentDoctorData}
/>
);
})}
</div>
);
}

return (
<div>
<div className="mt-5 rounded bg-white p-3 shadow-sm md:p-6">
<div className="justify-between md:flex md:pb-2">
<div className="mb-2 text-xl font-bold">Doctors List</div>
<ButtonV2
className="w-full md:w-auto"
onClick={() => setDoctorCapacityModalOpen(true)}
disabled={doctorList.length === DOCTOR_SPECIALIZATION.length}
authorizeFor={NonReadOnlyUsers}
>
<i className="fas fa-user-md mr-2 text-white" />
Add Doctor Types
</ButtonV2>
</div>
<div className="mt-4">{doctorList}</div>
</div>

{doctorCapacityModalOpen && (
<DialogModal
show={doctorCapacityModalOpen}
onClose={() => setDoctorCapacityModalOpen(false)}
title="Add Doctor Capacity"
className="max-w-md md:min-w-[600px]"
>
<DoctorCapacity
facilityId={props.facilityId}
handleClose={() => setDoctorCapacityModalOpen(false)}
handleUpdate={async () => {
doctorQuery.refetch();
}}
/>
</DialogModal>
)}
</div>
);
};
Loading

0 comments on commit 4da37ba

Please sign in to comment.