Skip to content

Instantly share code, notes, and snippets.

@alexytiger
Last active December 30, 2019 01:14
Show Gist options
  • Save alexytiger/2f9c6e1f4fd840766ce9febcc77b8388 to your computer and use it in GitHub Desktop.
Save alexytiger/2f9c6e1f4fd840766ce9febcc77b8388 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 * as fromRoot from '../store';
@Injectable({
providedIn: 'root',
})
export class EthInitGuard implements CanActivate {
constructor(private store: Store<fromRoot.AppState>) {}
canActivate(): Observable<boolean> {
return this.checkStore().pipe(
switchMap(() => of(true)),
catchError(() => of(false))
);
}
checkStore(): Observable<boolean> {
return this.store.pipe(
select(fromRoot.getMetaMaskEnable),
tap(connected => {
if (!connected) {
this.store.dispatch(fromRoot.Web3ProviderActions.init());
}
}),
filter(connected => connected),
take(1)
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment