Skip to content

Instantly share code, notes, and snippets.

@Dssdiego
Last active May 1, 2019 10:54
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 Dssdiego/f4cf5a357563c2f1fd944f4d36f54afc to your computer and use it in GitHub Desktop.
Save Dssdiego/f4cf5a357563c2f1fd944f4d36f54afc to your computer and use it in GitHub Desktop.
import { Injectable } from '@angular/core';
import { MatDialog,MatSnackBar } from '@angular/material';
@Injectable()
export class AlertService {
constructor(
private snackBar: MatSnackBar
) { }
showSnackBar(message: string) {
this.snackBar.open(message, null, {
duration: 2000,
});
}
}
constructor(
private confirmService: AppConfirmService // Serviço Nativo do Angular, não precisa ser criado no sistema
) { }
deleteItem(client) {
this.confirmService.confirm({ message: `Excluir o cliente "${client.name}"?` })
.subscribe(res => { // Subscribe é uma função de observables, significa que está verificando se há resultado de retorno da função
if (res) { // Se houver resultado continua
this.clientsService.deleteClient(client) // Chama no serviço a função de excluir (no nosso caso aqui, vai existir um serviço de tasks)
.then(() => this.alertService.showSnackBar("Cliente Excluído com Sucesso"), // Após exclusão, chama a função de mostrar um snackbar no serviço (este serviço precisa ser criado no sistema, código dele acima)
err => {
this.alertService.showSnackBar("Ocorreu um Erro") // Se tiver tido um erro, mostra um snackbar com erro
console.error(err);
});
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment