make poll voting update status, making it reactive, fix multiple choice voting

This commit is contained in:
shpuld 2019-06-05 22:19:13 +03:00
parent 82802c6a8d
commit 080921e977
5 changed files with 28 additions and 15 deletions

View File

@ -2,11 +2,14 @@
<poll-results <poll-results
v-if="currentUserHasVoted" v-if="currentUserHasVoted"
:poll="poll" :poll="poll"
v-on:poll-refreshed="handlePollUpdate" /> v-on:poll-refreshed="handlePollUpdate"
/>
<poll-vote <poll-vote
v-else v-else
:poll="poll" :poll="poll"
v-on:user-has-voted="handlePollUpdate" /> :status-id="statusId"
v-on:user-has-voted="handlePollUpdate"
/>
</template> </template>
<script> <script>
@ -15,13 +18,17 @@ import PollVote from './poll_vote/poll_vote.vue'
export default { export default {
name: 'Poll', name: 'Poll',
props: ['poll'], props: ['poll', 'statusId'],
components: { components: {
PollResults, PollResults,
PollVote PollVote
}, },
computed: { computed: {
currentUserHasVoted () { currentUserHasVoted () {
console.log('currentUserHasVoted poll', this.poll)
return this.poll.voted
},
voted () {
return this.poll.voted return this.poll.voted
} }
}, },

View File

@ -21,7 +21,7 @@
<script> <script>
export default { export default {
name: 'PollVote', name: 'PollVote',
props: ['poll'], props: ['poll', 'statusId'],
data () { data () {
return { return {
loading: false, loading: false,
@ -39,14 +39,14 @@ export default {
optionID (index) { optionID (index) {
return `pollOption${this.poll.id}#${index}` return `pollOption${this.poll.id}#${index}`
}, },
async onVote () { onVote () {
this.loading = true this.loading = true
const choices = this.checks.filter(_=>_).map((entry, index) => index) const choices = this.checks.map((entry, index) => index).filter(value => typeof value === 'number')
const poll = await this.$store.state.api.backendInteractor.vote(this.poll.id, choices) this.$store.dispatch('votePoll', { id: this.statusId, pollId: this.poll.id, choices }).then(poll => {
console.log('vote result:', poll)
this.loading = false this.loading = false
this.$emit('user-has-voted', poll) })
} }
} }
} }

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="status.poll" /> <poll :poll="status.poll" :status-id="status.id" />
</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

@ -494,6 +494,10 @@ export const mutations = {
const newStatus = state.allStatusesObject[id] const newStatus = state.allStatusesObject[id]
newStatus.favoritedBy = favoritedByUsers.filter(_ => _) newStatus.favoritedBy = favoritedByUsers.filter(_ => _)
newStatus.rebloggedBy = rebloggedByUsers.filter(_ => _) newStatus.rebloggedBy = rebloggedByUsers.filter(_ => _)
},
updateStatusWithPoll (state, { id, poll }) {
const status = state.allStatusesObject[id]
status.poll = poll
} }
} }
@ -578,6 +582,12 @@ const statuses = {
]).then(([favoritedByUsers, rebloggedByUsers]) => ]).then(([favoritedByUsers, rebloggedByUsers]) =>
commit('addFavsAndRepeats', { id, favoritedByUsers, rebloggedByUsers }) commit('addFavsAndRepeats', { id, favoritedByUsers, rebloggedByUsers })
) )
},
votePoll ({ rootState, commit }, { id, pollId, choices }) {
return rootState.api.backendInteractor.vote(pollId, choices).then(poll => {
commit('updateStatusWithPoll', { id, poll })
return poll
})
} }
}, },
mutations mutations

View File

@ -739,18 +739,14 @@ const markNotificationsAsSeen = ({ id, credentials }) => {
const vote = ({ pollID, choices, credentials }) => { const vote = ({ pollID, choices, credentials }) => {
const form = new FormData() const form = new FormData()
form.append('choices', choices) form.append('choices', choices)
const url = MASTODON_VOTE_URL(encodeURIComponent(pollID)) const url = MASTODON_VOTE_URL(encodeURIComponent(pollID))
console.log(url, choices)
return promisedRequest({ return promisedRequest({
url: MASTODON_VOTE_URL(encodeURIComponent(pollID)), url: MASTODON_VOTE_URL(encodeURIComponent(pollID)),
method: 'POST', method: 'POST',
credentials, credentials,
payload: { payload: {
'test': 'test',
choices: choices choices: choices
} }
}) })