How can I create a ngrx effect which triggers ONLY if both actions complete and return value?
Example: actionASuccess + actionBSuccess -> effect
I tried this solution: How to wait for 2 Actions in @ngrx/effects with combineLatest or ofType(...waitFor) but both do not work as expected.
Here is my code. The content of the switchMap gets called twice but should be called only once:
navigateEmployeeNotificationsTypeSuccess$ = createEffect(() =>
combineLatest([
this.actions$.pipe(ofType(RouterActions.navigateSuccess)),
this.actions$.pipe(ofType(RouterActions.navigateEmployeeNotificationsTypeSuccess)),
]).pipe(
filter(
([action1, action2]) =>
RouterEffects.notNull(action1.success) && RouterEffects.notNull(action2.tab) && RouterEffects.notNull(action2.notificationsType),
),
switchMap(([{ success }, { tab, notificationsType }]) => {
if (success) {
// have to set all sections active = false before changing state of the current
const data = cloneDeep(tab.data);
data.notificationTypes = data.notificationTypes.map((notificationType: NotificationType) => {
const active = isEqual(notificationType, notificationsType);
return {
...notificationType,
active,
};
});
return [
TabsActions.setTabData({ tab, data }),
TabsActions.saveCurrentRoute({ url: this.router.url }),
TabsActions.replaceLastBreadcrumb({ breadcrumb: notificationsType.managerName }),
];
}
return EMPTY;
}),
catchError((error) => of(requestError({ error }))),
),
);