import { HttpClient } from '@angular/common/http';
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { environment } from 'src/environments/environment';
import { TeamsService } from './teams.service';

@Component({
  selector: 'app-teams',
  templateUrl: './teams.component.html',
  styleUrls: ['./teams.component.scss']
})
export class TeamsComponent implements OnInit {

  constructor(
    private route: ActivatedRoute, 
    private http: HttpClient, 
    private router: Router,
    public teamsService: TeamsService) {}

ngOnInit() {
   const hash = this.route.snapshot.paramMap.get('hash');
   if (hash) {
    this.loadTeamOverviewByAlias(hash);
   }
   
}

  private async loadTeamOverviewByAlias(alias: string) { 
    const url = `${environment.apiUrl}teams/` + alias;
    this.http.get(url).subscribe(result => {
      if (result) {
        this.teamsService.team = result;
      } else {
        this.router.navigate(['/auth'])
      }
    }, error => {
      this.router.navigate(['/auth'])
    })
  }

}
