separate notification responsability

This commit is contained in:
Peter Maquiran
2023-09-19 10:40:39 +01:00
parent 4cea8358fc
commit be3f1e8f15
4 changed files with 132 additions and 17 deletions
@@ -1,13 +1,13 @@
import { TestBed } from '@angular/core/testing';
import { NotificationsStoreService } from './notifications-store.service';
import { NotificationHolderService } from './notification-holder.service';
describe('NotificationsStoreService', () => {
let service: NotificationsStoreService;
describe('NotificationHolderService', () => {
let service: NotificationHolderService;
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(NotificationsStoreService);
service = TestBed.inject(NotificationHolderService);
});
it('should be created', () => {
@@ -0,0 +1,119 @@
import { Injectable } from '@angular/core';
import { StorageService } from '../storage.service';
@Injectable({
providedIn: 'root'
})
export class NotificationHolderService {
notificationList = []
constructor(
private storageService: StorageService,
) {
try {
this.restore()
} catch(error) {}
}
restore() {
this.storageService.get("Notifications").then((store) => {
if(Array.isArray(store)) {
this.notificationList = store
}
}).catch((error) => {})
}
save() {
this.storageService.store("Notifications", this.notificationList)
}
addNotification(notification) {
const element = notification
const i = this.notificationList.length + 1
let notificationObject;
if (element.notification) {
notificationObject = {
index: i,
title: element.notification.title,
Service: element.data.Service,
Object: element.data.Object,
IdObject: element.data.IdObject,
FolderId: element.data.FolderId,
body: element.notification.body,
dateInit: this.getFormatedTime(element.data.dateInit),
dateEnd: this.getFormatedTime(element.data.dateEnd),
Location: element.data.Location,
TypeAgenda: element.data.TypeAgenda,
Role: element.data.Role,
Status: element.data.Status,
read: false,
}
} else if (element.data) {
notificationObject = {
index: i,
title: element.title,
Service: element.data.Service,
Object: element.data.Object,
IdObject: element.data.IdObject,
FolderId: element.data.FolderId,
body: element.body,
dateInit: this.getFormatedTime(element.data.dateInit),
dateEnd: this.getFormatedTime(element.data.dateEnd),
Location: element.data.Location,
TypeAgenda: element.data.TypeAgenda,
Role: element.data.Role,
Status: element.data.Status,
read: false,
}
} else {
{
notificationObject = {
FolderId: element.FolderId,
IdObject: element.IdObject,
Location: element.Location,
Object: element.Object,
Role: element.Role,
Service: element.Service,
Status: element.Status,
TypeAgenda: element.TypeAgenda,
body: element.body,
dateEnd: element.dateEnd,
dateInit: element.dateInit,
index: element.index,
title: element.title,
read: false,
}
}
}
this.notificationList.push(notificationObject)
this.save()
}
getFormatedTime(dateString) {
var date = new Date(dateString);
var hours = date.getHours() /* > 12 ? date.getHours() - 12 : date.getHours() */;
var am_pm = date.getHours() >= 12 ? "pm" : "am";
var minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
let time = hours + ":" + minutes /* + " " + am_pm */;
return time;
}
removeNotification(notification) {
this.notificationList = this.notificationList.filter( (e) => {
return e.index != notification.index
})
this.save()
}
}
@@ -1,9 +0,0 @@
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class NotificationsStoreService {
constructor() { }
}
+5
View File
@@ -149,14 +149,19 @@ export class HeaderPage implements OnInit {
console.log('Get notification data',)
this.zone.run(() => {
this.notificationLength = value.length;
console.log('Call notification data', this.notificationLength)
});
}).catch((error) => {
if (!error) {
this.zone.run(() => {
console.error('header storage get notification', error)
this.notificationLength = 0;
});
} else {
console.error('header storage get notification', error)
}