Created
February 1, 2020 17:13
-
-
Save whisher/cc8b87804dae5b8ee6b5794ae8e4880a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Injectable() | |
export class CategoriesGuard implements CanActivate { | |
constructor(private store: Store<fromStore.CategoriesState>) {} | |
canActivate(): Observable<boolean> { | |
return this.checkStore().pipe( | |
switchMap(() => of(true)), | |
catchError(() => of(false)) | |
); | |
} | |
checkStore(): Observable<boolean> { | |
return this.store.select(fromStore.selectCategoriesLoaded).pipe( | |
tap(loaded => { | |
if (!loaded) { | |
this.store.dispatch(new fromStore.LoadCategories()); | |
} | |
}), | |
take(1) | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment