-
Notifications
You must be signed in to change notification settings - Fork 502
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bda3f65
commit 4bf5708
Showing
10 changed files
with
403 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import CareIcon from "../../CAREUI/icons/CareIcon"; | ||
import { FacilityModel } from "./models"; | ||
|
||
export default function FacilityBlock(props: { facility: FacilityModel }) { | ||
const { facility } = props; | ||
|
||
return ( | ||
<div className="flex items-center gap-4"> | ||
<div className="flex aspect-square h-14 items-center justify-center overflow-hidden rounded-lg border border-gray-400 bg-gray-200"> | ||
{facility.read_cover_image_url ? ( | ||
<img | ||
className="h-full w-full object-cover" | ||
src={facility.read_cover_image_url} | ||
/> | ||
) : ( | ||
<> | ||
<CareIcon icon="l-hospital" /> | ||
</> | ||
)} | ||
</div> | ||
<div> | ||
<b className="font-semibold">{facility.name}</b> | ||
<p className="text-sm"> | ||
{facility.address} {facility.local_body_object?.name} | ||
</p> | ||
</div> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
//import { useTranslation } from "react-i18next"; | ||
import routes from "../../Redux/api"; | ||
import request from "../../Utils/request/request"; | ||
import useQuery from "../../Utils/request/useQuery"; | ||
import { SelectFormField } from "../Form/FormFields/SelectFormField"; | ||
import { | ||
FacilityModel, | ||
FacilitySpokeErrors, | ||
FacilitySpokeModel, | ||
FacilitySpokeRequest, | ||
SpokeRelationship, | ||
} from "./models"; | ||
import ModelCrudEditor from "../Form/ModelCrudEditor"; | ||
import { FacilitySelect } from "../Common/FacilitySelect"; | ||
import { useEffect, useState } from "react"; | ||
import { SPOKE_RELATION_TYPES } from "../../Common/constants"; | ||
import FacilityBlock from "./FacilityBlock"; | ||
|
||
export interface SpokeFacilityEditorProps { | ||
facility: Omit<FacilityModel, "id"> & { id: string }; | ||
} | ||
|
||
export default function SpokeFacilityEditor(props: SpokeFacilityEditorProps) { | ||
const { facility } = props; | ||
|
||
//const { t } = useTranslation(); | ||
|
||
const spokesQuery = useQuery(routes.getFacilitySpokes, { | ||
pathParams: { | ||
id: facility.id, | ||
}, | ||
}); | ||
|
||
const spokes = spokesQuery.data?.results; | ||
|
||
const createSpoke = (body: FacilitySpokeRequest) => | ||
request(routes.createFacilitySpoke, { | ||
body, | ||
pathParams: { | ||
id: facility.id, | ||
}, | ||
onResponse: ({ res }) => { | ||
if (res?.ok) { | ||
spokesQuery.refetch(); | ||
} | ||
}, | ||
}); | ||
|
||
const deleteSpoke = (spokeFacilityId: string) => | ||
request(routes.deleteFacilitySpoke, { | ||
pathParams: { | ||
id: facility.id, | ||
spoke_id: spokeFacilityId, | ||
}, | ||
onResponse: ({ res }) => { | ||
if (res?.ok) { | ||
spokesQuery.refetch(); | ||
} | ||
}, | ||
}); | ||
|
||
const updateSpoke = (spokeFacilityId: string, body: FacilitySpokeRequest) => | ||
request(routes.updateFacilitySpokes, { | ||
pathParams: { | ||
id: facility.id, | ||
spoke_id: spokeFacilityId, | ||
}, | ||
body, | ||
onResponse: ({ res }) => { | ||
if (res?.ok) { | ||
spokesQuery.refetch(); | ||
} | ||
}, | ||
}); | ||
|
||
const FormRender = ( | ||
item: FacilitySpokeModel | FacilitySpokeRequest, | ||
setItem: (item: FacilitySpokeModel | FacilitySpokeRequest) => void, | ||
processing: boolean, | ||
) => { | ||
const [selectedFacility, setSelectedFacility] = useState<FacilityModel>(); | ||
|
||
useEffect(() => { | ||
setItem({ ...item, spoke: selectedFacility?.id }); | ||
}, [selectedFacility]); | ||
|
||
return ( | ||
<> | ||
{"id" in item ? ( | ||
<div className="w-full"> | ||
<FacilityBlock facility={item.spoke_object} /> | ||
</div> | ||
) : ( | ||
<FacilitySelect | ||
multiple={false} | ||
name="facility" | ||
placeholder="Add Spoke Facility" | ||
showAll={false} | ||
showNOptions={8} | ||
selected={selectedFacility} | ||
setSelected={(v) => | ||
v && !Array.isArray(v) && setSelectedFacility(v) | ||
} | ||
errors="" | ||
className="w-full" | ||
disabled={processing} | ||
filter={(f) => | ||
!!f.id && | ||
!spokes?.flatMap((s) => s.spoke_object.id).includes(f.id) | ||
} | ||
/> | ||
)} | ||
<SelectFormField | ||
name="relationship_type" | ||
options={SPOKE_RELATION_TYPES} | ||
optionLabel={(v) => v.text} | ||
optionValue={(v) => v.value} | ||
value={item.relationship} | ||
onChange={(v) => setItem({ ...item, relationship: v.value })} | ||
errorClassName="hidden" | ||
className="shrink-0" | ||
disabled={processing} | ||
/> | ||
</> | ||
); | ||
}; | ||
|
||
return ( | ||
<> | ||
<ModelCrudEditor< | ||
FacilitySpokeModel, | ||
FacilitySpokeRequest, | ||
FacilitySpokeErrors | ||
> | ||
items={spokes} | ||
onCreate={createSpoke} | ||
onUpdate={updateSpoke} | ||
onDelete={deleteSpoke} | ||
loading={spokesQuery.loading} | ||
errors={{}} | ||
emptyText={"No Spokes"} | ||
empty={{ | ||
spoke: "", | ||
relationship: SpokeRelationship.REGULAR, | ||
}} | ||
createText="Add Spoke" | ||
> | ||
{FormRender} | ||
</ModelCrudEditor> | ||
</> | ||
); | ||
} |
Oops, something went wrong.