Search code examples
angularrxjsobservablengrx

Get params from typeOf ngrx effects


I have something like this

ngOnInit() {
  this.store.dispatch(new TutorialActions.GetTutorials(1, 20));
}

export class GetTutorials implements Action {
  readonly type = GET_TUTORIALS
  constructor(public number: number, public offset: number) {}
}

And then i have effects like this

export class TutorialEffects {
  loadTutorials$ = createEffect(() =>
    this.action$.pipe(
      ofType(TutorialActions.GET_TUTORIALS),
      switchMap(() =>
        this.tutorialService.getAll(0, 20).pipe(
          map((tutorial: Tutorial[]) => new TutorialActions.SuccesGetTutorials(tutorial)),
          catchError((error) => of (error))
        )
      )
    )
  );
}

The problem is in ofType(TutorialActions.GET_TUTORIALS), this.tutorialService.getAll(0,20).

That value should be passed from ngOnInit functions and can be dynamic?


Solution

  • Yes, you have access to the action object. So you can access the properties of it.

    export class TutorialEffects {
       loadTutorials$ = createEffect(() =>
         this.action$.pipe(
           ofType(TutorialActions.GET_TUTORIALS),
           switchMap((action: any) =>
             this.tutorialService.getAll(action.number, action.offset).pipe(
               map((tutorial: Tutorial[]) => new TutorialActions.SuccesGetTutorials(tutorial)),
               catchError((error) => of (error))
             )
           )
         )
       );
     }