remove notification duplicate

This commit is contained in:
Peter Maquiran
2023-09-26 16:13:29 +01:00
parent 43ebb4a062
commit 1f2827680d
+34 -9
View File
@@ -1,6 +1,6 @@
import { Injectable } from '@angular/core';
import { StorageService } from '../services/storage.service';
import { v4 as uuidv4 } from 'uuid'
@Injectable({
providedIn: 'root'
})
@@ -8,9 +8,12 @@ export class NotificationHolderService {
notificationList = []
private _notificationList = []
constructor(
private storageService: StorageService,
) {
) {
try {
this.restore()
@@ -18,27 +21,34 @@ export class NotificationHolderService {
}
reverse() {
this.notificationList = this._notificationList.reverse()
}
restore() {
this.storageService.get("Notifications").then((store) => {
if(Array.isArray(store)) {
this.notificationList = store
this._notificationList = store
this.reverse()
}
}).catch((error) => {})
}
save() {
this.storageService.store("Notifications", this.notificationList)
this.storageService.store("Notifications", this._notificationList)
}
addNotification(notification) {
const element = notification
const i = this.notificationList.length + 1
const i = this._notificationList.length + 1
let notificationObject;
if (element.notification) {
notificationObject = {
id: notification?.id || uuidv4(),
index: i,
title: element.notification.title,
Service: element.data.Service,
@@ -57,6 +67,7 @@ export class NotificationHolderService {
} else if (element.data) {
notificationObject = {
id: notification?.id || uuidv4(),
index: i,
title: element.title,
Service: element.data.Service,
@@ -76,6 +87,7 @@ export class NotificationHolderService {
} else {
{
notificationObject = {
id: notification?.id || uuidv4(),
FolderId: element.FolderId,
IdObject: element.IdObject,
Location: element.Location,
@@ -94,8 +106,19 @@ export class NotificationHolderService {
}
}
this.notificationList.push(notificationObject)
this.save()
if(this.notificationExist(notificationObject)) {
this._notificationList.push(notificationObject)
this.reverse()
this.save()
}
}
notificationExist(notificationObject) {
return this._notificationList.find(item => {
return item.id === notificationObject.id;
});
}
@@ -109,9 +132,11 @@ export class NotificationHolderService {
}
removeNotification(notification) {
this.notificationList = this.notificationList.filter( (e) => {
this._notificationList = this._notificationList.filter( (e) => {
return e.index != notification.index
})
this.reverse()
this.save()
}