Miomir Dancevic
Miomir Dancevic

Reputation: 6852

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?

Upvotes: 0

Views: 47

Answers (1)

hawks
hawks

Reputation: 941

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))
         )
       )
     )
   );
 }

Upvotes: 1

Related Questions