I have a resolver that needs to return Organization details from the api service. At the same time we are using ngrx to store the details of all organizations.
I want my resolver to look into the ngrx store: - populate it with list of Organizations if it is empty - find Organization with specific id from the ngrx store and return it.
The way i am trying to do it now:
resolve(route: ActivatedRouteSnapshot): Observable<Organization> {
const organizationId = route.paramMap.get(RouterHelperService.urlParam.organizationId);
this.initOrganizationsData();
const company = this.store.select(store => store.companies)
.filter(state => state && state.length > 0 && state.filter(c => c.id === organizationId).length > 0)
.take(1)[0];
return company;
}
and here is the method initOrganizationsData():
private initOrganizationsData(): void {
this.store.take(1).subscribe(store => {
if (store.companies.length === 0) {
this.organizationService.getOrganizations().subscribe(data => {
this.store.dispatch(new companyActions.LoadCompaniesSuccessAction(data));
});
}
});
}
At the moment I always get the the result Company is undefined.
Can anyone point me into the right direction. What is wrong? How to get Observable<Organization> from Observable<Organization[]> with specific value?
flattenshould work