Skip to content
Snippets Groups Projects
Commit 550d21e4 authored by Tomas Madeja's avatar Tomas Madeja
Browse files

feat: delete game button triggers chain of even emmits

parent 4d88ba40
No related branches found
No related tags found
No related merge requests found
<p>All Games</p> <p>All Games</p>
<app-games [games]=games></app-games> <app-games [games]=games (reloadEvent)="handleRefreshEvent()"></app-games>
...@@ -22,4 +22,7 @@ export class AllGamesComponent implements OnInit { ...@@ -22,4 +22,7 @@ export class AllGamesComponent implements OnInit {
.subscribe(games => this.games = games); .subscribe(games => this.games = games);
} }
handleRefreshEvent() {
this.getGames();
}
} }
<button type="button" class="button delete-button" (click)="onDeleteClick()"></button> <button type="button" class="button delete-button" (click)="onDeleteClick()">Delete [{{id}}]</button>
import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core'; import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
import { GameService } from 'src/app/services/game.service';
@Component({ @Component({
selector: 'app-delete-game-button', selector: 'app-delete-game-button',
...@@ -11,12 +12,13 @@ export class DeleteGameButtonComponent implements OnInit { ...@@ -11,12 +12,13 @@ export class DeleteGameButtonComponent implements OnInit {
@Output() onDelete: EventEmitter<number> = new EventEmitter(); @Output() onDelete: EventEmitter<number> = new EventEmitter();
constructor() { } constructor(private gameService: GameService) { }
ngOnInit(): void { ngOnInit(): void {
} }
onDeleteClick(): void { onDeleteClick(): void {
this.gameService.deleteGame(this.id);
this.onDelete.emit(this.id); this.onDelete.emit(this.id);
} }
} }
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
<th>{{game.homeTeam.name}}</th> <th>{{game.homeTeam.name}}</th>
<th>{{game.awayTeam.name}}</th> <th>{{game.awayTeam.name}}</th>
<th>{{game.homeTeamScore}}:{{game.awayTeamScore}}</th> <th>{{game.homeTeamScore}}:{{game.awayTeamScore}}</th>
<th><app-delete-game-button [id]=game.id (onDelete)="triggerRefreshEvent()"></app-delete-game-button></th>
</tr> </tr>
</table> </table>
import { Component, Input, OnInit } from '@angular/core'; import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
import { Game } from 'src/app/models/game'; import { Game } from 'src/app/models/game';
@Component({ @Component({
selector: 'app-games', selector: 'app-games',
...@@ -9,8 +9,14 @@ export class GamesComponent implements OnInit { ...@@ -9,8 +9,14 @@ export class GamesComponent implements OnInit {
@Input() games!: Array<Game>; @Input() games!: Array<Game>;
@Output() reloadEvent: EventEmitter<any>= new EventEmitter();
constructor() { } constructor() { }
ngOnInit(): void { ngOnInit(): void {
} }
triggerRefreshEvent(): void {
this.reloadEvent.emit(null);
}
} }
import { ValueConverter } from '@angular/compiler/src/render3/view/template';
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { Observable, of } from 'rxjs'; import { Observable, of } from 'rxjs';
import { Game } from '../models/game'; import { Game } from '../models/game';
...@@ -25,6 +26,6 @@ export class GameService { ...@@ -25,6 +26,6 @@ export class GameService {
} }
deleteGame(id: number) { deleteGame(id: number) {
this.games = this.games.filter((val, i, array) => val.id != id);
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment