70 lines
1.9 KiB
TypeScript
70 lines
1.9 KiB
TypeScript
import {acceptHMRUpdate, defineStore} from 'pinia'
|
|
import {computed, ref} from 'vue'
|
|
import {tryOnUnmounted} from '@vueuse/core'
|
|
|
|
import type {INotification} from '@/modelTypes/INotification'
|
|
import NotificationService from '@/services/notification'
|
|
import {findIndexById} from '@/helpers/utils'
|
|
|
|
const NOTIFICATIONS_PULL_INTERVAL = 10000
|
|
|
|
export const useNotificationStore = defineStore('notification', () => {
|
|
const allNotifications = ref<INotification[]>([])
|
|
|
|
const notifications = computed(() => {
|
|
return allNotifications.value ? allNotifications.value.filter(n => n.name !== '') : []
|
|
})
|
|
const unreadNotifications = computed(() => {
|
|
return notifications.value.filter(n => n.readAt === null).length
|
|
})
|
|
const hasUnreadNotifications = computed(() => unreadNotifications.value > 0)
|
|
|
|
let interval: ReturnType<typeof setInterval>
|
|
|
|
const notificationService = new NotificationService()
|
|
|
|
async function loadNotifications() {
|
|
allNotifications.value = await notificationService.getAll() as INotification[]
|
|
}
|
|
|
|
function startNotificationPulling() {
|
|
loadNotifications()
|
|
interval = setInterval(loadNotifications, NOTIFICATIONS_PULL_INTERVAL)
|
|
return stopNotificationPulling
|
|
}
|
|
|
|
function stopNotificationPulling() {
|
|
clearInterval(interval)
|
|
}
|
|
|
|
async function markNotificationAsRead(notificationItem: INotification): Promise<INotification | undefined> {
|
|
const index = findIndexById(allNotifications.value, notificationItem.id)
|
|
if (index === -1) {
|
|
return
|
|
}
|
|
|
|
allNotifications.value[index] = {
|
|
...notificationItem,
|
|
read: true,
|
|
}
|
|
|
|
await notificationService.update(allNotifications.value[index])
|
|
}
|
|
|
|
tryOnUnmounted(stopNotificationPulling)
|
|
|
|
return {
|
|
notifications,
|
|
unreadNotifications,
|
|
hasUnreadNotifications,
|
|
|
|
startNotificationPulling,
|
|
stopNotificationPulling,
|
|
markNotificationAsRead,
|
|
}
|
|
})
|
|
|
|
// support hot reloading
|
|
if (import.meta.hot) {
|
|
import.meta.hot.accept(acceptHMRUpdate(useNotificationStore, import.meta.hot))
|
|
} |