mission-list.component.ts 1.18 KB
Newer Older
Milan Mozolák's avatar
Milan Mozolák committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import { HttpClient } from '@angular/common/http';
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { firstValueFrom } from 'rxjs';
import { environment } from '../../environments/environment';
import { RequestState } from '../request-state.enum';
import { Mission } from './mission.interface';

@Component({
  selector: 'mission-list',
  templateUrl: 'mission-list.component.html',
})
export class MissionListComponent implements OnInit {
  requestState = RequestState;
  state: RequestState = this.requestState.PENDING;
  errorMessage = '';
Juraj Fiala's avatar
Juraj Fiala committed
17
  missions: Mission[] = [];
Milan Mozolák's avatar
Milan Mozolák committed
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
  displayedColumns: string[] = ['name'];

  constructor(private httpClient: HttpClient, private router: Router) {}

  async ngOnInit() {
    this.state = RequestState.PENDING;
    try {
      this.missions = (
        await firstValueFrom(
          this.httpClient.get<Mission[]>(`${environment.apiUrl}/missions`)
        )
      ).sort((a, b) => (a.name === b.name ? 0 : a.name > b.name ? 1 : -1));
      this.state = RequestState.SUCCESS;
    } catch (err) {
      this.state = RequestState.ERROR;
      this.errorMessage = 'Could not load missions!';
      throw err;
    }
  }
}