Skip to content
Snippets Groups Projects
app.component.ts 1.56 KiB
Newer Older
import {Component} from '@angular/core';
import {MatDialog, MatDialogConfig} from '@angular/material/dialog';
import { Observable, of } from 'rxjs';
import { mergeMap } from 'rxjs/operators';
import {LogInDialogComponent} from './components/log-in-dialog/log-in-dialog.component';
import {AuthServiceService} from './services/auth-service.service';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'Ice Hockey Manager';
Tomas Madeja's avatar
Tomas Madeja committed

  isAuthenticated: boolean = false;
  isTeamManager: boolean = false;
  isLeagueManager: boolean = false;
Tomas Madeja's avatar
Tomas Madeja committed

  constructor(
    private dialog: MatDialog,
    private authService: AuthServiceService
Tomas Madeja's avatar
Tomas Madeja committed

  ngOnInit(): void {
    this.refreshAuth();
  }

  refreshAuth(): void {
    this.authService.isAuthenticated().subscribe(e => this.isAuthenticated = e);
    this.authService.isTeamManager().subscribe(e => this.isTeamManager = e);
    this.authService.isLeagueManager().subscribe(e => this.isLeagueManager = e);
Tomas Madeja's avatar
Tomas Madeja committed
    const dialogConfig = new MatDialogConfig();
    dialogConfig.data = {};
Tomas Madeja's avatar
Tomas Madeja committed
    const dialogRef = this.dialog.open(LogInDialogComponent, dialogConfig);
    dialogRef.afterClosed().subscribe(data => this.onLogInClose(data));
  }

  onLogInClose(data: boolean | null): void {
    if (data != null || data) {
      this.refreshAuth();
    }
  }

  handleLogOutClick() {
    this.authService.logOut()
      .subscribe((success: boolean) => {
Tomas Madeja's avatar
Tomas Madeja committed
        if (success) {
          this.refreshAuth();
        }
      });
  }