0

I want to chain effects with the latest syntax of ngrx. I've searched and found this question on stackoverflow but it has old syntax.

This is the current effect:

export class DeleteCommentEffect {

deleteComment$ = createEffect(() =>
    this.actions$.pipe(
        ofType(DeletingComment),
        mergeMap((action) => this.commentService.deleteComment(action.dossierId, action.commentId)
            .pipe(
                map((statusCode: number) => {
                    return DeleteCommentSuccess({ statusCode });
                }),
                catchError((error: HttpErrorResponse) => {
                    return of(DeleteCommentError({ error }));
                })
        ))
    )
);

constructor(
    private actions$: Actions,
    private commentService: DBCommentService) {
}

}

I want to chain this effect after successfully delete a comment.

export class GetCommentEffects {
getComment$ = createEffect(() =>
    this.actions$.pipe(
        ofType(GettingComment),
        mergeMap(action =>
            this.commentService.getAllComments(action.dossierId).pipe(
                map((comments: Comment[]) => {
                    return GetCommentSuccess({comments});
                }),
                catchError((error: HttpErrorResponse) => {
                    return of(GetCommentError({error}));
                })
            ))
    )
);

constructor(
    private actions$: Actions,
    private commentService: DBCommentService
) {}

}

I've searched in ngrx docs but it seems like it does not mention about how to chain effects.

Loc Truong
  • 359
  • 5
  • 22

1 Answers1

1

Create an effect that listens for DeleteCommentSuccess action that dispatches GettingComment.

deleteCommentSuccess$ = createEffect(() =>
  this.actions$.pipe(
    ofType(DeleteCommentSuccess),
    map(() => CommentActions.GettingComments())
  )
);
jordanpowell88
  • 857
  • 3
  • 11
  • 25
  • Thanks, I'll try this. – Loc Truong Nov 07 '19 at 16:00
  • Let me know if you run into any problem but this is essentially the purpose of side effects in that they handle the various side effects of your app. You can also trigger them using ngrx-router as well which would listen for certain route changes that then dispatch certain actions – jordanpowell88 Nov 07 '19 at 20:14
  • Sorry for replying late, your solution works. Thank you. – Loc Truong Nov 11 '19 at 04:19