How do I get the result of setUserCourses
in UserCourseService
to bubble up to DashboardBodyHomeCourseModalComponent
? At the moment, result
returns null.
Also, I am currently passing in an array called courseSelectedOptions
which executes multiple HTTP calls to my API. At the end of the last API call, I want to bubble up a HTTP post response to my component.
UserCourseService
setUserCourses(userId: string, courseSelectedOptions: number[]): Promise<unknown | void> {
return this.firebaseService.generateToken().then((token: string) => {
this.logger.debug(`Token generated for setUserCourses: ${token}`);
return observableFrom(courseSelectedOptions)
.pipe(
concatMap((courseSelectedOption) =>
this.httpClient.post(
`${environment.apiBasePath}/userCourses`,
{
user_id: userId,
course_id: courseSelectedOption,
},
{ headers: { authorization: `Bearer ${token}` } }
)
)
)
.subscribe(
(response) => this.logger.debug(`setUserCourses response: ${JSON.stringify(response)}`),
(error) => this.logger.debug(`setUserCourses error: ${JSON.stringify(error)}`),
() => this.logger.debug('setUserCourses complete')
);
});
}
DashboardBodyHomeFacade
setUserCourses(userId: string, courseSelectedOptions: number[]): Promise<unknown | void> {
return this.userCourseService.setUserCourses(userId, courseSelectedOptions);
}
DashboardBodyHomeCourseModalComponent
this.dashboardBodyHomeFacade
.setUserCourses(this.user.id, this.courseSelectedOptions)
.then((result: unknown) => {
if (result) {
this.toastrService.success('Your courses have been selected.', 'Great!');
this.bsModalRef.hide();
} else {
this.toastrService.warning('Something has gone wrong. Please try again.', 'Oops!');
}
})
.catch(() => {
this.toastrService.warning('Something has gone wrong. Please try again.', 'Oops!');
});