Skip to content
Snippets Groups Projects
app.component.ts 1.29 KiB
Newer Older
import {Component} from '@angular/core';
import {MatDialog, MatDialogConfig} from '@angular/material/dialog';
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;

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

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

  refreshAuth(): void {
    this.authService.isAuthenticated()
      .subscribe(isAuthenticated => this.isAuthenticated = isAuthenticated);
  }

  handleLogInClick() {
    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();
        }
      });
  }