Added sockets for notifications
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
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++;
|
||||
|
||||
if (notification.is_achievement) {
|
||||
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};
|
||||
});
|
||||
Reference in New Issue
Block a user