tornado-initiation-ui/store/notice.js

81 lines
2.1 KiB
JavaScript
Raw Normal View History

2020-11-03 08:35:46 +01:00
const NOTICE_INTERVAL = 10000
export const state = () => {
return {
notices: [],
timers: {},
}
}
export const mutations = {
ADD_NOTICE(state, notice) {
state.notices.push(notice)
},
UPDATE_NOTICE(state, { index, notice }) {
this._vm.$set(state.notices, index, notice)
},
DELETE_NOTICE(state, index) {
this._vm.$delete(state.notices, index)
},
ADD_NOTICE_TIMER(state, { id, timerId }) {
this._vm.$set(state.timers, id, { timerId })
},
DELETE_NOTICE_TIMER(state, id) {
this._vm.$delete(state.timers, id)
},
}
export const actions = {
addNotice({ commit }, { notice }) {
return new Promise((resolve) => {
const id = `f${(+new Date()).toString(16)}`
commit('ADD_NOTICE', { ...notice, id })
resolve(id)
})
},
2020-11-11 01:51:41 +01:00
addNoticeTimer({ commit, dispatch }, { id, interval = NOTICE_INTERVAL }) {
2020-11-03 08:35:46 +01:00
const timerId = setTimeout(() => {
dispatch('deleteNotice', { id })
}, interval)
commit('ADD_NOTICE_TIMER', { id, timerId })
},
deleteNoticeTimer({ state, commit }, { id }) {
if (state.timers[id]) {
clearTimeout(state.timers[id].timerId)
commit('DELETE_NOTICE_TIMER', id)
}
},
addNoticeWithInterval({ dispatch }, { notice, interval }) {
2020-11-19 11:25:44 +01:00
// eslint-disable-next-line no-async-promise-executor
return new Promise(async (resolve) => {
const id = await dispatch('addNotice', { notice })
dispatch('addNoticeTimer', { id, interval })
resolve(id)
2020-11-03 08:35:46 +01:00
})
},
deleteNotice({ state, commit, dispatch }, { id }) {
const index = state.notices.findIndex((i) => {
return i.id === id
})
commit('DELETE_NOTICE', index)
dispatch('deleteNoticeTimer', { id })
},
updateNotice({ state, commit, dispatch }, { id, notice, interval }) {
const { notices } = state
const index = notices.findIndex((i) => {
return i.id === id
})
commit('UPDATE_NOTICE', {
index,
notice: {
...notices[index],
...notice,
},
})
if (interval) {
dispatch('deleteNoticeTimer', { id })
dispatch('addNoticeTimer', { id, interval })
}
},
}