66 lines
1.7 KiB
TypeScript
66 lines
1.7 KiB
TypeScript
import { Injectable } from '@angular/core';
|
|
import { MatBottomSheet, MatBottomSheetRef } from '@angular/material/bottom-sheet';
|
|
import { NotificationsComponent } from '../components/notifications/notifications.component';
|
|
import { v4 as uuidv4 } from 'uuid';
|
|
import moment, { Moment } from 'moment';
|
|
|
|
@Injectable({
|
|
providedIn: 'root'
|
|
})
|
|
export class NotificationsService {
|
|
|
|
public messages: Message[] = [];
|
|
private handler?: MatBottomSheetRef;
|
|
|
|
constructor(
|
|
private bottomSheet$: MatBottomSheet
|
|
) { }
|
|
public add(message: Message): string {
|
|
message.id = uuidv4();
|
|
message.createdAt = moment();
|
|
this.messages.push(message);
|
|
this.sortMessages();
|
|
if (this.messages.length === 1) {
|
|
this.handler = this.bottomSheet$.open(NotificationsComponent, {
|
|
hasBackdrop: false,
|
|
disableClose: true
|
|
});
|
|
}
|
|
setTimeout(() => {
|
|
this.remove(message.id);
|
|
}, message.duration ? message.duration : 5000);
|
|
// close parent
|
|
if (message.parentId) {
|
|
this.remove(message.parentId);
|
|
}
|
|
return message.id;
|
|
}
|
|
private remove(id: string|undefined) {
|
|
this.messages = this.messages.filter(x => x.id!==id);
|
|
this.sortMessages();
|
|
if (this.messages.length === 0 && this.handler) {
|
|
this.handler.dismiss();
|
|
}
|
|
}
|
|
private sortMessages() {
|
|
this.messages = this.messages.sort((a, b) => {
|
|
return a.createdAt && a.createdAt.isAfter(b.createdAt) ? 1 : -1;
|
|
})
|
|
}
|
|
doAction(message: Message) {
|
|
if (message.action) { message.action(); }
|
|
this.remove(message.id);
|
|
}
|
|
}
|
|
|
|
interface Message {
|
|
id?: string;
|
|
text: string;
|
|
duration?: number;
|
|
btn?: string;
|
|
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
action?: Function;
|
|
createdAt?: Moment,
|
|
parentId?: string;
|
|
}
|