diff options
Diffstat (limited to 'src/components/poll')
| -rw-r--r-- | src/components/poll/poll.js | 39 | ||||
| -rw-r--r-- | src/components/poll/poll.vue | 16 |
2 files changed, 32 insertions, 23 deletions
diff --git a/src/components/poll/poll.js b/src/components/poll/poll.js index ecacbc35..98db5582 100644 --- a/src/components/poll/poll.js +++ b/src/components/poll/poll.js @@ -3,26 +3,39 @@ import { forEach, map } from 'lodash' export default { name: 'Poll', - props: ['poll', 'statusId'], + props: ['basePoll'], components: { Timeago }, data () { return { loading: false, - choices: [], - refreshInterval: null + choices: [] } }, created () { - this.refreshInterval = setTimeout(this.refreshPoll, 30 * 1000) - // Initialize choices to booleans and set its length to match options - this.choices = this.poll.options.map(_ => false) + if (!this.$store.state.polls.pollsObject[this.pollId]) { + this.$store.dispatch('mergeOrAddPoll', this.basePoll) + } + this.$store.dispatch('trackPoll', this.pollId) }, destroyed () { - clearTimeout(this.refreshInterval) + this.$store.dispatch('untrackPoll', this.pollId) }, computed: { + pollId () { + return this.basePoll.id + }, + poll () { + const storePoll = this.$store.state.polls.pollsObject[this.pollId] + return storePoll || {} + }, + options () { + return (this.poll && this.poll.options) || [] + }, + expiresAt () { + return (this.poll && this.poll.expires_at) || 0 + }, expired () { - return Date.now() > Date.parse(this.poll.expires_at) + return (this.poll && this.poll.expired) || false }, loggedIn () { return this.$store.state.users.currentUser @@ -33,9 +46,6 @@ export default { totalVotesCount () { return this.poll.votes_count }, - expiresAt () { - return Date.parse(this.poll.expires_at).toLocaleString() - }, containerClass () { return { loading: this.loading @@ -55,11 +65,6 @@ export default { } }, methods: { - refreshPoll () { - if (this.expired) return - this.fetchPoll() - this.refreshInterval = setTimeout(this.refreshPoll, 30 * 1000) - }, percentageForOption (count) { return this.totalVotesCount === 0 ? 0 : Math.round(count / this.totalVotesCount * 100) }, @@ -104,4 +109,4 @@ export default { }) } } -}
\ No newline at end of file +} diff --git a/src/components/poll/poll.vue b/src/components/poll/poll.vue index 28e9f4a8..bb67101a 100644 --- a/src/components/poll/poll.vue +++ b/src/components/poll/poll.vue @@ -2,7 +2,7 @@ <div class="poll" v-bind:class="containerClass"> <div class="poll-option" - v-for="(option, index) in poll.options" + v-for="(option, index) in options" :key="index" > <div v-if="showResults" :title="resultTitle(option)" class="option-result"> @@ -31,8 +31,8 @@ :disabled="loading" :value="index" > - <label> - {{option.title}} + <label class="option-vote"> + <div>{{option.title}}</div> </label> </div> </div> @@ -50,7 +50,7 @@ {{totalVotesCount}} {{ $t("polls.votes") }} ยท </div> <i18n :path="expired ? 'polls.expired' : 'polls.expires_in'"> - <Timeago :time="this.poll.expires_at" :auto-update="60" :now-threshold="0" /> + <Timeago :time="this.expiresAt" :auto-update="60" :now-threshold="0" /> </i18n> </div> </div> @@ -68,8 +68,7 @@ margin: 0 0 0.5em; } .poll-option { - margin: 0.5em 0; - height: 1.5em; + margin: 0.75em 0.5em; } .option-result { height: 100%; @@ -87,6 +86,7 @@ } .result-percentage { width: 3.5em; + flex-shrink: 0; } .result-fill { height: 100%; @@ -99,6 +99,10 @@ left: 0; transition: width 0.5s; } + .option-vote { + display: flex; + align-items: center; + } input { width: 3.5em; } |
