Skip to content

Instantly share code, notes, and snippets.

@alexytiger
Last active March 2, 2019 19:33
Show Gist options
  • Save alexytiger/973951109b47a65742399aeb4ebf9e2a to your computer and use it in GitHub Desktop.
Save alexytiger/973951109b47a65742399aeb4ebf9e2a to your computer and use it in GitHub Desktop.
import { Injectable } from '@angular/core';
import { CanActivate } from '@angular/router';
import { Store, select } from '@ngrx/store';
import { Observable, of } from 'rxjs';
import { map, take, tap, filter, switchMap, catchError } from 'rxjs/operators';
import {EthAnchorModule} from '../eth.anchor.module';
import * as fromEth from '../../ethereum';
@Injectable({
providedIn: EthAnchorModule,
})
export class EthInitGuard implements CanActivate {
constructor(private store: Store<fromEth.AppState>) {}
canActivate(): Observable<boolean> {
return this.checkStore().pipe(
switchMap(() => of(true)),
catchError(() => of(false))
);
}
checkStore(): Observable<boolean> {
return this.store.pipe(
select(fromEth.getConStatus),
tap(connected => {
if (!connected) {
this.store.dispatch(new fromEth.InitEth());
}
}),
filter(connected => connected),
take(1)
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment