Search code examples
angularngrxngrx-effects

How to call another dispatch action in an NgRx Effect?


Overview: User presses the + button to add increase item in cart by 1.(this.store.dispatch(CartActions.IncrementCoin({name}))

After this the next method is called
this.store.dispatch(CartActions.updateTotalPrice());

This is to update the total price of all the products, now I want to move the second action call to an effect, because the second action is a side effect of the increment action. But how do I do this? I have not seen an answer to this question online so I'm asking it here.

This is the effect I'm trying to make, so it listens to the incementCoin action. After this it needs to fire the updateTotalPrice action. How do I structure this?

enter image description here

enter image description here

enter image description here

UPDATE

The following solution has been implemented. This effect now listens to 5 different actions!

enter image description here


Solution

  • This is how it might be implemented:

    public incrementCoin$ = createEffect(() => this.actions$
      .pipe(
        ofType(CartActions.IncrementCoin),
        map(CartActions.updateTotalPrice)
      )
    )