In my Angular Cli app, I have a data table with dynamic filters and a search bar. Whenever the search value changes, pagination changes, or a filter is applied or removed, I need to update the table items and filter information by making an API call.
My service has a function that handles the API call and returns the response:
getFilters() {
const body = { searchTerm: 'foo' }; //
return http.post(url, body)
.map(res => res)
.catch(err => Observable.of(err);
}
getTableItems() {
const body = { searchTerm: 'foo', ...stuff for filtering }; //
return http.post(url, body)
.map(res => res)
.catch(err => Observable.of(err);
}
getApiData() {
const filters = this.getFilters();
const items = this.getTableItems();
return forkJoin([ filters, items ]);
}
Since I have multiple methods triggering API calls, I'm duplicating the
getApiData().subscribe(res => doSomethingWithResponse(res));
in the constructor and each trigger method.
How can I avoid this repetition and maintain a DRY codebase? I utilize @ngrx/store
for data storage on the Front End and work with rxjs Observable
's and BehaviorSubject
's.