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
… string
  • Loading branch information
mogoodrich committed Oct 30, 2024
1 parent 3a2620e commit e028053
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
12 changes: 10 additions & 2 deletions packages/framework/esm-api/src/shared-api-objects/location.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,16 @@ export function toLocationObject(openmrsRestForm: any): Location {
};
}

export function getLocations(tagUuidOrName: string | null = null): Observable<Array<Location>> {
const url = `${restBaseUrl}/location` + (tagUuidOrName ? '?tag=' + tagUuidOrName : '');
export function getLocations(
tagUuidOrName: string | null = null,
query: string | null = null,
): Observable<Array<Location>> {
const url =
`${restBaseUrl}/location` +
(tagUuidOrName || query ? '?' : '') +
(tagUuidOrName ? 'tag=' + tagUuidOrName : '') +
(tagUuidOrName && query ? '&' : '') +
(query ? 'q=' + query : '');
return openmrsObservableFetch<any>(url)
.pipe(
map((results) => {
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();
}, []);
}, [query]);

return locations;
}

0 comments on commit e028053

Please sign in to comment.