From 6999dabfd232bcaac612742306ac6d37914b2e68 Mon Sep 17 00:00:00 2001 From: Abhiuday Date: Sat, 28 Oct 2023 18:45:31 +0530 Subject: [PATCH] fix(location): added duty staff section --- src/Components/Assets/AssetTypes.tsx | 6 + src/Components/Facility/AddLocationForm.tsx | 151 +++++++++++++++--- .../Facility/LocationManagement.tsx | 105 +++++++----- src/Components/Facility/models.tsx | 8 +- src/Locale/en/Location.json | 6 + src/Locale/en/index.js | 16 +- src/Redux/actions.tsx | 10 ++ src/Redux/api.tsx | 14 +- 8 files changed, 233 insertions(+), 83 deletions(-) create mode 100644 src/Locale/en/Location.json diff --git a/src/Components/Assets/AssetTypes.tsx b/src/Components/Assets/AssetTypes.tsx index 8b96b6beeb7..39e8b35a6b0 100644 --- a/src/Components/Assets/AssetTypes.tsx +++ b/src/Components/Assets/AssetTypes.tsx @@ -1,6 +1,7 @@ import { BedModel } from "../Facility/models"; import { PerformedByModel } from "../HCX/misc"; import { PatientModel } from "../Patient/models"; +import { UserAssignedModel } from "../Users/models"; export interface AssetLocationObject { id: string; @@ -14,6 +15,11 @@ export interface AssetLocationObject { }; } +export interface AssetLocationDutyStaffObject { + duty_staff_objects: UserAssignedModel[]; + duty_staff: number; +} + export enum AssetType { NONE = "NONE", INTERNAL = "INTERNAL", diff --git a/src/Components/Facility/AddLocationForm.tsx b/src/Components/Facility/AddLocationForm.tsx index e71b68cc95c..44dd5024d1b 100644 --- a/src/Components/Facility/AddLocationForm.tsx +++ b/src/Components/Facility/AddLocationForm.tsx @@ -1,17 +1,21 @@ -import { useState, useEffect, lazy, SyntheticEvent } from "react"; +import { navigate } from "raviger"; +import { SyntheticEvent, lazy, useEffect, useState } from "react"; import { useDispatch } from "react-redux"; import { createFacilityAssetLocation, + createFacilityAssetLocationDutyStaff, getAnyFacility, getFacilityAssetLocation, + getFacilityUsers, updateFacilityAssetLocation, } from "../../Redux/actions"; import * as Notification from "../../Utils/Notifications.js"; -import { navigate } from "raviger"; -import { Submit, Cancel } from "../Common/components/ButtonV2"; -import TextFormField from "../Form/FormFields/TextFormField"; -import TextAreaFormField from "../Form/FormFields/TextAreaFormField"; +import { Cancel, Submit } from "../Common/components/ButtonV2"; import Page from "../Common/components/Page"; +import { MultiSelectFormField } from "../Form/FormFields/SelectFormField"; +import TextAreaFormField from "../Form/FormFields/TextAreaFormField"; +import TextFormField from "../Form/FormFields/TextFormField"; +import { UserAssignedModel } from "../Users/models"; const Loading = lazy(() => import("../Common/Loading")); @@ -29,10 +33,20 @@ export const AddLocationForm = (props: LocationFormProps) => { const [description, setDescription] = useState(""); const [facilityName, setFacilityName] = useState(""); const [locationName, setLocationName] = useState(""); - const [errors, setErrors] = useState({ + const [doctorList, setDoctorList] = useState([]); + const [staffList, setStaffList] = useState([]); + const [doctors, setDoctors] = useState([]); + const [staff, setStaff] = useState([]); + const [errors, setErrors] = useState<{ + name: string; + description: string; + middlewareAddress: string; + duty_staff: string; + }>({ name: "", description: "", middlewareAddress: "", + duty_staff: "", }); const headerText = !locationId ? "Add Location" : "Update Location"; const buttonText = !locationId ? "Add Location" : "Update Location"; @@ -41,9 +55,22 @@ export const AddLocationForm = (props: LocationFormProps) => { async function fetchFacilityName() { setIsLoading(true); if (facilityId) { - const res = await dispatchAction(getAnyFacility(facilityId)); - - setFacilityName(res?.data?.name || ""); + const facility = await dispatchAction(getAnyFacility(facilityId)); + const doctor = await dispatchAction( + getFacilityUsers(facilityId, { + user_type: "Doctor", + home_facility: facilityId, + }) + ); + const staff = await dispatchAction( + getFacilityUsers(facilityId, { + user_type: "Staff", + home_facility: facilityId, + }) + ); + setFacilityName(facility?.data?.name || ""); + setDoctorList(doctor?.data?.results || []); + setStaffList(staff?.data?.results || []); } if (locationId) { const res = await dispatchAction( @@ -54,6 +81,16 @@ export const AddLocationForm = (props: LocationFormProps) => { setLocationName(res?.data?.name || ""); setDescription(res?.data?.description || ""); setMiddlewareAddress(res?.data?.middleware_address || ""); + setDoctors( + res?.data?.duty_staff_objects + .filter((doc: UserAssignedModel) => doc.user_type === "Doctor") + .map((doc: UserAssignedModel) => doc.id) || [] + ); + setStaff( + res?.data?.duty_staff_objects + .filter((s: UserAssignedModel) => s.user_type === "Staff") + .map((s: UserAssignedModel) => s.id) || [] + ); } setIsLoading(false); } @@ -66,6 +103,7 @@ export const AddLocationForm = (props: LocationFormProps) => { name: "", description: "", middlewareAddress: "", + duty_staff: "", }; if (name.trim().length === 0) { @@ -108,24 +146,47 @@ export const AddLocationForm = (props: LocationFormProps) => { setIsLoading(false); if (res) { if (res.status === 201 || res.status === 200) { - const notificationMessage = locationId - ? "Location updated successfully" - : "Location created successfully"; - - navigate(`/facility/${facilityId}/location`, { - replace: true, - }); - Notification.Success({ - msg: notificationMessage, - }); - } else if (res.status === 400) { - Object.keys(res.data).forEach((key) => { - setErrors((prevState: any) => ({ - ...prevState, - [key]: res.data[key], - })); - }); + const duty_res = await dispatchAction( + createFacilityAssetLocationDutyStaff( + { + duty_staff: [...doctors, ...staff], + }, + facilityId, + res.data.id + ) + ); + if (duty_res.status === 200) { + Notification.Success({ + msg: "Duty Staff assigned successfully", + }); + + const notificationMessage = locationId + ? "Location updated successfully" + : "Location created successfully"; + + Notification.Success({ + msg: notificationMessage, + }); + + navigate(`/facility/${facilityId}/location`, { + replace: true, + }); + } else if (duty_res.status === 400) { + Object.keys(duty_res.data).forEach((key) => { + setErrors((prevState) => ({ + ...prevState, + [key]: duty_res.data[key], + })); + }); + } } + } else if (res.status === 400) { + Object.keys(res.data).forEach((key) => { + setErrors((prevState) => ({ + ...prevState, + [key]: res.data[key], + })); + }); } }; @@ -151,6 +212,12 @@ export const AddLocationForm = (props: LocationFormProps) => {
+
+ +
+
{ error={errors.middlewareAddress} />
+
+ +
+
+
+ setDoctors(e.value)} + options={doctorList} + value={doctors} + optionLabel={(option: any) => + `${option.first_name} ${option.last_name}` + } + optionValue={(option: any) => option.id} + /> +
+
+ setStaff(e.value)} + options={staffList} + value={staff} + optionLabel={(option: any) => + `${option.first_name} ${option.last_name}` + } + optionValue={(option: any) => option.id} + /> +
import("../Common/Loading")); @@ -14,6 +15,7 @@ interface Props { } export default function LocationManagement({ facilityId }: Props) { + const { t } = useTranslation(); return ( {() => ( - Add New Location + {t("add_new_location")}
- No locations available + {t("no_locations_available")} @@ -70,42 +72,61 @@ const Location = ({ description, middleware_address, id, -}: LocationModel) => ( -
-
-
-

- {name} -

- {description || "-"} + duty_staff_objects, +}: LocationModel) => { + const doctors = + duty_staff_objects?.filter((u) => u.user_type === "Doctor") || []; + const staffs = + duty_staff_objects?.filter((u) => u.user_type === "Staff") || []; + + const { t } = useTranslation(); + return ( +

+
+
+

{name}

+

{description}

+

{middleware_address}

+

+ {doctors.map((doctor) => ( +

+ +
{`${doctor.first_name} ${doctor.last_name}`}
+
+ ))}

-

-

- {middleware_address} -

+

+ {staffs.map((s) => ( +

+ +
{`${s.first_name} ${s.last_name}`}
+
+ ))} +

+
-
-
- - - Edit - - - - Manage Beds - +
+ + + {t("edit")} + + + + {t("manage_beds")} + +
-
-); + ); +}; diff --git a/src/Components/Facility/models.tsx b/src/Components/Facility/models.tsx index f5ddde19d2d..57bfc857aff 100644 --- a/src/Components/Facility/models.tsx +++ b/src/Components/Facility/models.tsx @@ -1,8 +1,8 @@ -import { AssignedToObjectModel } from "../Patient/models"; +import { AssetData } from "../Assets/AssetTypes"; import { ProcedureType } from "../Common/prescription-builder/ProcedureBuilder"; import { NormalPrescription, PRNPrescription } from "../Medicine/models"; -import { AssetData } from "../Assets/AssetTypes"; -import { UserBareMinimum } from "../Users/models"; +import { AssignedToObjectModel } from "../Patient/models"; +import { UserAssignedModel, UserBareMinimum } from "../Users/models"; export interface LocalBodyModel { name: string; @@ -194,6 +194,8 @@ export interface LocationModel { facility?: { name: string; }; + + duty_staff_objects?: UserAssignedModel[]; } export interface BedModel { diff --git a/src/Locale/en/Location.json b/src/Locale/en/Location.json new file mode 100644 index 00000000000..626cbada8da --- /dev/null +++ b/src/Locale/en/Location.json @@ -0,0 +1,6 @@ +{ + "location_management": "Location Management", + "manage_beds": "Manage Beds", + "add_new_location": "Add New Location", + "no_locations_available": "No Locations Available" +} \ No newline at end of file diff --git a/src/Locale/en/index.js b/src/Locale/en/index.js index 950b441ba84..f5479a805fe 100644 --- a/src/Locale/en/index.js +++ b/src/Locale/en/index.js @@ -1,19 +1,20 @@ -import Auth from "./Auth.json"; import Asset from "./Asset.json"; +import Auth from "./Auth.json"; +import Bed from "./Bed.json"; import Common from "./Common.json"; import Consultation from "./Consultation.json"; +import CoverImageEdit from "./CoverImageEdit.json"; import Entities from "./Entities.json"; +import ErrorPages from "./ErrorPages.json"; +import ExternalResult from "./ExternalResult.json"; import Facility from "./Facility.json"; import Hub from "./Hub.json"; -import ErrorPages from "./ErrorPages.json"; -import Shifting from "./Shifting.json"; +import Location from "./Location.json"; +import Medicine from "./Medicine.json"; import Notifications from "./Notifications.json"; -import ExternalResult from "./ExternalResult.json"; -import CoverImageEdit from "./CoverImageEdit.json"; import Resource from "./Resource.json"; +import Shifting from "./Shifting.json"; import SortOptions from "./SortOptions.json"; -import Bed from "./Bed.json"; -import Medicine from "./Medicine.json"; export default { ...Auth, @@ -31,5 +32,6 @@ export default { ...Resource, ...Shifting, ...Bed, + ...Location, SortOptions, }; diff --git a/src/Redux/actions.tsx b/src/Redux/actions.tsx index 1d2d6f4f7b6..74e892cf691 100644 --- a/src/Redux/actions.tsx +++ b/src/Redux/actions.tsx @@ -176,6 +176,16 @@ export const partialUpdateFacilityAssetLocation = ( external_id, }); +export const createFacilityAssetLocationDutyStaff = ( + params: object, + facility_external_id: string, + external_id: string +) => + fireRequest("createFacilityAssetLocationDutyStaff", [], params, { + facility_external_id, + external_id, + }); + // asset bed export const listAssetBeds = (params: object, altKey?: string) => fireRequest("listAssetBeds", [], params, {}, altKey); diff --git a/src/Redux/api.tsx b/src/Redux/api.tsx index 104fa0c0c75..85e51d526fa 100644 --- a/src/Redux/api.tsx +++ b/src/Redux/api.tsx @@ -27,11 +27,6 @@ import { AssetTransaction, AssetUpdate, } from "../Components/Assets/AssetTypes"; -import { - FacilityModel, - LocationModel, - WardModel, -} from "../Components/Facility/models"; import { IDeleteExternalResult, IExternalResult, @@ -40,6 +35,11 @@ import { ILocalBodyByDistrict, IPartialUpdateExternalResult, } from "../Components/ExternalResult/models"; +import { + FacilityModel, + LocationModel, + WardModel, +} from "../Components/Facility/models"; import { Prescription } from "../Components/Medicine/models"; import { PatientModel } from "../Components/Patient/models"; import { UserModel } from "../Components/Users/models"; @@ -282,6 +282,10 @@ const routes = { path: "/api/v1/facility/{facility_external_id}/asset_location/{external_id}/", method: "PATCH", }, + createFacilityAssetLocationDutyStaff: { + path: "/api/v1/facility/{facility_external_id}/asset_location/{external_id}/duty_staff/", + method: "POST", + }, // Asset bed listAssetBeds: {