Files
doneit-web/src/app/store/despacho-store.service.ts
T
Peter Maquiran 9208b055f2 add search
2023-05-04 09:48:04 +01:00

140 lines
3.0 KiB
TypeScript

import { Injectable } from '@angular/core';
import { localstoreService } from './localstore.service'
import { SHA1 } from 'crypto-js'
import { customTask } from '../models/dailyworktask.model';
import { ObjectQueryService } from '../services/object-query.service';
import { momentG } from 'src/plugin/momentG';
@Injectable({
providedIn: 'root'
})
export class DespachoStoreService {
// main data
private _list: customTask[] = []
// local storage keyName
private keyName: string;
private _count = 0
callbacks: {
[key: string]: {
funx: Function
id: string
}
} = {}
ObjectQueryService = new ObjectQueryService()
newList = []
constructor() {
this.keyName = (SHA1("DespachoStoreService"+ 'home/eventSource')).toString()
window['ObjectQueryService'] = this.Query()
setTimeout(()=>{
let restore = localstoreService.get(this.keyName, {})
this._list = restore.list || []
this._count = parseInt(restore.count) || 0
this.runCallback()
}, 10)
}
registerCallback({funx, id}) {
this.callbacks[id] = { funx, id}
}
runCallback() {
for (const [key, value] of Object.entries(this.callbacks)) {
value.funx()
}
}
Query() {
return {
Update: (select:Function, update:Function) => {
this.ObjectQueryService.Update(select, update, this._list )
this.save({dynamicCount : true})
},
select: (select) => {
this.ObjectQueryService.select(select, this._list )
this.save({dynamicCount : true})
},
Delete : (select) => {
this.ObjectQueryService.Delete(select, this._list )
this.save({dynamicCount : true})
},
Insert: (insert: customTask | customTask[]) =>{
this.ObjectQueryService.Insert(insert, this._list)
this.save({dynamicCount : true})
}
}
}
ObjectQuery() {}
get list(): any[] {
return this._list || []
}
get count() {
return this._list.length || 0
}
set count(value: number) {
this._count = value
this.save({})
}
reset(eventsList: any) {
this._list = eventsList
this.save({dynamicCount:true})
if(window['all-process-gabinete']) {
window['all-process-gabinete']()
}
}
private save = ({dynamicCount = false})=> {
if(dynamicCount) {
this._count = this._list.length
}
this.runCallback()
setTimeout(()=>{
localstoreService.set(this.keyName,{
list: this._list,
count: this._count
})
}, 10)
this.updateNewCount()
}
updateNewCount() {
this.newList = this._list.filter((e) =>{
return this.lessthen24Hours(e.TaskReceiveDate)
})
}
lessthen24Hours(isoDateString:string) {
if(!isoDateString) {
return false
}
const creationDate = new Date(isoDateString)
return momentG(new Date(), 'dd MMMM yyyy') == momentG(new Date(creationDate), 'dd MMMM yyyy')
}
}
export const DespachoStore = new DespachoStoreService()