Komla Sodji
Komla Sodji

Reputation: 15

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

Upvotes: 0

Views: 1128

Answers (1)

Daniel Caldera
Daniel Caldera

Reputation: 1119

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

Upvotes: 1

Related Questions