34 lines
1.1 KiB
TypeScript
34 lines
1.1 KiB
TypeScript
import { defineStore } from 'pinia';
|
|
import { ref } from 'vue';
|
|
import { Notification } from '@/Types/types';
|
|
import axios from 'axios';
|
|
|
|
export const useNotificationStore = defineStore('notifications', () => {
|
|
const toasts = ref<Notification[]>([]);
|
|
const unreadCount = ref(0);
|
|
const latestNotification = ref<Notification | null>(null);
|
|
const achievementSound = new Audio('/sounds/seatBelt.wav');
|
|
|
|
function push(notification: Notification) {
|
|
toasts.value.push(notification);
|
|
latestNotification.value = notification;
|
|
unreadCount.value++;
|
|
|
|
|
|
achievementSound.play().catch(() => {});
|
|
|
|
}
|
|
|
|
async function dismiss(notification: Notification) {
|
|
toasts.value = toasts.value.filter(n => n.id !== notification.id);
|
|
await axios.patch(`/notifications/${notification.id}/read`);
|
|
unreadCount.value = Math.max(0, unreadCount.value - 1);
|
|
}
|
|
|
|
function setInitialUnreadCount(count: number) {
|
|
unreadCount.value = count;
|
|
}
|
|
|
|
return { toasts, unreadCount, latestNotification, push, dismiss, setInitialUnreadCount};
|
|
});
|