jpe-controle/FencerJudgeFront/src/app/components/match/matches/matches.component.ts
2025-06-03 14:14:54 +02:00

106 lines
2.9 KiB
TypeScript

import { Component } from '@angular/core';
import { Router } from '@angular/router';
import { MatchesService } from '@services/matches/matches.service';
import { Matches, MatchState } from '@interfaces/matches';
import { PlayerService } from '@services/player/player.service';
import { Player } from '@interfaces/player';
@Component({
selector: 'app-matches',
standalone: false,
templateUrl: './matches.component.html',
styleUrl: './matches.component.css',
})
export class MatchesComponent {
matches: Matches[] = [];
playersMap: Map<number, Player> = new Map();
MatchState = MatchState;
constructor(
private matchesService: MatchesService,
private playerService: PlayerService,
private router: Router
) {}
parseMatchState(stateStr: string): MatchState | undefined {
return MatchState[stateStr as keyof typeof MatchState];
}
ngOnInit(): void {
this.playerService.getPlayers().subscribe({
next: (response) => {
if (response && response.data) {
response.data.forEach((player) =>
this.playersMap.set(player.id, player)
);
} else {
console.warn(
'[MatchesComponent] Erreur lors du chargement des joueurs'
);
}
},
error: () =>
console.warn("[MatchesComponent] Erreur lors de l'appel getPlayers"),
});
this.matchesService.getMatches().subscribe({
next: (response) => {
if (response) {
this.matches = response;
this.matches = this.matches.map((match) => {
const enumState = this.parseMatchState(
match.state as unknown as string
);
return {
...match,
state: enumState !== undefined ? enumState : match.state,
};
});
} else {
console.warn(
'[MatchesComponent] Erreur lors du chargement des matchs'
);
}
},
error: () =>
console.warn("[MatchesComponent] Erreur lors de l'appel getMatches"),
});
}
getPlayerName(id: number): string {
const player = this.playersMap.get(id);
return player ? `${player.firstName} ${player.name}` : 'Joueur inconnu';
}
getMatchBorderColor(state: MatchState): string {
switch (state) {
case MatchState.NOT_STARTED:
return 'border-secondary';
case MatchState.ONGOING:
return 'border-warning';
case MatchState.OVER:
return 'border-success';
default:
return 'border-light';
}
}
getMatchStatusLabel(state: MatchState): string {
switch (state) {
case MatchState.NOT_STARTED:
return 'À venir';
case MatchState.ONGOING:
return 'En cours';
case MatchState.OVER:
return 'Terminé';
default:
return 'Inconnu';
}
}
goToMatchDetail(matchId: number): void {
this.router.navigate(['/matches', matchId]);
}
}