Skip to content

Commit

Permalink
(feat) O3-4143: useLocations should support searching by tag or query… (
Browse files Browse the repository at this point in the history
#1191)

---------

Co-authored-by: Dennis Kigen <kigen.work@gmail.com>
  • Loading branch information
mogoodrich and denniskigen authored Nov 12, 2024
1 parent e21aca6 commit a861e3a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
18 changes: 15 additions & 3 deletions packages/framework/esm-api/src/shared-api-objects/location.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,21 @@ export function toLocationObject(openmrsRestForm: any): Location {
};
}

export function getLocations(tagUuidOrName: string | null = null): Observable<Array<Location>> {
const url = `${restBaseUrl}/location` + (tagUuidOrName ? '?tag=' + tagUuidOrName : '');
return openmrsObservableFetch<any>(url)
export function getLocations(
tagUuidOrName: string | null = null,
query: string | null = null,
): Observable<Array<Location>> {
const params = new URLSearchParams();
if (tagUuidOrName) {
params.set('tag', tagUuidOrName);
}
if (query) {
params.set('q', query);
}
const queryString = params.toString();
const url = `${restBaseUrl}/location${queryString ? '?' + queryString : ''}`;

return openmrsObservableFetch<{ results: Array<Location> }>(url)
.pipe(
map((results) => {
const locations: Array<Location> = results.data.results.map(toLocationObject);
Expand Down
6 changes: 3 additions & 3 deletions packages/framework/esm-react-utils/src/useLocations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import type { Location } from '@openmrs/esm-api';
import { getLocations } from '@openmrs/esm-api';
import { useState, useEffect } from 'react';

export function useLocations(tagUuidOrName: string | null = null) {
export function useLocations(tagUuidOrName: string | null = null, query: string | null = null): Array<Location> {
const [locations, setLocations] = useState<Array<Location>>([]);

useEffect(() => {
const locationSub = getLocations(tagUuidOrName).subscribe(
const locationSub = getLocations(tagUuidOrName, query).subscribe(
(locations) => {
setLocations(locations);
},
Expand All @@ -16,7 +16,7 @@ export function useLocations(tagUuidOrName: string | null = null) {
},
);
return () => locationSub.unsubscribe();
}, []);
}, [tagUuidOrName, query]);

return locations;
}

0 comments on commit a861e3a

Please sign in to comment.