feat: notifications
This commit is contained in:
16
client/src/app/services/notification.spec.ts
Normal file
16
client/src/app/services/notification.spec.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
|
||||
import { NotificationService } from './notification';
|
||||
|
||||
describe('Notification', () => {
|
||||
let service: NotificationService;
|
||||
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({});
|
||||
service = TestBed.inject(NotificationService);
|
||||
});
|
||||
|
||||
it('should be created', () => {
|
||||
expect(service).toBeTruthy();
|
||||
});
|
||||
});
|
||||
41
client/src/app/services/notification.ts
Normal file
41
client/src/app/services/notification.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import { Injectable, signal } from '@angular/core';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
})
|
||||
export class NotificationService {
|
||||
notifications = signal<Notification[]>([]);
|
||||
|
||||
show(notification: Notification) {
|
||||
this.notifications.update(items => [
|
||||
...items,
|
||||
notification,
|
||||
]);
|
||||
}
|
||||
|
||||
remove(notification: Notification) {
|
||||
this.notifications.update(items =>
|
||||
items.filter(item => item !== notification)
|
||||
);
|
||||
}
|
||||
|
||||
success(message: string) {
|
||||
this.show({ type: 'success', message});
|
||||
}
|
||||
|
||||
error(message: string) {
|
||||
this.show({ type: 'danger', message});
|
||||
}
|
||||
|
||||
warn(message: string) {
|
||||
this.show({ type: 'warning', message});
|
||||
}
|
||||
info(message: string) {
|
||||
this.show({ type: 'info', message});
|
||||
}
|
||||
}
|
||||
export interface Notification {
|
||||
type: 'success' | 'warning' | 'info' | 'danger';
|
||||
message: string;
|
||||
delay?: number;
|
||||
}
|
||||
Reference in New Issue
Block a user