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 }) {
|
|
|
|
if (!state.announcements[id]) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
set(state.announcements[id], 'read', read)
|
|
|
|
},
|
|
|
|
setFetchAnnouncementsTimer (state, timer) {
|
|
|
|
set(state, 'fetchAnnouncementsTimer', announcements)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const announcements = {
|
|
|
|
state: defaultState,
|
|
|
|
mutations,
|
|
|
|
actions: {
|
|
|
|
fetchAnnouncements (store) {
|
2022-03-17 13:51:32 -07:00
|
|
|
const currentUser = store.rootState.users.currentUser
|
|
|
|
const isAdmin = currentUser && currentUser.role === 'admin'
|
|
|
|
|
|
|
|
return (isAdmin
|
|
|
|
? store.rootState.api.backendInteractor.adminFetchAnnouncements()
|
|
|
|
: store.rootState.api.backendInteractor.fetchAnnouncements())
|
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
|
|
|
},
|
|
|
|
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
|