Я хочу удалить элемент в моем приложении (Angular версия 6).Раньше я вызывал службу напрямую, но теперь использовал ngrx / store и ngrx / Effects и отправлял действия.
Как показано в коде, я мог легко показать уведомление, если удаление было успешным.
Вопрос
Как мой компонент узнает, была ли операция удаления успешной или нет?
Как теперь отобразить уведомление при использованиидействия?
Заранее спасибо.
Код
component.ts
constructor(
private store: Store<any>,
private snackBar: MatSnackBar,
) { }
deleteTopic(topicId: number) {
// new way: dispatch action to delete the topic
this.store.dispatch(new fromCourse.DeleteTopic(topicId));
// old way
// this.courseService.deleteTopic(topicId)
// .subscribe(
// data => {
// this.ngOnInit();
// this.snackBar.open('successfully deleted');
// },
// err => {
// if (err.status === 403) {
// this.snackBar.open('you dont have access to delete');
// }
// }
// );
}
эффекты
@Effect()
deleteTopic$ = this.actions$.pipe(
ofType<fromCourse.DeleteTopic>(CourseActionTypes.DELETE_TOPIC),
switchMap((action) => {
return this.courseService.deleteTopic(action.payload).pipe( // payload is the topicId to be deleted
map(data => new fromCourse.DeleteTopicSuccess(action.payload) ) // send the topicId to reducer, to update state
);
})
);
редуктор
case CourseActionTypes.DELETE_TOPIC_SUCCESS: {
// find the index of topic and splice it
return {
// return new state
};
}