Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(feat) O3-4143: useLocations should support searching by tag or query… #1191

Merged
merged 3 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
Loading