Search code examples
angularngrx-storengrx-effectsngrx-router-store

ngrx/effect how to parse params from a selector into a GET


I m getting the Id and the ova from a Selected service selector and want to parse it into a GET method calling an API endpoint in the effect. this is what I have done but on the selected service page load I'm getting null when I log the data. Need helps.

@Effect()
    loadService$ = this.actions$.ofType(servicesActions.LOAD_ONE_SERVICE)
    .pipe(        
        switchMap(() => {
            this.store.select(fromSelect.getSelectedService).subscribe(
                data => {
                    this.id = data.merchantId,
                    this. ova = data.ova
                }
            )
            return this.appservice
            .getServiceById(
                this.id,
                this.ova
            )
            .pipe(
                map(service => new servicesActions.LoadServiceSuccess(service)),
                catchError(error => of (new servicesActions.LoadServiceFail(error)))
            );
        })
    );

Solution

  • You can try something like this. Use the switchmap to keep one level of nesting in the pipe.

    @Effect()
        loadService$ = this.actions$.ofType(servicesActions.LOAD_ONE_SERVICE)
        .pipe(
          withLatestFrom(this.store.select(fromSelect.getSelectedService)),
          switchMap(({merchantId, ova}) => this.appservice.getServiceById(merchantId, ova))
          .pipe(
            map(service => new servicesActions.LoadServiceSuccess(service)),
            catchError(error => of(new servicesActions.LoadServiceFail(error)))
          )         
        );