Created
November 25, 2019 22:31
-
-
Save parzibyte/24b0f581ae96ff0fc5a63cd6d947a565 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component, OnInit } from '@angular/core'; | |
import { MascotasService } from "../mascotas.service" | |
import { Mascota } from "../mascota" | |
import { MatDialog } from '@angular/material/dialog'; | |
import { DialogoConfirmacionComponent } from "../dialogo-confirmacion/dialogo-confirmacion.component" | |
import { MatSnackBar } from '@angular/material/snack-bar'; | |
@Component({ | |
selector: 'app-listar-mascotas', | |
templateUrl: './listar-mascotas.component.html', | |
styleUrls: ['./listar-mascotas.component.css'] | |
}) | |
export class ListarMascotasComponent implements OnInit { | |
private mascotas: Mascota[] = [ | |
new Mascota("Maggie", "Chihuahua", 20) | |
]; | |
constructor(private mascotasService: MascotasService, private dialogo: MatDialog, private snackBar: MatSnackBar) { } | |
eliminarMascota(mascota: Mascota) { | |
this.dialogo | |
.open(DialogoConfirmacionComponent, { | |
data: `¿Realmente quieres eliminar a ${mascota.nombre}?` | |
}) | |
.afterClosed() | |
.subscribe((confirmado: Boolean) => { | |
if (!confirmado) return; | |
this.mascotasService | |
.deleteMascota(mascota) | |
.subscribe(() => { | |
this.obtenerMascotas(); | |
this.snackBar.open('Mascota eliminada', undefined, { | |
duration: 1500, | |
}); | |
}); | |
}) | |
} | |
ngOnInit() { | |
this.obtenerMascotas(); | |
} | |
obtenerMascotas() { | |
return this.mascotasService | |
.getMascotas() | |
.subscribe((mascotas: Mascota[]) => this.mascotas = mascotas); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment