Skip to content

Instantly share code, notes, and snippets.

@alexytiger
Last active January 11, 2020 02:58
Show Gist options
  • Save alexytiger/b22afbb7a671548a121267dbb9c4e6ef to your computer and use it in GitHub Desktop.
Save alexytiger/b22afbb7a671548a121267dbb9c4e6ef to your computer and use it in GitHub Desktop.
e-book
import { Injectable } from '@angular/core';
import { CanActivate } from '@angular/router';
import { Store, select } from '@ngrx/store';
import { Observable, of } from 'rxjs';
import { take, tap, filter, switchMap, catchError } from 'rxjs/operators';
import { MarketPlaceAnchorModule } from '../market-place-anchor.module';
import * as fromStore from '../store/reducers';
import { PurchaseContractActions } from '../store/actions';
@Injectable({
providedIn: MarketPlaceAnchorModule
})
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