-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(feat) Introduce Location Datasource (#113)
* Introduce concept location control * Refactor datasource implementation code
- Loading branch information
Showing
5 changed files
with
79 additions
and
21 deletions.
There are no files selected for viewing
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
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
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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { openmrsFetch, OpenmrsResource } from '@openmrs/esm-framework'; | ||
import { DataSource } from '../api/types'; | ||
|
||
export class BaseOpenMRSDataSource implements DataSource<OpenmrsResource> { | ||
url: string; | ||
|
||
constructor(url: string) { | ||
this.url = url; | ||
} | ||
|
||
fetchData(searchTerm: string): Promise<any[]> { | ||
return openmrsFetch(searchTerm ? `${this.url}&q=${searchTerm}` : this.url).then(({ data }) => { | ||
return data.results; | ||
}); | ||
} | ||
|
||
toUuidAndDisplay(data: OpenmrsResource): OpenmrsResource { | ||
if (typeof data.uuid === 'undefined' || typeof data.display === 'undefined') { | ||
throw new Error("'uuid' or 'display' not found in the OpenMRS object."); | ||
} | ||
return data; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { openmrsFetch } from '@openmrs/esm-framework'; | ||
import { BaseOpenMRSDataSource } from './data-source'; | ||
|
||
export class LocationDataSource extends BaseOpenMRSDataSource { | ||
constructor(){ | ||
super("/ws/rest/v1/location?v=custom:(uuid,display)"); | ||
} | ||
|
||
fetchData(searchTerm: string, config?: Record<string, any>): Promise<any[]> { | ||
if (config?.tag) { | ||
let urlParts = this.url.split('?'); | ||
this.url = `${urlParts[0]}?tag=${config.tag}&${urlParts[1]}`; | ||
} | ||
return openmrsFetch(searchTerm ? `${this.url}&q=${searchTerm}` : this.url).then(({ data }) => { | ||
return data.results; | ||
}); | ||
} | ||
|
||
} |
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