Hello I need to fetch data coming from API and then based on response return Observable in ngOnInit.
@Edit Basically this.dayOverViewByGroup
should be fully downloaded from api when is returned in fetchChildrensData()
=> return Observable.of(this.dayOverViewByGroup)
.
@Edit2 Added code for getDayOverViewByGroupId
ngOnInit
ngOnInit() {
const filter$: Observable<string> = this.filterControl.valueChanges.pipe(
startWith(''),
debounceTime(100),
distinctUntilChanged(),
share()
);
this.dayOverViewByGroup$ = this.fetchChildrensData().pipe(
map(days => days.map(day => (
{
...day,
filteredChildren$: filter$.pipe(this.filterFrom(day.children))
}
)))
);
}
fetchChildrensData function
fetchChildrensData(): Observable<any[]> {
return Observable.of(this.dayOverViewByGroup)
}
Functions that handle API call
getChildrens(date, groupId) {
return this.attendanceService.getDayOverViewByGroupId(date, groupId).then(
(res1: any) => {
let dayOverViewByGroup = res1.mobile_employee_dayoverview;
this.dayOverViewByGroup = dayOverViewByGroup;
this.appFunctionCtrl.dismissLoader();
},
(error) => console.log('Error occured in this.attendanceService.getDayOverViewByGroupId()', error)
);
}
getDayOverViewByGroupId(currentDate = null, groupId) {
let that = this;
return new Promise((resolve, reject) => {
that.wsseGenerator.getWSSEHeader().then(header => {
header.append('Access-Control-Allow-Origin', '*');
header.append('Access-Control-Allow-Methods', 'POST, GET, OPTIONS, PUT');
let options = new RequestOptions({ headers: header });
let paramsData = {
groupId: groupId,
currentDate: currentDate
};
let params = that.serializeObject(paramsData);
that.http.post(`${that.baseUrl}/dayoverview?${params}`, null, options).timeout(this.timeTimeout).subscribe((success: any) => {
resolve(success);
}, (err) => {
this.appFunctionCtrl.dismissLoader();
if (err.name == 'TimeoutError') {
this.alert.present();
}
else {
reject(err);
}
});
});
});
}