Files
myprojplanet_vite/src/store/NotifStore.ts

161 lines
4.2 KiB
TypeScript
Raw Normal View History

2022-07-10 01:24:54 +02:00
import { defineStore } from 'pinia'
import { Api } from '@api'
import { serv_constants } from './Modules/serv_constants'
import { INotif, INotifState } from '../model'
import { tools } from '@src/store/Modules/tools'
import { NotifDefault } from '@src/model'
import { shared_consts } from '@src/common/shared_vuejs'
import { useUserStore } from '@store/UserStore'
export const useNotifStore = defineStore('NotifStore', {
state: (): INotifState => ({
last_notifs: [],
2022-07-21 00:20:48 +02:00
show_all: true
2022-07-10 01:24:54 +02:00
}),
getters: {
getlasts_notifs: (mystate: INotifState) => (): INotif[] => {
const ctrec = (mystate.last_notifs) ? mystate.last_notifs.slice(0, 20).filter((rec) => mystate.show_all ? true : !rec.read) : []
2022-07-10 01:24:54 +02:00
return (ctrec)
},
getnumNotifUnread: (mystate: INotifState) => () => {
2022-07-21 00:20:48 +02:00
const myarr = mystate.last_notifs.filter((notif) => !notif.read)
return (tools.isArray(myarr) ? myarr.length : 0)
2022-07-10 01:24:54 +02:00
},
},
actions: {
setNotif(notif: INotif) {
console.log('setNotif', notif)
2022-07-10 01:24:54 +02:00
if (notif) {
this.last_notifs = [notif, ...this.last_notifs]
}
},
2022-07-21 00:20:48 +02:00
setAsRead(idnotif: string) {
const rec = this.last_notifs.find((rec: any) => rec._id === idnotif)
if (rec) {
rec.read = true
}
},
setAllRead(username: string) {
return Api.SendReq(`/sendnotif/setall/${username}/${process.env.APP_ID}`, 'GET', null)
.then((res) => {
// console.log('res', res)
if (res) {
for (const rec of this.last_notifs) {
rec.read = true
}
}
})
.catch((error) => {
console.error(error)
return false
})
},
deleteRec(username: string, id: string) {
return Api.SendReq(`/sendnotif/del/${username}/${id}/${process.env.APP_ID}`, 'GET', null)
.then((res) => {
// console.log('res', res)
if (res) {
this.last_notifs = this.last_notifs.filter((rec) => rec._id !== id)
}
})
.catch((error) => {
console.error(error)
return false
})
2022-07-21 00:20:48 +02:00
},
deleteAll(username: string, id: string) {
return Api.SendReq(`/sendnotif/delall/${username}/${process.env.APP_ID}`, 'GET', null)
.then((res) => {
// console.log('res', res)
if (res) {
this.last_notifs = []
}
})
.catch((error) => {
console.error(error)
return false
})
2022-07-21 00:20:48 +02:00
},
deactivateRec(id: string) {
return ''
2022-07-21 00:20:48 +02:00
},
2022-07-10 01:24:54 +02:00
async updateNotifDataFromServer({ username, lastdataread }: {username: string, lastdataread: Date}) {
// console.log('updateNotifDataFromServer', username, lastdataread)
return Api.SendReq(`/sendnotif/${username}/${lastdataread}/${process.env.APP_ID}`, 'GET', null)
.then((res) => {
// console.log('res', res)
2022-07-21 00:20:48 +02:00
if (!!res.data && !!res.data.arrnotif) {
this.last_notifs = res.data.arrnotif
} else {
this.last_notifs = []
}
2022-07-10 01:24:54 +02:00
return true
})
.catch((error) => {
console.error(error)
return false
})
},
async SendNotifEvent(notif: INotif) {
console.log('SendNotifEvent', notif)
const userStore = useUserStore()
2022-07-10 01:24:54 +02:00
const data: INotif = { ...NotifDefault, ...notif }
data.idapp = process.env.APP_ID
data.typedir = notif.typedir
data.typeid = notif.typeid
2022-07-10 01:24:54 +02:00
data.sender = notif.sender
data.dest = notif.dest
data.title = notif.title
2022-07-10 01:24:54 +02:00
data.descr = notif.descr
data.datenotif = tools.getDateNow()
data.read = false
// console.log('DOPO:')
// console.table(data)
return Api.SendReq('/sendnotif', 'POST', data)
.then((res) => {
console.log('res', res)
2022-07-10 01:24:54 +02:00
if (res.status === 200) {
if (res.data.code === serv_constants.RIS_CODE_OK && res.data.record) {
if (res.data.record.dest === userStore.my.username) {
this.setNotif(res.data.record)
}
2022-07-10 01:24:54 +02:00
return true
}
}
return false
})
.catch((error) => {
console.error(error)
return false
})
},
},
})