Last active
January 11, 2020 02:58
-
-
Save alexytiger/b22afbb7a671548a121267dbb9c4e6ef to your computer and use it in GitHub Desktop.
e-book
This file contains hidden or 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
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