Reputation: 2772
I saw some of the SO answers, and this medium post as well, but these still don't solve my current problem.
Scenario:
Each of these two cases can have errors:
Currently, i have the effect that will use a service to add a product, and dispatch an action based on the success of failure of the operation. This will update the store with the currentProduct being set to the added product.
@Effect()
addProduct$: Observable<Action> = this.actions$.pipe(
ofType(AddProductAction),
map((action: AddProductAction) => action.payload),
switchMap((ProductToAdd: Product) =>
this.productService.addProduct(ProductToAdd).pipe(
map(response => {
if (this.responseContainsBusinessError(response)) {
return new AddProductBusinessError(response);
} else { // no business error
return new AddProductSuccess(response);
}
}),
catchError(err => of(new AddProductFail(err))) // network/http error etc
)
)
);
Can someone please show how to change this code so that i can add the second effect/api call to fetch the list of products from the server ?
Upvotes: 2
Views: 570
Reputation: 441
Just add another @Effect which will trigger on AddProductSuccess
.
@Effect()
listAllProducts$: Observable<Action> = this.actions$.pipe(
ofType(AddProductSuccess),
....
Upvotes: 4