Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How propagate action.payload from Action in ngrx-effect to catchError or map operator?

I write ngrx effect with httpRequest which use action.payload of Action.

From request I get data which I use in map operator (if error occur i handle it in catchError)

From Backend I get only boolean value, How I can propagate action.payload from Action to map or catchError operator?

Example:

effect= this.actions$
    .ofType(Actions.IS_OK)
    .pipe(
         mergeMap(action => this.service.isOk(action.payload),
         map((isOk: boolean)  => new SuccessAction(isOk, action.payload), //here I need somehow get to action.payload value
         catchError(error => of(new ErrorAction(error, action.payload))) //here I need somehow get to action.payload value
     );

And I can not store payload to store, because I have triggered more actions: Actions.IS_OK

Thanks for help

like image 864
Petr Avatar asked Sep 14 '25 14:09

Petr


2 Answers

Using some newer rxjs/ngrx syntax this can be written as:


isOkEffect$ = createEffect(() => this.actions$.pipe(
  ofType(Actions.IS_OK),
  mergeMap(({payload}) => this.service.isOk(payload).pipe(
    map((isOk: boolean) => new SuccessAction(isOk, payload),
    catchError(error => of(new ErrorAction(error, payload)))
  ))
));
like image 94
Tobias Lindgren Avatar answered Sep 17 '25 06:09

Tobias Lindgren


Can you try :

effect = this.actions$
    .ofType(Actions.IS_OK)
    .pipe(
         mergeMap(action => this.service.isOk(action.payload).pipe(
             map((isOk: boolean) => { payload: action.payload, isOk: isOk} )
         ),
         // need to create proper interface forPayload + isOk 
         map((payloadPlusIsOk: any)  => new SuccessAction(payloadPlusIsOk.isOk, payloadPlusIsOk.payload),
         catchError(error => of(new ErrorAction(error, action.payload))) //here I need somehow get to action.payload value
     );
like image 23
Pierre Mallet Avatar answered Sep 17 '25 05:09

Pierre Mallet