Skip to content

Instantly share code, notes, and snippets.

@kobvel
Last active October 8, 2017 08:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kobvel/594667902df26f02d5e7cf737952b0fa to your computer and use it in GitHub Desktop.
Save kobvel/594667902df26f02d5e7cf737952b0fa to your computer and use it in GitHub Desktop.
import { Store } from '@ngrx/store';
import { Injectable } from '@angular/core';
import { Resolve } from '@angular/router';
import { Observable } from 'rxjs/Observable';
import { IAppState } from './app.state';
import * as profileActions from './profile/profile.actions'
import { IProfileData } from './profile/profile.model';
import { ApiService } from './api.service';
@Injectable()
export class ProfileResolver implements Resolve<IProfileData> {
constructor(private apiService: ApiService, private store: Store<IAppState>) { }
resolve(): Observable<IProfileData> {
this.initProfileData();
return this.waitForProfileDataToLoad();
}
waitForProfileDataToLoad(): Observable<IProfileData> {
return this.store.select('profile')
.map(store => store.profileData)
.filter(profileData => !!profileData)
.take(1);
}
initProfileData(): void {
this.store.take(1).subscribe(store => {
if (!store.profile.profileData) {
this.apiService.getProfileData().toPromise().then(data => {
this.store.dispatch(new profileActions.UpdateAction(data));
});
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment