From e0b25734e43034e712fa69454de4405e30918fd2 Mon Sep 17 00:00:00 2001 From: Abhiuday Date: Tue, 5 Dec 2023 16:14:31 +0530 Subject: [PATCH] fix(location): updated modal UI and fixed reloading --- .../Facility/LocationManagement.tsx | 176 +++++++++++------- 1 file changed, 104 insertions(+), 72 deletions(-) diff --git a/src/Components/Facility/LocationManagement.tsx b/src/Components/Facility/LocationManagement.tsx index a68ca368d81..7f11d35a39c 100644 --- a/src/Components/Facility/LocationManagement.tsx +++ b/src/Components/Facility/LocationManagement.tsx @@ -72,38 +72,50 @@ export default function LocationManagement({ facilityId }: Props) { ); } -interface LocationProps extends LocationModel { - facilityId: string; -} -const Location = (props: LocationProps) => { +const DutyStaff = ({ + facilityId, + locationId, + toggle, + setToggle, +}: { + facilityId: string; + locationId: string; + toggle: boolean; + setToggle: React.Dispatch>; +}) => { const { t } = useTranslation(); - const { id, name, description, middleware_address, facilityId } = props; - const [toggle, setToggle] = useState(false); const [disabled, setDisabled] = useState(false); const { data, loading } = useQuery(routes.getFacilityUsers, { pathParams: { facility_id: facilityId }, }); const [selected, setSelected] = useState(); - const userList = - data?.results.filter( - (u) => u.user_type === "Doctor" || u.user_type === "Staff" - ) || []; - const { data: dutyStaffList, - loading: dutyStaffLoading, + loading: _dutyStaffLoading, refetch, } = useQuery(routes.getFacilityAssetLocationDutyStaff, { - pathParams: { facility_external_id: facilityId, external_id: id ?? "" }, + pathParams: { + facility_external_id: facilityId, + external_id: locationId ?? "", + }, }); + const dutyStaffIds = dutyStaffList?.map((u) => u.id) || []; + const userList = + data?.results + .filter((u) => u.user_type === "Doctor" || u.user_type === "Staff") + .filter((u) => !dutyStaffIds.includes(u.id)) || []; + const handleAssign = async () => { if (!selected) return; setDisabled(true); const { res } = await request(routes.createFacilityAssetLocationDutyStaff, { - pathParams: { facility_external_id: facilityId, external_id: id ?? "" }, + pathParams: { + facility_external_id: facilityId, + external_id: locationId ?? "", + }, body: { duty_staff: selected.id }, }); @@ -129,7 +141,10 @@ const Location = (props: LocationProps) => { setDisabled(true); const { res } = await request(routes.removeFacilityAssetLocationDutyStaff, { - pathParams: { facility_external_id: facilityId, external_id: id ?? "" }, + pathParams: { + facility_external_id: facilityId, + external_id: locationId ?? "", + }, body: { duty_staff: userId }, }); @@ -148,9 +163,74 @@ const Location = (props: LocationProps) => { setDisabled(false); }; - if (loading || dutyStaffLoading) { - return ; - } + return ( + setToggle((prev) => !prev)} + > +
+ setSelected(e.value)} + options={userList} + optionLabel={(option) => + `${option.first_name} ${option.last_name} (${option.user_type})` + } + optionValue={(option) => option} + isLoading={loading} + /> + handleAssign()} + disabled={!selected} + > + {t("assign")} + +
+
+ {dutyStaffList?.map((user) => ( +
+
+ +
{`${user.first_name} ${user.last_name} (${user.user_type})`}
+
+ handleDelete(user.id)} + > + + +
+ ))} +
+
+ ); +}; + +interface LocationProps extends LocationModel { + facilityId: string; +} + +const Location = (props: LocationProps) => { + const { t } = useTranslation(); + const { id, name, description, middleware_address, facilityId } = props; + const [toggle, setToggle] = useState(false); return (
@@ -164,60 +244,12 @@ const Location = (props: LocationProps) => {
{toggle && ( - setToggle((prev) => !prev)} - > -
- setSelected(e.value)} - options={userList} - optionLabel={(option) => - `${option.first_name} ${option.last_name} (${option.user_type})` - } - optionValue={(option) => option} - isLoading={loading} - /> - handleAssign()} - disabled={!selected} - > - {t("assign")} - -
-
- {dutyStaffList?.map((user) => ( -
-
- -
{`${user.first_name} ${user.last_name} (${user.user_type})`}
-
- handleDelete(user.id)} - > - {t("remove")} - -
- ))} -
-
+ )}