forked from ohcnetwork/care_fe
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds support for filtering patients by diagnoses (ohcnetwork#7062)
* Adds support for filtering by diagnoses * code cleanup * sort diagnoses filter based on priority
- Loading branch information
1 parent
5cc5072
commit 35e78bd
Showing
9 changed files
with
211 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import routes from "../../Redux/api"; | ||
import request from "../../Utils/request/request"; | ||
import { ICD11DiagnosisModel } from "./types"; | ||
|
||
// TODO: cache ICD11 responses and hit the cache if present instead of making an API call. | ||
|
||
export const getDiagnosisById = async (id: ICD11DiagnosisModel["id"]) => { | ||
return (await request(routes.getICD11Diagnosis, { pathParams: { id } })).data; | ||
}; | ||
|
||
export const getDiagnosesByIds = async (ids: ICD11DiagnosisModel["id"][]) => { | ||
return Promise.all([...new Set(ids)].map(getDiagnosisById)); | ||
}; |
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,81 @@ | ||
import { useEffect, useState } from "react"; | ||
import { ICD11DiagnosisModel } from "../Diagnosis/types"; | ||
import { getDiagnosesByIds } from "../Diagnosis/utils"; | ||
import { useTranslation } from "react-i18next"; | ||
import AutocompleteMultiSelectFormField from "../Form/FormFields/AutocompleteMultiselect"; | ||
import useQuery from "../../Utils/request/useQuery"; | ||
import routes from "../../Redux/api"; | ||
import { mergeQueryOptions } from "../../Utils/utils"; | ||
import { debounce } from "lodash-es"; | ||
|
||
export const FILTER_BY_DIAGNOSES_KEYS = [ | ||
"diagnoses", | ||
"diagnoses_confirmed", | ||
"diagnoses_unconfirmed", | ||
"diagnoses_provisional", | ||
"diagnoses_differential", | ||
] as const; | ||
|
||
export const DIAGNOSES_FILTER_LABELS = { | ||
diagnoses: "Diagnoses (of any verification status)", | ||
diagnoses_unconfirmed: "Unconfirmed Diagnoses", | ||
diagnoses_provisional: "Provisional Diagnoses", | ||
diagnoses_differential: "Differential Diagnoses", | ||
diagnoses_confirmed: "Confirmed Diagnoses", | ||
} as const; | ||
|
||
export type DiagnosesFilterKey = (typeof FILTER_BY_DIAGNOSES_KEYS)[number]; | ||
|
||
interface Props { | ||
name: DiagnosesFilterKey; | ||
value?: string; | ||
onChange: (event: { name: DiagnosesFilterKey; value: string }) => void; | ||
} | ||
export default function DiagnosesFilter(props: Props) { | ||
const { t } = useTranslation(); | ||
const [diagnoses, setDiagnoses] = useState<ICD11DiagnosisModel[]>([]); | ||
const { data, loading, refetch } = useQuery(routes.listICD11Diagnosis); | ||
|
||
useEffect(() => { | ||
if (!props.value) { | ||
setDiagnoses([]); | ||
return; | ||
} | ||
if (diagnoses.map((d) => d.id).join(",") === props.value) { | ||
return; | ||
} | ||
|
||
// Re-use the objects which we already have, fetch the rest. | ||
const ids = props.value.split(","); | ||
const existing = diagnoses.filter(({ id }) => ids.includes(id)); | ||
const objIds = existing.map((o) => o.id); | ||
const diagnosesToBeFetched = ids.filter((id) => !objIds.includes(id)); | ||
getDiagnosesByIds(diagnosesToBeFetched).then((data) => { | ||
const retrieved = data.filter(Boolean) as ICD11DiagnosisModel[]; | ||
setDiagnoses([...existing, ...retrieved]); | ||
}); | ||
}, [props.value]); | ||
|
||
return ( | ||
<AutocompleteMultiSelectFormField | ||
label={DIAGNOSES_FILTER_LABELS[props.name]} | ||
labelClassName="text-sm" | ||
name="icd11_search" | ||
className="w-full" | ||
placeholder={t("search_icd11_placeholder")} | ||
value={diagnoses} | ||
onChange={(e) => { | ||
setDiagnoses(e.value); | ||
props.onChange({ | ||
name: props.name, | ||
value: e.value.map((o) => o.id).join(","), | ||
}); | ||
}} | ||
options={mergeQueryOptions(diagnoses, data ?? [], (obj) => obj.id)} | ||
optionLabel={(option) => option.label} | ||
optionValue={(option) => option} | ||
onQuery={debounce((query: string) => refetch({ query: { query } }), 300)} | ||
isLoading={loading} | ||
/> | ||
); | ||
} |
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
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