I'm trying to catch an error on the effect of a specific action. Everything is working fine, whether when the call succeeds or fails. However, I think the code is very verbose, and could be improved on, specially:
- The catchError part, I'm returning
empty()but I bieleve should be returning theSavedReplyDeleteFailedaction. When doing so the whole effects breaks and does not compile, not sure on whats the best practice for handling this. - I'm relying too much on the
tapanddispatchinstead of returning the actions from the map/mergeMap call backs. When I do so the effects compilation breaks as well.
How I can apply the above improvements?
deleteSavedReply = createEffect(() => this.actions$.pipe(
ofType(SavedRepliesActions.SavedReplyDeleteRequested),
mergeMap( action => {
this.store.dispatch( SavedRepliesActions.DeleteLoading({ isLoading: true }) );
return this.savedRepliesService.deleteSavedReply(action.id).pipe(
tap(res => {
this.store.dispatch( SavedRepliesActions.SavedReplyDeleteSucess({ id: action.id }));
this.toastr.success( 'Saved Reply has been successfully deleted');
}),
catchError((error) => {
this.store.dispatch( SavedRepliesActions.SavedReplyDeleteFailed({message: error.message}) );
this.toastr.error( error.message, 'Something went wrong');
this.store.dispatch( SavedRepliesActions.DeleteLoading({ isLoading: false }) );
return empty();
})
);
}),
map(() => {
return SavedRepliesActions.DeleteLoading({ isLoading: false });
}),
));