fix refreshing

This commit is contained in:
shpuld 2019-06-06 07:33:00 +03:00
parent 080921e977
commit 70ba232ce3
6 changed files with 37 additions and 34 deletions

View File

@ -2,6 +2,7 @@
<poll-results
v-if="currentUserHasVoted"
:poll="poll"
:status-id="statusId"
v-on:poll-refreshed="handlePollUpdate"
/>
<poll-vote

View File

@ -12,7 +12,7 @@
</div>
<footer>
<div class="refresh">
<a href="#" @click="fetchPoll(poll.id)">Refresh</a>&nbsp;·&nbsp;
<a href="#" @click.stop.prevent="fetchPoll(poll.id)">Refresh</a>&nbsp;·&nbsp;
</div>
<div class="total">
{{totalVotesCount}} {{ $t("polls.votes") }}
@ -24,7 +24,7 @@
<script>
export default {
name: 'PollResults',
props: ['poll'],
props: ['poll', 'statusId'],
created () {
console.log(this.poll)
},
@ -37,10 +37,8 @@ export default {
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)
console.log(poll)
this.$emit('poll-refreshed', poll)
fetchPoll () {
this.$store.dispatch('refreshPoll', { id: this.statusId, pollId: this.poll.id })
}
}
}

View File

@ -1,21 +1,24 @@
<template>
<div class="poll-vote" v-bind:class="containerClass">
<form class="poll-vote" v-bind:class="containerClass">
<div
class="poll-choice"
v-for="(pollOption, index) in poll.options"
:key="index">
:key="index"
>
<input
:disabled="loading"
type="checkbox"
:id="optionID(index)"
:value="pollOption.title"
name="choice"
:id="index"
:disabled="loading"
:value="pollOption.title"
v-model="checks[index]"
>
<label :for="optionID(index)">{{pollOption.title}}</label>
</div>
<button class="btn btn-default poll-vote-button" @click="onVote">{{$t('polls.vote')}}</button>
<label :for="index">
{{pollOption.title}}
</label>
</div>
<button class="btn btn-default poll-vote-button" type="button" @click="vote">{{$t('polls.vote')}}</button>
</form>
</template>
<script>
@ -36,15 +39,11 @@ export default {
}
},
methods: {
optionID (index) {
return `pollOption${this.poll.id}#${index}`
},
onVote () {
vote () {
this.loading = true
const choices = this.checks.map((entry, index) => index).filter(value => typeof value === 'number')
this.$store.dispatch('votePoll', { id: this.statusId, pollId: this.poll.id, choices }).then(poll => {
console.log('vote result:', poll)
this.loading = false
})
}

View File

@ -588,6 +588,12 @@ const statuses = {
commit('updateStatusWithPoll', { id, poll })
return poll
})
},
refreshPoll ({ rootState, commit }, { id, pollId }) {
return rootState.api.backendInteractor.fetchPoll(pollId).then(poll => {
commit('updateStatusWithPoll', { id, poll })
return poll
})
}
},
mutations

View File

@ -737,13 +737,12 @@ const markNotificationsAsSeen = ({ id, credentials }) => {
}).then((data) => data.json())
}
const vote = ({ pollID, choices, credentials }) => {
const vote = ({ pollId, choices, credentials }) => {
const form = new FormData()
form.append('choices', choices)
const url = MASTODON_VOTE_URL(encodeURIComponent(pollID))
return promisedRequest({
url: MASTODON_VOTE_URL(encodeURIComponent(pollID)),
url: MASTODON_VOTE_URL(encodeURIComponent(pollId)),
method: 'POST',
credentials,
payload: {
@ -752,12 +751,12 @@ const vote = ({ pollID, choices, credentials }) => {
})
}
const fetchPoll = ({ pollID, credentials }) => {
const fetchPoll = ({ pollId, credentials }) => {
return promisedRequest(
MASTODON_POLL_URL(encodeURIComponent(pollID)),
{
url: MASTODON_POLL_URL(encodeURIComponent(pollId)),
method: 'GET',
headers: authHeaders(credentials)
credentials
}
)
}

View File

@ -87,12 +87,12 @@ const backendInteractorService = credentials => {
return apiService.deleteUser({ screen_name, credentials })
}
const vote = (pollID, choices) => {
return apiService.vote({ credentials, pollID, choices })
const vote = (pollId, choices) => {
return apiService.vote({ credentials, pollId, choices })
}
const fetchPoll = (pollID) => {
return apiService.fetchPoll({ credentials, pollID })
const fetchPoll = (pollId) => {
return apiService.fetchPoll({ credentials, pollId })
}
const updateNotificationSettings = ({ settings }) => {