-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
O3-953: Location picker does not show all locations (#343)
- Loading branch information
1 parent
4e97bb0
commit 6b93e02
Showing
8 changed files
with
276 additions
and
607 deletions.
There are no files selected for viewing
160 changes: 0 additions & 160 deletions
160
packages/apps/esm-login-app/src/choose-location/choose-location.component.test.tsx
This file was deleted.
Oops, something went wrong.
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
109 changes: 77 additions & 32 deletions
109
packages/apps/esm-login-app/src/choose-location/choose-location.resource.ts
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 |
---|---|---|
@@ -1,40 +1,85 @@ | ||
import { useEffect, useMemo } from "react"; | ||
import { | ||
openmrsFetch, | ||
openmrsObservableFetch, | ||
fhirBaseUrl, | ||
FetchResponse, | ||
showNotification, | ||
} from "@openmrs/esm-framework"; | ||
import { map } from "rxjs/operators"; | ||
import { LocationResponse } from "../types"; | ||
import { LocationEntry, LocationResponse } from "../types"; | ||
import useSwrInfinite from "swr/infinite"; | ||
|
||
export function getLoginLocations(): Observable<Object[]> { | ||
return openmrsObservableFetch( | ||
`/ws/rest/v1/location?tag=Login%20Location&v=custom:(uuid,display)` | ||
).pipe(map(({ data }) => data["results"])); | ||
} | ||
const fhirLocationUrl = `${fhirBaseUrl}/Location?_summary=data`; | ||
|
||
export function searchLocationsFhir( | ||
location: string, | ||
abortController: AbortController, | ||
useLoginLocationTag: boolean | ||
) { | ||
const baseUrl = `${fhirBaseUrl}/Location?name=${location}`; | ||
const url = useLoginLocationTag | ||
? baseUrl.concat("&_tag=login location") | ||
: baseUrl; | ||
return openmrsFetch<LocationResponse>(url, { | ||
method: "GET", | ||
signal: abortController.signal, | ||
}); | ||
} | ||
export function useLoginLocations( | ||
useLoginLocationTag: boolean, | ||
count: number = 0, | ||
searchQuery: string = "" | ||
): { | ||
locationData: Array<LocationEntry>; | ||
isLoading: boolean; | ||
totalResults: number; | ||
hasMore: boolean; | ||
loadingNewData: boolean; | ||
setPage: ( | ||
size: number | ((_size: number) => number) | ||
) => Promise<FetchResponse<LocationResponse>[]>; | ||
} { | ||
const getUrl = (page, prevPageData: FetchResponse<LocationResponse>) => { | ||
if ( | ||
prevPageData && | ||
!prevPageData?.data?.link?.some((link) => link.relation === "next") | ||
) | ||
return null; | ||
let url = fhirLocationUrl; | ||
if (count) { | ||
url += `&_count=${count}`; | ||
} | ||
if (page) { | ||
url += `&_getpagesoffset=${page * count}`; | ||
} | ||
if (useLoginLocationTag) { | ||
url += "&_tag=login location"; | ||
} | ||
if (typeof searchQuery === "string" && searchQuery != "") { | ||
url += `&name=${searchQuery}`; | ||
} | ||
return url; | ||
}; | ||
|
||
const { data, isValidating, size, setSize, error } = useSwrInfinite< | ||
FetchResponse<LocationResponse>, | ||
Error | ||
>(getUrl, openmrsFetch); | ||
|
||
if (error) { | ||
console.error(error.message); | ||
showNotification({ | ||
title: error.name, | ||
description: error.message, | ||
kind: "error", | ||
}); | ||
} | ||
|
||
useEffect(() => { | ||
setSize(1); | ||
}, [searchQuery, setSize]); | ||
|
||
const returnValue = useMemo(() => { | ||
return { | ||
locationData: data | ||
? [].concat(...data?.map((resp) => resp?.data?.entry)) | ||
: null, | ||
isLoading: !data, | ||
totalResults: data?.[0]?.data?.total ?? null, | ||
hasMore: data?.length | ||
? data?.[data.length - 1]?.data?.link.some( | ||
(link) => link.relation === "next" | ||
) | ||
: false, | ||
loadingNewData: isValidating, | ||
setPage: setSize, | ||
}; | ||
}, [data, isValidating, setSize]); | ||
|
||
export function queryLocations( | ||
location: string, | ||
abortController = new AbortController(), | ||
useLoginLocationTag: boolean | ||
) { | ||
return searchLocationsFhir( | ||
location, | ||
abortController, | ||
useLoginLocationTag | ||
).then((locs) => locs.data.entry); | ||
return returnValue; | ||
} |
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.