Mario
Mario

Reputation: 183

Waiting for ngrx action before loading page with URL parameter

I'm building an ng2 application using ngrx. When the app is launched a web service is called to get initial data, once this data is fetched I create an INIT_DONE action.

My State looks like this :

export interface State {
  documents: Document[];
  selectedDocument: Document
}

When I go to the page /mypage/456 where 456 is a url parameter, I need to get some of the fetched data so I get the URL parameter like this :

ngOnInit() {
  this.paramSubscription = this.route.params
    .select<string>('id')
    .map((id) => new SelectAction(id))
    .subscribe(this.store);
}

The SELECT_ACTION finds the element in the fetched data and sets selectedDocument. The problem is that the SELECT_ACTION is created before INIT_DONE and at that point documents is empty.

How do I wait for INIT_DONE before loading my page ?

Upvotes: 8

Views: 5408

Answers (3)

chrigu
chrigu

Reputation: 826

I would make use of the combineLatest operator as it combines the latest values of multiple source streams. In addition I'd double-check that documents is set (here I assumed it's an array) using filter.

ngOnInit() {
  this.subscription = Observable.combineLatest(
      this.store.select("documents")
          .filter(documents => documents.length > 0),
      this.paramSubscription = this.route.params
          .select<string>('id')
  )
  .map((combinedData: [Object[], string]) => combinedData[1])
  .subscribe(this.store);
}

Also assign the subscription to a variable so that you can unsubscribe when the component is destroyed. Otherwise your subscription will be around after the component has been destroyed and it's possible that your action still gets emitted:

ngOnDestroy() {
    this.subscription.unsubscribe();
}

Upvotes: 6

Andr&#233; Werlang
Andr&#233; Werlang

Reputation: 5964

You need a resolver. A resolver waits until data is available before completing the navigation action.

@Injectable()
export class DocumentsResolver implements Resolve {

    constructor(
        private store: Store
    ) {}

    resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<Hero> {
        // take id from snapshot
        const id = route.params['id'];
        // start with the document list
        return this.store.select('documents')
          // wait until there is data available
          .filter(documents => documents && documents.length > 0)
          // then produce the selected document
          .mergeMapTo(this.store.select('selectedDocument'));
    }
}

On route configuration:

export const DocumentsRoutes: RouterConfig = [
    { path: 'documents/:id', component: DocumentsDetailComponent, resolve: { document: DocumentsResolver } }
];

More about router resolve here

Upvotes: 4

John Hamm
John Hamm

Reputation: 514

You could select documents from the store and subscribe to it and emit your action from there:

ngOnInit() {
  this.store.select("documents").subscribe(documents => {
    this.paramSubscription = this.route.params
      .select<string>('id')
      .map((id) => new SelectAction(id))
      .subscribe(this.store);
  });  
}

Upvotes: 0

Related Questions