Files
doneit-web/src/app/store/notification-holder.service.ts
T

144 lines
3.7 KiB
TypeScript
Raw Normal View History

2023-08-31 16:37:11 +01:00
import { Injectable } from '@angular/core';
import { StorageService } from '../services/storage.service';
2023-09-26 16:13:29 +01:00
import { v4 as uuidv4 } from 'uuid'
2023-08-31 16:37:11 +01:00
@Injectable({
providedIn: 'root'
})
export class NotificationHolderService {
notificationList = []
2023-09-26 16:13:29 +01:00
private _notificationList = []
2023-08-31 16:37:11 +01:00
constructor(
private storageService: StorageService,
2023-09-26 16:13:29 +01:00
) {
2023-08-31 16:37:11 +01:00
try {
this.restore()
} catch(error) {}
}
2023-09-26 16:13:29 +01:00
reverse() {
this.notificationList = this._notificationList.reverse()
}
2023-08-31 16:37:11 +01:00
restore() {
this.storageService.get("Notifications").then((store) => {
if(Array.isArray(store)) {
2023-09-26 16:13:29 +01:00
this._notificationList = store
this.reverse()
2023-08-31 16:37:11 +01:00
}
}).catch((error) => {})
}
save() {
2023-09-26 16:13:29 +01:00
this.storageService.store("Notifications", this._notificationList)
2023-08-31 16:37:11 +01:00
}
addNotification(notification) {
const element = notification
2023-09-26 16:13:29 +01:00
const i = this._notificationList.length + 1
2023-08-31 16:37:11 +01:00
let notificationObject;
if (element.notification) {
notificationObject = {
2023-09-26 16:13:29 +01:00
id: notification?.id || uuidv4(),
2023-08-31 16:37:11 +01:00
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 = {
2023-09-26 16:13:29 +01:00
id: notification?.id || uuidv4(),
2023-08-31 16:37:11 +01:00
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 = {
2023-09-26 16:13:29 +01:00
id: notification?.id || uuidv4(),
2023-08-31 16:37:11 +01:00
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,
}
}
}
2023-09-26 16:13:29 +01:00
if(this.notificationExist(notificationObject)) {
this._notificationList.push(notificationObject)
this.reverse()
this.save()
}
}
notificationExist(notificationObject) {
return this._notificationList.find(item => {
return item.id === notificationObject.id;
});
2023-08-31 16:37:11 +01:00
}
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) {
2023-09-26 16:13:29 +01:00
this._notificationList = this._notificationList.filter( (e) => {
2023-08-31 16:37:11 +01:00
return e.index != notification.index
})
2023-09-26 16:13:29 +01:00
this.reverse()
2023-08-31 16:37:11 +01:00
this.save()
}
}