toast.service.ts 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import { Injectable } from '@angular/core';
  2. import { v4 as uuid } from 'uuid';
  3. interface ToastOptions {
  4. id: string;
  5. header: string;
  6. message: string;
  7. alertClass: 'danger' | 'warning' | 'info';
  8. delay?: number;
  9. }
  10. @Injectable({
  11. providedIn: 'root'
  12. })
  13. export class ToastService {
  14. public toasts: ToastOptions[] = [];
  15. constructor() {}
  16. public error(message: string, header: string, delay?: number) {
  17. this.toasts.push({
  18. id: uuid(),
  19. message,
  20. header,
  21. alertClass: 'danger',
  22. delay
  23. });
  24. }
  25. public warning(message: string, header: string, delay?: number) {
  26. this.toasts.push({
  27. id: uuid(),
  28. message,
  29. header,
  30. alertClass: 'warning',
  31. delay
  32. });
  33. }
  34. public info(message: string, header: string, delay?: number) {
  35. this.toasts.push({
  36. id: uuid(),
  37. message,
  38. header,
  39. alertClass: 'info',
  40. delay
  41. });
  42. }
  43. public remove(id: string) {
  44. const idx = this.toasts.findIndex(t => t.id === id);
  45. if (idx >= 0) this.toasts.splice(idx, 1);
  46. }
  47. }