yandere_fe/src/modules/announcements.js

119 lines
3.5 KiB
JavaScript
Raw Normal View History

2022-03-17 11:01:45 -07:00
import { set } from 'vue'
const FETCH_ANNOUNCEMENT_INTERVAL_MS = 1000 * 60 * 5
export const defaultState = {
announcements: [],
fetchAnnouncementsTimer: undefined
}
export const mutations = {
setAnnouncements (state, announcements) {
set(state, 'announcements', announcements)
},
setAnnouncementRead (state, { id, read }) {
const index = state.announcements.findIndex(a => a.id === id)
if (index < 0) {
2022-03-17 11:01:45 -07:00
return
}
set(state.announcements[index], 'read', read)
2022-03-17 11:01:45 -07:00
},
setFetchAnnouncementsTimer (state, timer) {
set(state, 'fetchAnnouncementsTimer', announcements)
}
}
2022-03-17 14:51:39 -07:00
export const getters = {
unreadAnnouncementCount (state, _getters, rootState) {
if (!rootState.users.currentUser) {
return 0
}
2022-03-20 10:26:57 -07:00
const unread = state.announcements.filter(announcement => !(announcement.inactive || announcement.read))
return unread.length
2022-03-17 14:51:39 -07:00
}
}
2022-03-17 11:01:45 -07:00
const announcements = {
state: defaultState,
mutations,
2022-03-17 14:51:39 -07:00
getters,
2022-03-17 11:01:45 -07:00
actions: {
fetchAnnouncements (store) {
2022-03-17 13:51:32 -07:00
const currentUser = store.rootState.users.currentUser
const isAdmin = currentUser && currentUser.role === 'admin'
const getAnnouncements = async () => {
if (!isAdmin) {
return store.rootState.api.backendInteractor.fetchAnnouncements()
}
const all = await store.rootState.api.backendInteractor.adminFetchAnnouncements()
const visible = await store.rootState.api.backendInteractor.fetchAnnouncements()
const visibleObject = visible.reduce((a, c) => {
a[c.id] = c
return a
}, {})
all.forEach(announcement => {
if (!visibleObject[announcement.id]) {
announcement.inactive = true
} else {
announcement.read = visibleObject[announcement.id].read
}
})
return all
}
return getAnnouncements()
2022-03-17 11:01:45 -07:00
.then(announcements => {
store.commit('setAnnouncements', announcements)
})
},
markAnnouncementAsRead (store, id) {
return store.rootState.api.backendInteractor.dismissAnnouncement({ id })
.then(() => {
store.commit('setAnnouncementRead', { id, read: true })
})
},
startFetchingAnnouncements (store) {
if (store.state.fetchAnnouncementsTimer) {
return
}
const interval = setInterval(() => store.dispatch('fetchAnnouncements'), FETCH_ANNOUNCEMENT_INTERVAL_MS)
store.commit('setFetchAnnouncementsTimer', interval)
return store.dispatch('fetchAnnouncements')
},
stopFetchingAnnouncements (store) {
const interval = store.state.fetchAnnouncementsTimer
store.commit('setFetchAnnouncementsTimer', undefined)
clearInterval(interval)
2022-03-17 11:54:52 -07:00
},
postAnnouncement (store, { content, startsAt, endsAt, allDay }) {
return store.rootState.api.backendInteractor.postAnnouncement({ content, startsAt, endsAt, allDay })
.then(() => {
return store.dispatch('fetchAnnouncements')
})
2022-03-17 12:07:04 -07:00
},
2022-03-17 14:11:53 -07:00
editAnnouncement (store, { id, content, startsAt, endsAt, allDay }) {
return store.rootState.api.backendInteractor.editAnnouncement({ id, content, startsAt, endsAt, allDay })
.then(() => {
return store.dispatch('fetchAnnouncements')
})
},
2022-03-17 12:07:04 -07:00
deleteAnnouncement (store, id) {
return store.rootState.api.backendInteractor.deleteAnnouncement({ id })
.then(() => {
return store.dispatch('fetchAnnouncements')
})
2022-03-17 11:01:45 -07:00
}
}
}
export default announcements