transporter-app/store/notificationStore.js

28 lines
709 B
JavaScript
Raw Normal View History

2024-06-26 23:24:26 +03:00
import {proxy} from "valtio";
import Database from '../db'
import Api from "../services/api";
const store = proxy({
notifications: [],
newNotificationBadge: 0
})
const getNotificationsFromDB = () => {
store.notifications.length = 0
store.notifications.push(
...Database.getAllKeys()
.filter(key => key.startsWith('notification.'))
.map(key => JSON.parse(Database.getString(key)))
)
}
export const getNotifications = () => {
Api.getNotifications()
.then(() => getNotificationsFromDB())
.catch(() => getNotificationsFromDB())
}
export const increaseNewNotificationBadge = () => {
store.newNotificationBadge++
}
export default store