load admin annoucements actions code on-demand
This commit is contained in:
parent
72f41d78ff
commit
50044efaf9
1 changed files with 53 additions and 55 deletions
|
|
@ -2,12 +2,6 @@ import { defineStore } from 'pinia'
|
||||||
|
|
||||||
import { useOAuthStore } from 'src/stores/oauth.js'
|
import { useOAuthStore } from 'src/stores/oauth.js'
|
||||||
|
|
||||||
import {
|
|
||||||
getAnnouncements as adminGetAnnouncements,
|
|
||||||
deleteAnnouncement,
|
|
||||||
editAnnouncement,
|
|
||||||
postAnnouncement,
|
|
||||||
} from 'src/api/admin.js'
|
|
||||||
import { getAnnouncements } from 'src/api/public.js'
|
import { getAnnouncements } from 'src/api/public.js'
|
||||||
import { dismissAnnouncement } from 'src/api/user.js'
|
import { dismissAnnouncement } from 'src/api/user.js'
|
||||||
|
|
||||||
|
|
@ -18,6 +12,7 @@ export const useAnnouncementsStore = defineStore('announcements', {
|
||||||
announcements: [],
|
announcements: [],
|
||||||
supportsAnnouncements: true,
|
supportsAnnouncements: true,
|
||||||
fetchAnnouncementsTimer: undefined,
|
fetchAnnouncementsTimer: undefined,
|
||||||
|
adminActions: {},
|
||||||
}),
|
}),
|
||||||
getters: {
|
getters: {
|
||||||
unreadAnnouncementCount() {
|
unreadAnnouncementCount() {
|
||||||
|
|
@ -32,34 +27,37 @@ export const useAnnouncementsStore = defineStore('announcements', {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
actions: {
|
actions: {
|
||||||
fetchAnnouncements() {
|
async fetchAnnouncements() {
|
||||||
if (!this.supportsAnnouncements) {
|
if (!this.supportsAnnouncements) return
|
||||||
return Promise.resolve()
|
|
||||||
}
|
|
||||||
|
|
||||||
const currentUser = window.vuex.state.users.currentUser
|
const currentUser = window.vuex.state.users.currentUser
|
||||||
const isAdmin =
|
const isAdmin =
|
||||||
currentUser &&
|
currentUser &&
|
||||||
currentUser.privileges.has('announcements_manage_announcements')
|
currentUser.privileges.has('announcements_manage_announcements')
|
||||||
|
|
||||||
const fetchAnnouncements = async () => {
|
try {
|
||||||
if (!isAdmin) {
|
if (isAdmin) {
|
||||||
const result = await getAnnouncements({
|
this.adminActions = await import('src/api/admin.js')
|
||||||
|
} else {
|
||||||
|
const all = await getAnnouncements({
|
||||||
credentials: useOAuthStore().token,
|
credentials: useOAuthStore().token,
|
||||||
})
|
})
|
||||||
return result.data
|
return all.data
|
||||||
}
|
}
|
||||||
|
|
||||||
const { data: all } = await adminGetAnnouncements({
|
const { data: all } = await this.adminActions.getAnnouncements({
|
||||||
credentials: useOAuthStore().token,
|
credentials: useOAuthStore().token,
|
||||||
})
|
})
|
||||||
|
|
||||||
const { data: visible } = await getAnnouncements({
|
const { data: visible } = await getAnnouncements({
|
||||||
credentials: useOAuthStore().token,
|
credentials: useOAuthStore().token,
|
||||||
})
|
})
|
||||||
|
|
||||||
const visibleObject = visible.reduce((a, c) => {
|
const visibleObject = visible.reduce((a, c) => {
|
||||||
a[c.id] = c
|
a[c.id] = c
|
||||||
return a
|
return a
|
||||||
}, {})
|
}, {})
|
||||||
|
|
||||||
const getWithinVisible = (announcement) =>
|
const getWithinVisible = (announcement) =>
|
||||||
visibleObject[announcement.id]
|
visibleObject[announcement.id]
|
||||||
|
|
||||||
|
|
@ -72,22 +70,16 @@ export const useAnnouncementsStore = defineStore('announcements', {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
return all
|
this.announcements = all
|
||||||
|
} catch (error) {
|
||||||
|
// If and only if backend does not support announcements, it would return 404.
|
||||||
|
// In this case, silently ignores it.
|
||||||
|
if (error && error.statusCode === 404) {
|
||||||
|
this.supportsAnnouncements = false
|
||||||
|
} else {
|
||||||
|
throw error
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return fetchAnnouncements()
|
|
||||||
.then((announcements) => {
|
|
||||||
this.announcements = announcements
|
|
||||||
})
|
|
||||||
.catch((error) => {
|
|
||||||
// If and only if backend does not support announcements, it would return 404.
|
|
||||||
// In this case, silently ignores it.
|
|
||||||
if (error && error.statusCode === 404) {
|
|
||||||
this.supportsAnnouncements = false
|
|
||||||
} else {
|
|
||||||
throw error
|
|
||||||
}
|
|
||||||
})
|
|
||||||
},
|
},
|
||||||
markAnnouncementAsRead(id) {
|
markAnnouncementAsRead(id) {
|
||||||
return dismissAnnouncement({
|
return dismissAnnouncement({
|
||||||
|
|
@ -122,35 +114,41 @@ export const useAnnouncementsStore = defineStore('announcements', {
|
||||||
clearInterval(interval)
|
clearInterval(interval)
|
||||||
},
|
},
|
||||||
postAnnouncement({ content, startsAt, endsAt, allDay }) {
|
postAnnouncement({ content, startsAt, endsAt, allDay }) {
|
||||||
return postAnnouncement({
|
return this.adminActions
|
||||||
credentials: useOAuthStore().token,
|
.postAnnouncement({
|
||||||
content,
|
credentials: useOAuthStore().token,
|
||||||
startsAt,
|
content,
|
||||||
endsAt,
|
startsAt,
|
||||||
allDay,
|
endsAt,
|
||||||
}).then(() => {
|
allDay,
|
||||||
return this.fetchAnnouncements()
|
})
|
||||||
})
|
.then(() => {
|
||||||
|
return this.fetchAnnouncements()
|
||||||
|
})
|
||||||
},
|
},
|
||||||
editAnnouncement({ id, content, startsAt, endsAt, allDay }) {
|
editAnnouncement({ id, content, startsAt, endsAt, allDay }) {
|
||||||
return editAnnouncement({
|
return this.adminActions
|
||||||
id,
|
.editAnnouncement({
|
||||||
content,
|
id,
|
||||||
startsAt,
|
content,
|
||||||
endsAt,
|
startsAt,
|
||||||
allDay,
|
endsAt,
|
||||||
credentials: useOAuthStore().token,
|
allDay,
|
||||||
}).then(() => {
|
credentials: useOAuthStore().token,
|
||||||
return this.fetchAnnouncements()
|
})
|
||||||
})
|
.then(() => {
|
||||||
|
return this.fetchAnnouncements()
|
||||||
|
})
|
||||||
},
|
},
|
||||||
deleteAnnouncement(id) {
|
deleteAnnouncement(id) {
|
||||||
return deleteAnnouncement({
|
return this.adminActions
|
||||||
id,
|
.deleteAnnouncement({
|
||||||
credentials: useOAuthStore().token,
|
id,
|
||||||
}).then(() => {
|
credentials: useOAuthStore().token,
|
||||||
return this.fetchAnnouncements()
|
})
|
||||||
})
|
.then(() => {
|
||||||
|
return this.fetchAnnouncements()
|
||||||
|
})
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue