I am struggling with passing parameter to selector when by using withLatestFrom, which was mapped earlier from load action payload
loadLocalSubServices$: Observable<Action> = this.actions$.pipe(
ofType(LocalSubServiceTemplateActions.LocalSubServicesTemplateActionTypes.LoadLocalSubService),
map((action: LocalSubServiceTemplateActions.LoadLocalSubService) => action.payload.globalSubServiceId),
// and below I would like to pass globalSubServiceId
withLatestFrom(this.store.pipe(select(fromLocalSubservices.getSearchParams(globalSubServiceId)))),
map(searchParams => searchParams[1]),
mergeMap((params) =>
this.subServiceService.getLocalSubServices(params).pipe(
map(localSubServices => (new LocalSubServiceTemplateActions.LocalSubServiceLoadSuccess(localSubServices))),
catchError(err => of(new LocalSubServiceTemplateActions.LocalSubServiceLoadFail(err)))
)
)
);
I think I have the recipe you (or future wanderers) are looking for. You have to map the initial payload (of
operator below) to an inner observable so that it can be piped and passed as a param to withLatestFrom
. Then mergeMap
will flatten it and you can return it to the next operator as one array with the initial payload as the first value.
map(action => action.payload),
mergeMap((id) =>
of(id).pipe(
withLatestFrom(
this.store.pipe(select(state => getEntityById(state, id))),
this.store.pipe(select(state => getWhateverElse(state)))
)
),
(id, latestStoreData) => latestStoreData
),
switchMap(([id, entity, whateverElse]) => callService(entity))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With