2019-06-23 00:01:36 +10:00
|
|
|
import { merge } from 'lodash'
|
2019-06-20 23:02:04 +10:00
|
|
|
import { set } from 'vue'
|
|
|
|
|
|
|
|
const polls = {
|
|
|
|
state: {
|
|
|
|
// Contains key = id, value = number of trackers for this poll
|
|
|
|
trackedPolls: {},
|
|
|
|
pollsObject: {}
|
|
|
|
},
|
|
|
|
mutations: {
|
2019-06-23 00:01:36 +10:00
|
|
|
mergeOrAddPoll (state, poll) {
|
|
|
|
const existingPoll = state.pollsObject[poll.id]
|
|
|
|
// Make expired-state change trigger re-renders properly
|
|
|
|
poll.expired = Date.now() > Date.parse(poll.expires_at)
|
|
|
|
if (existingPoll) {
|
|
|
|
set(state.pollsObject, poll.id, merge(existingPoll, poll))
|
|
|
|
} else {
|
|
|
|
set(state.pollsObject, poll.id, poll)
|
|
|
|
}
|
2019-06-20 23:02:04 +10:00
|
|
|
},
|
|
|
|
trackPoll (state, pollId) {
|
|
|
|
const currentValue = state.trackedPolls[pollId]
|
|
|
|
if (currentValue) {
|
|
|
|
set(state.trackedPolls, pollId, currentValue + 1)
|
|
|
|
} else {
|
|
|
|
set(state.trackedPolls, pollId, 1)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
untrackPoll (state, pollId) {
|
|
|
|
const currentValue = state.trackedPolls[pollId]
|
|
|
|
if (currentValue) {
|
|
|
|
set(state.trackedPolls, pollId, currentValue - 1)
|
|
|
|
} else {
|
|
|
|
set(state.trackedPolls, pollId, 0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
actions: {
|
2019-06-23 00:01:36 +10:00
|
|
|
mergeOrAddPoll ({ commit }, poll) {
|
|
|
|
commit('mergeOrAddPoll', poll)
|
2019-06-20 23:02:04 +10:00
|
|
|
},
|
|
|
|
updateTrackedPoll ({ rootState, dispatch, commit }, pollId) {
|
2019-11-24 22:57:46 +11:00
|
|
|
rootState.api.backendInteractor.fetchPoll({ pollId }).then(poll => {
|
2019-06-20 23:02:04 +10:00
|
|
|
setTimeout(() => {
|
|
|
|
if (rootState.polls.trackedPolls[pollId]) {
|
|
|
|
dispatch('updateTrackedPoll', pollId)
|
|
|
|
}
|
|
|
|
}, 30 * 1000)
|
2019-06-23 00:01:36 +10:00
|
|
|
commit('mergeOrAddPoll', poll)
|
2019-06-20 23:02:04 +10:00
|
|
|
})
|
|
|
|
},
|
|
|
|
trackPoll ({ rootState, commit, dispatch }, pollId) {
|
|
|
|
if (!rootState.polls.trackedPolls[pollId]) {
|
|
|
|
setTimeout(() => dispatch('updateTrackedPoll', pollId), 30 * 1000)
|
|
|
|
}
|
|
|
|
commit('trackPoll', pollId)
|
|
|
|
},
|
|
|
|
untrackPoll ({ commit }, pollId) {
|
|
|
|
commit('untrackPoll', pollId)
|
|
|
|
},
|
|
|
|
votePoll ({ rootState, commit }, { id, pollId, choices }) {
|
2019-11-24 22:57:46 +11:00
|
|
|
return rootState.api.backendInteractor.vote({ pollId, choices }).then(poll => {
|
2019-06-23 00:01:36 +10:00
|
|
|
commit('mergeOrAddPoll', poll)
|
2019-06-20 23:02:04 +10:00
|
|
|
return poll
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default polls
|