Skip to content

Instantly share code, notes, and snippets.

@alexytiger
Created September 10, 2019 03:27
Show Gist options
  • Save alexytiger/28a7dddd1b8eea3fdab7dcc137e4aca6 to your computer and use it in GitHub Desktop.
Save alexytiger/28a7dddd1b8eea3fdab7dcc137e4aca6 to your computer and use it in GitHub Desktop.
@Injectable({
providedIn: P2pBazaarAnchorModule
})
export class ProductsLoadedGuard implements CanActivate {
constructor(private store: Store<fromStore.AppState>) {}
canActivate(): Observable<boolean> {
return this.waitForProductsToLoad().pipe(
switchMap(() => of(true)),
catchError(() => of(false))
);
}
waitForProductsToLoad(): Observable<boolean> {
return this.store.pipe(
select(fromStore.isProductsLoaded),
tap(loaded => {
if (!loaded) {
this.store.dispatch(PurchaseContractActions.loadProducts());
}
}),
filter(loaded => loaded),
take(1)
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment