From 347a4eeff1b2a142156c874a1022315575a6b6bf Mon Sep 17 00:00:00 2001 From: Polianin Nikita Date: Tue, 13 Feb 2024 02:53:39 +0300 Subject: [PATCH] feat: add a service to launch notifications --- src/services/open-notify.service.ts | 54 +++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/services/open-notify.service.ts diff --git a/src/services/open-notify.service.ts b/src/services/open-notify.service.ts new file mode 100644 index 0000000..de07ea9 --- /dev/null +++ b/src/services/open-notify.service.ts @@ -0,0 +1,54 @@ +import {Injectable} from '@angular/core'; +import {MatSnackBar} from '@angular/material/snack-bar'; +import {NotificationComponent} from "@component/notification/notification.component"; + +export enum NotifyColor { + Basic, + Warn, + Danger, + Success +} + +@Injectable({ + providedIn: 'root' +}) + +export class OpenNotifyService { + constructor(private _snackBar: MatSnackBar) { + } + + colorClass: string = ''; + + set setColorClass(color: NotifyColor) { + switch (color) { + case NotifyColor.Warn: { + this.colorClass = 'yellow-snackbar'; + } + break; + case NotifyColor.Danger: { + this.colorClass = 'red-snackbar'; + } + break; + case NotifyColor.Success: { + this.colorClass = 'green-snackbar'; + } + break; + default: { + this.colorClass = 'snackbar'; + } + break; + } + } + + open(message: string, color: NotifyColor = NotifyColor.Basic, duration: number = 5000) { + this.setColorClass = color; + this._snackBar.openFromComponent(NotificationComponent, { + data: {message: message, duration: duration, className: this.colorClass}, + duration: duration, + verticalPosition: 'top', + horizontalPosition: 'center', + panelClass: [this.colorClass] + }) + ; + } +}