Skip to content

Commit

Permalink
Fetching obs for a patient should only fetch required params and useI…
Browse files Browse the repository at this point in the history
…nitialEncounters hook fix (#811)
  • Loading branch information
vasharma05 authored Sep 13, 2023
1 parent fa12f3f commit db1bf35
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { FetchResponse, getSynchronizationItems, openmrsFetch, useConfig, usePatient } from '@openmrs/esm-framework';
import {
FetchResponse,
OpenmrsResource,
getSynchronizationItems,
openmrsFetch,
useConfig,
usePatient,
} from '@openmrs/esm-framework';
import camelCase from 'lodash-es/camelCase';
import { Dispatch, useEffect, useMemo, useState } from 'react';
import useSWR from 'swr';
Expand Down Expand Up @@ -239,14 +246,16 @@ export function useInitialPatientIdentifiers(patientUuid: string): {
function useInitialEncounters(patientUuid: string, patientToEdit: fhir.Patient) {
const { registrationObs } = useConfig() as RegistrationConfig;
const { data, error, isLoading } = useSWR<FetchResponse<{ results: Array<Encounter> }>>(
patientToEdit
? `/ws/rest/v1/encounter?patient=${patientUuid}&v=full&encounterType=${registrationObs.encounterTypeUuid}`
patientToEdit && registrationObs.encounterTypeUuid
? `/ws/rest/v1/encounter?patient=${patientUuid}&v=custom:(encounterDatetime,obs:(concept:ref,value:ref))&encounterType=${registrationObs.encounterTypeUuid}`
: null,
openmrsFetch,
);
const obs = data?.data.results.sort(latestFirstEncounter)?.at(0)?.obs;
const encounters = obs
?.map(({ concept, value }) => ({ [concept['uuid']]: value['uuid'] }))
?.map(({ concept, value }) => ({
[(concept as OpenmrsResource).uuid]: typeof value === 'object' ? value?.uuid : value,
}))
.reduce((accu, curr) => Object.assign(accu, curr), {});

return { data: encounters, isLoading, error };
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Session } from '@openmrs/esm-framework';
import { OpenmrsResource, Session } from '@openmrs/esm-framework';
import { RegistrationConfig } from '../config-schema';
import { SavePatientTransactionManager } from './form-manager';

Expand Down Expand Up @@ -128,8 +128,8 @@ export interface Encounter {
}>;
form: string;
obs: Array<{
concept: string;
value: string | number;
concept: string | OpenmrsResource;
value: string | number | OpenmrsResource;
}>;
}

Expand Down

0 comments on commit db1bf35

Please sign in to comment.