Refresh poll

This commit is contained in:
Maxim Filippov 2019-04-09 23:02:48 +02:00
parent d661be99c2
commit 9571770e32
5 changed files with 33 additions and 9 deletions

View File

@ -1,9 +1,12 @@
<template>
<poll-results v-if="currentUserHasVoted" :poll="poll" />
<poll-results
v-if="currentUserHasVoted"
:poll="poll"
v-on:poll-refreshed="handlePollUpdate" />
<poll-vote
v-else
:poll="poll"
v-on:user-has-voted="onUserVote" />
v-on:user-has-voted="handlePollUpdate" />
</template>
<script>
@ -23,7 +26,7 @@ export default {
}
},
methods: {
onUserVote (poll) {
handlePollUpdate (poll) {
this.poll = poll
}
}

View File

@ -12,7 +12,7 @@
</div>
<footer>
<div class="refresh">
<a href="#">Refresh</a>&nbsp;·&nbsp;
<a href="#" @click="fetchPoll(poll.id)">Refresh</a>&nbsp;·&nbsp;
</div>
<div class="total">
{{totalVotesCount}} {{ $t("polls.votes") }}
@ -31,8 +31,12 @@ export default {
}
},
methods: {
percentageForOption: function (count) {
percentageForOption (count) {
return (this.totalVotesCount === 0 ? 0 : (count / this.totalVotesCount * 100)).toFixed(1)
},
async fetchPoll (pollID) {
const poll = await this.$store.state.api.backendInteractor.fetchPoll(pollID)
this.$emit('poll-refreshed', poll)
}
}
}

View File

@ -110,7 +110,7 @@
<a v-if="showingMore" href="#" class="status-unhider" @click.prevent="toggleShowMore">{{$t("general.show_less")}}</a>
</div>
<div v-if="status.poll.votes">
<div v-if="status.poll && status.poll.votes">
<poll :poll="status.poll" />
</div>

View File

@ -46,6 +46,7 @@ const MASTODON_UNMUTE_USER_URL = id => `/api/v1/accounts/${id}/unmute`
const MASTODON_POST_STATUS_URL = '/api/v1/statuses'
const MASTODON_MEDIA_UPLOAD_URL = '/api/v1/media'
const MASTODON_VOTE_URL = '/api/v1/polls/vote'
const MASTODON_POLL_URL = id => `/api/v1/polls/${id}`
import { each, map } from 'lodash'
import { parseStatus, parseUser, parseNotification, parseAttachment } from '../entity_normalizer/entity_normalizer.service.js'
@ -658,6 +659,16 @@ const vote = ({pollID, optionName, credentials}) => {
)
}
const fetchPoll = ({pollID, credentials}) => {
return promisedRequest(
MASTODON_POLL_URL(encodeURIComponent(pollID)),
{
method: 'GET',
headers: authHeaders(credentials)
}
)
}
const apiService = {
verifyCredentials,
fetchTimeline,
@ -701,7 +712,8 @@ const apiService = {
denyUser,
suggestions,
markNotificationsAsSeen,
vote
vote,
fetchPoll
}
export default apiService

View File

@ -59,7 +59,11 @@ const backendInteractorService = (credentials) => {
}
const vote = (pollID, optionName) => {
return apiService.vote({pollID, optionName})
return apiService.vote({credentials, pollID, optionName})
}
const fetchPoll = (pollID) => {
return apiService.fetchPoll({credentials, pollID})
}
const startFetching = ({timeline, store, userId = false, tag}) => {
@ -121,7 +125,8 @@ const backendInteractorService = (credentials) => {
fetchFollowRequests,
approveUser,
denyUser,
vote
vote,
fetchPoll
}
return backendInteractorServiceInstance