-
Notifications
You must be signed in to change notification settings - Fork 522
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
c483e81
commit 7878cd6
Showing
11 changed files
with
463 additions
and
11 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,34 @@ | ||
import { Link } from "raviger"; | ||
import CareIcon from "../../CAREUI/icons/CareIcon"; | ||
import { FacilityModel } from "./models"; | ||
|
||
export default function FacilityBlock(props: { facility: FacilityModel }) { | ||
const { facility } = props; | ||
|
||
return ( | ||
<Link | ||
className="flex items-center gap-4 text-inherit" | ||
target="_blank" | ||
href={`/facility/${facility.id}`} | ||
> | ||
<div className="flex aspect-square h-14 shrink-0 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> | ||
</Link> | ||
); | ||
} |
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,154 @@ | ||
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"; | ||
import { useTranslation } from "react-i18next"; | ||
|
||
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 ( | ||
<div className="flex w-full flex-col items-center gap-4 md:flex-row"> | ||
{"id" in item ? ( | ||
<div className="w-full"> | ||
<FacilityBlock facility={item.spoke_object} /> | ||
</div> | ||
) : ( | ||
<FacilitySelect | ||
multiple={false} | ||
name="facility" | ||
placeholder={t("add_spoke")} | ||
showAll={false} | ||
showNOptions={8} | ||
selected={selectedFacility} | ||
setSelected={(v) => | ||
v && !Array.isArray(v) && setSelectedFacility(v) | ||
} | ||
errors="" | ||
className="w-full" | ||
disabled={processing} | ||
filter={(f) => | ||
!!f.id && | ||
facility.id !== 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="w-full shrink-0 md:w-auto" | ||
disabled={processing} | ||
/> | ||
</div> | ||
); | ||
}; | ||
|
||
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" | ||
allowCreate={(item) => !item.relationship || !item.spoke} | ||
> | ||
{FormRender} | ||
</ModelCrudEditor> | ||
</> | ||
); | ||
} |
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
Oops, something went wrong.