Reputation: 112
I am trying to dispatch different action based on type splitting up in several effects. I know i am doing something wrong or thinking in a wrong way, so I am sharing my concerns.
The use case is the following:
notifyChangeInBack$ = createEffect(
() => {
return this.actions$.pipe(
ofType(
InteractionActions.interactionAccepted,
InteractionActions.interactionSelected,
),
exhaustMap(action => {
const { interaction } = action;
return this.interactionDatasource.notifyInteractionChange(interaction)
.pipe(
map(() => InteractionActions.changeInteractionSuccess({ interaction })),
catchError(() => of(InteractionActions.changeInteractionError({ interaction })))
)
})
);
}
);
changeInteractionSuccess
action, there are different functionality to execute based on action type. If the action was interactionAccepted
, the user has to navigate to a concrete page. If the action was interactionSelected
the user has to navigate to a url based in a logic. Right now, I have two effects for doing this:navigateToCustomer$ = createEffect(
() => {
return this.actions$.pipe(
ofType(InteractionActions.interactionAccepted),
tap((action) =>
// logic for navigation...
)
);
},
{ dispatch: false }
);
navigateToLastInteractionState$ = createEffect(
() => {
return this.actions$.pipe(
ofType(InteractionActions.interactionSelected),
concatLatestFrom(() => this.interactionFacade.selectInteractionsUi$),
tap(([action, interactionsUi]) => {
// logic for navigation...
})
);
},
{ dispatch: false }
);
My question is: how can I connect changeInteractionSuccess
action after notifying to back with different effects that needs the previous action? Should I create two different effects for notifying to back that listen to the actions separately? I.E: one effect for interactionAccepted
action which do the request and return a proper action and the same for the interactionSelected
action.
Many thanks in advance!!!
Upvotes: 0
Views: 1247
Reputation: 15515
You can nest actions, this is a pattern to listen for action X, then wait until Y occurs. For example:
navigateToCustomer$ = createEffect(
() => {
return this.actions$.pipe(
ofType(InteractionActions.interactionAccepted),
switchMap((action) => {
return this.actions$.pipe(ofType(InteractionActions.changeInteractionSuccess))
})
);
},
{ dispatch: false }
);
Upvotes: 1