Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created November 25, 2019 22:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/24b0f581ae96ff0fc5a63cd6d947a565 to your computer and use it in GitHub Desktop.
Save parzibyte/24b0f581ae96ff0fc5a63cd6d947a565 to your computer and use it in GitHub Desktop.
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