Skip to content

Instantly share code, notes, and snippets.

@whisher
Created February 1, 2020 17:13
Show Gist options
  • Save whisher/cc8b87804dae5b8ee6b5794ae8e4880a to your computer and use it in GitHub Desktop.
Save whisher/cc8b87804dae5b8ee6b5794ae8e4880a to your computer and use it in GitHub Desktop.
@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