You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
41 lines
1.1 KiB
TypeScript
41 lines
1.1 KiB
TypeScript
import { Injectable } from '@angular/core';
|
|
import { BehaviorSubject, timer } from 'rxjs';
|
|
import { switchMap, startWith } from 'rxjs/operators';
|
|
|
|
@Injectable({
|
|
providedIn: 'root'
|
|
})
|
|
export class NotificationService {
|
|
private notificationSubject = new BehaviorSubject<{ type: string, message: string } | null>(null);
|
|
notifications$ = this.notificationSubject.asObservable();
|
|
|
|
success(message: string) {
|
|
this.notify('success', 'Success: ' + message);
|
|
}
|
|
|
|
error(message: string) {
|
|
this.notify('error', 'Error: ' + message);
|
|
}
|
|
|
|
warning(message: string) {
|
|
this.notify('warning', 'Warning: ' + message);
|
|
}
|
|
|
|
info(message: string) {
|
|
this.notify('info', 'Info: ' + message);
|
|
}
|
|
|
|
private notify(type: string, message: string) {
|
|
this.notificationSubject.next({ type, message });
|
|
|
|
// Automatically clear notification after 3 seconds using RxJS timer
|
|
this.notifications$.pipe(
|
|
startWith(null),
|
|
switchMap(() => timer(5000))
|
|
).subscribe(() => this.notificationSubject.next(null));
|
|
}
|
|
clearNotification() {
|
|
this.notificationSubject.next(null);
|
|
}
|
|
}
|