Fix: problems with polls state

This commit is contained in:
Shpuld Shpludson 2019-06-22 14:01:36 +00:00
parent c2b48c32a2
commit 947f69a953
3 changed files with 24 additions and 21 deletions

View file

@ -3,7 +3,7 @@ import { forEach, map } from 'lodash'
export default { export default {
name: 'Poll', name: 'Poll',
props: ['pollId'], props: ['basePoll'],
components: { Timeago }, components: { Timeago },
data () { data () {
return { return {
@ -11,13 +11,19 @@ export default {
choices: [] choices: []
} }
}, },
mounted () { created () {
if (!this.$store.state.polls.pollsObject[this.pollId]) {
this.$store.dispatch('mergeOrAddPoll', this.basePoll)
}
this.$store.dispatch('trackPoll', this.pollId) this.$store.dispatch('trackPoll', this.pollId)
}, },
destroyed () { destroyed () {
this.$store.dispatch('untrackPoll', this.pollId) this.$store.dispatch('untrackPoll', this.pollId)
}, },
computed: { computed: {
pollId () {
return this.basePoll.id
},
poll () { poll () {
const storePoll = this.$store.state.polls.pollsObject[this.pollId] const storePoll = this.$store.state.polls.pollsObject[this.pollId]
return storePoll || {} return storePoll || {}
@ -29,7 +35,7 @@ export default {
return (this.poll && this.poll.expires_at) || 0 return (this.poll && this.poll.expires_at) || 0
}, },
expired () { expired () {
return Date.now() > Date.parse(this.expiresAt) return (this.poll && this.poll.expired) || false
}, },
loggedIn () { loggedIn () {
return this.$store.state.users.currentUser return this.$store.state.users.currentUser

View file

@ -124,7 +124,7 @@
</div> </div>
<div v-if="status.poll && status.poll.options"> <div v-if="status.poll && status.poll.options">
<poll :poll-id="status.poll.id" /> <poll :base-poll="status.poll" />
</div> </div>
<div v-if="status.attachments && (!hideSubjectStatus || showingLongSubject)" class="attachments media-body"> <div v-if="status.attachments && (!hideSubjectStatus || showingLongSubject)" class="attachments media-body">

View file

@ -1,4 +1,4 @@
import { each, merge } from 'lodash' import { merge } from 'lodash'
import { set } from 'vue' import { set } from 'vue'
const polls = { const polls = {
@ -8,15 +8,15 @@ const polls = {
pollsObject: {} pollsObject: {}
}, },
mutations: { mutations: {
addNewStatuses (state, { statuses }) { mergeOrAddPoll (state, poll) {
each(statuses, status => { const existingPoll = state.pollsObject[poll.id]
if (status.poll) { // Make expired-state change trigger re-renders properly
set(state.pollsObject, status.poll.id, status.poll) poll.expired = Date.now() > Date.parse(poll.expires_at)
} if (existingPoll) {
}) set(state.pollsObject, poll.id, merge(existingPoll, poll))
}, } else {
mergePoll (state, poll) { set(state.pollsObject, poll.id, poll)
state.pollsObject[poll.id] = merge(state.pollsObject[poll.id], poll) }
}, },
trackPoll (state, pollId) { trackPoll (state, pollId) {
const currentValue = state.trackedPolls[pollId] const currentValue = state.trackedPolls[pollId]
@ -36,11 +36,8 @@ const polls = {
} }
}, },
actions: { actions: {
updatePoll ({ rootState, commit }, pollId) { mergeOrAddPoll ({ commit }, poll) {
return rootState.api.backendInteractor.fetchPoll(pollId).then(poll => { commit('mergeOrAddPoll', poll)
commit('mergePoll', poll)
return poll
})
}, },
updateTrackedPoll ({ rootState, dispatch, commit }, pollId) { updateTrackedPoll ({ rootState, dispatch, commit }, pollId) {
rootState.api.backendInteractor.fetchPoll(pollId).then(poll => { rootState.api.backendInteractor.fetchPoll(pollId).then(poll => {
@ -49,7 +46,7 @@ const polls = {
dispatch('updateTrackedPoll', pollId) dispatch('updateTrackedPoll', pollId)
} }
}, 30 * 1000) }, 30 * 1000)
commit('mergePoll', poll) commit('mergeOrAddPoll', poll)
}) })
}, },
trackPoll ({ rootState, commit, dispatch }, pollId) { trackPoll ({ rootState, commit, dispatch }, pollId) {
@ -63,7 +60,7 @@ const polls = {
}, },
votePoll ({ rootState, commit }, { id, pollId, choices }) { votePoll ({ rootState, commit }, { id, pollId, choices }) {
return rootState.api.backendInteractor.vote(pollId, choices).then(poll => { return rootState.api.backendInteractor.vote(pollId, choices).then(poll => {
commit('mergePoll', poll) commit('mergeOrAddPoll', poll)
return poll return poll
}) })
} }