I use Nx to create a new application using NgRx and DataPersistence.
I have generated several states in my application (example: State A, State B).
To retrieve the data via an API request to populate State A, I use in the effect associated with this.dataPersistence.fetch.
However to launch my API request, I need data contained in State B. This is where I block.
I saw that by using the basic effects, we can use the "withLastestFrom" operator to retrieve a different state than the one currently used.
@Injectable()
export class ElementEffects {
constructor (private store: Store<any>, private apiService: ApiService) {}
@Effect()
public yourEffect: Observable<Action> = this.actions$.pipe(
ofType<yourActionClass>(ActionsEnum.YOUR_ACTION),
withLatestFrom(this.store.pipe(select(selectSomethingFromTheStore))),
concatMap(([action, selectedDateFromTheStore]) => this.apiService.doBackendCall(selectedDateFromTheStore, action.payload).pipe(
map(([resultFromTheBackendCall, selectedDateFromTheStore]) => {
// Do Stuff
},
catchError((error) => of(new FailureAction(error)))
)
),
);
}
However I do not know how to handle that with this.dataPersistence.fetch.
By opening the DataPersistent code (https://github.com/nrwl/nx/blob/master/packages/angular/src/runtime/nx/data-persistence.ts), I see that the fetch
feature is already using the withLatestFrom
operator. I do not see how to recover another State.
fetch<A extends Action = Action>(
actionType: string,
opts: FetchOpts<T, A>
): Observable<any> {
return this.actions.pipe(
ofType<A>(actionType),
withLatestFrom(this.store),
fetch(opts)
);
}
My question is therefore: How to recover State B when I use a State A effect?
Hoping to be clear enough :)