-
Notifications
You must be signed in to change notification settings - Fork 7
/
app.component.ts
51 lines (40 loc) · 1.57 KB
/
app.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { Component, OnInit, OnDestroy, ViewContainerRef } from '@angular/core';
import { Router, UrlSegment, NavigationEnd } from '@angular/router';
import { MdSnackBar } from '@angular/material';
import { Observable } from 'rxjs/Observable';
import { Subscription } from 'rxjs/Subscription';
import './rxjs_operators';
import { UserService } from './services/utils/user.service';
import { UserData } from './data/user-data';
import { SnackService, SnackBarMessage } from './services/utils/snack.service';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit, OnDestroy {
public sub: Subscription = null;
constructor(private user: UserService, public router: Router,
public viewContainerRef: ViewContainerRef, private snackbar: MdSnackBar,
private snackService: SnackService) {
}
ngOnInit() {
const source: Observable<boolean> = this.user.userDataState.map((u: UserData) => u.loggedIn);
// Go to home at login
// source.filter((u: boolean) => u === true)
// .subscribe(() => this.router.navigate(['/']));
// Go to login page when logged out
this.sub = source
.filter((u: boolean) => u === false)
.subscribe(() => this.router.navigate(['/login']));
this.snackService.newMessage$.subscribe(m => this.showSnackBar(m));
}
ngOnDestroy() {
if (this.sub !== null) {
this.sub.unsubscribe();
}
}
private showSnackBar(message: SnackBarMessage) {
this.snackbar.open(message.message, message.action);
}
}