In my application i have a modal with a list and an input search field. The modal is invoked on button click in parent component which calls the public function inside the modal component that fetches the list item.Also, whenever user types in the search bar, again the same http request is made to get all the data having the search value since all data has not been loaded at once.
For search input, i'm using subject which emits value on keyup.
public showModal(msg) {
this.headMsg = msg;
this.loadList();
}
loadList() {
this.onListUpdate(this.getList(""));
}
getList(param: string) {
this.loader = true;
return this.httpService.getProducts(param);
}
onListUpdate(list$: Observable<any>) {
list.subscribe((response: any[]) => {
if(response) this.items = response;
},
error => console.log(error),
() => this.loader = false
)
}
input keyup
this.filterChanged.next(searchTerm);
onSearchFieldChanged() {
let item$ = this.filterChanged.pipe(
debounceTime(this._debounceTime),
distinctUntilChanged(),
switchMap(term => {
return this.getList(term).pipe(
tap(() => {
this.loader = false;
})
);
})
)
this.onListUpdate(item$);
}
Is there a better way to handle the list update functionality?