pleroma-fe/src/components/poll/poll.js

112 lines
3 KiB
JavaScript
Raw Normal View History

2021-08-13 12:19:57 +03:00
import Timeago from 'components/timeago/timeago.vue'
import genRandomSeed from '../../services/random_seed/random_seed.service.js'
2021-08-13 12:19:57 +03:00
import RichContent from 'components/rich_content/rich_content.jsx'
2025-03-13 02:10:20 +02:00
import Checkbox from 'components/checkbox/checkbox.vue'
2025-02-03 13:02:14 +02:00
import { usePollsStore } from 'src/stores/polls'
2019-06-18 20:28:31 +00:00
export default {
name: 'Poll',
2021-08-13 12:19:57 +03:00
props: ['basePoll', 'emoji'],
components: {
Timeago,
2025-03-13 02:10:20 +02:00
RichContent,
Checkbox
2021-08-13 12:19:57 +03:00
},
2019-06-18 20:28:31 +00:00
data () {
return {
loading: false,
2023-02-18 13:40:42 -05:00
choices: [],
randomSeed: genRandomSeed()
2019-06-18 20:28:31 +00:00
}
},
2019-06-22 14:01:36 +00:00
created () {
2023-04-05 22:30:20 -06:00
if (!usePollsStore().pollsObject[this.pollId]) {
usePollsStore().mergeOrAddPoll(this.basePoll)
2019-06-22 14:01:36 +00:00
}
2023-04-05 22:30:20 -06:00
usePollsStore().trackPoll(this.pollId)
2019-06-18 20:28:31 +00:00
},
2021-04-25 13:44:50 +03:00
unmounted () {
2023-04-05 22:30:20 -06:00
usePollsStore().untrackPoll(this.pollId)
2019-06-18 20:28:31 +00:00
},
computed: {
2019-06-22 14:01:36 +00:00
pollId () {
return this.basePoll.id
},
poll () {
2023-04-05 22:30:20 -06:00
const storePoll = usePollsStore().pollsObject[this.pollId]
return storePoll || {}
},
options () {
return (this.poll && this.poll.options) || []
},
expiresAt () {
return (this.poll && this.poll.expires_at) || null
},
2019-06-18 20:28:31 +00:00
expired () {
2019-06-22 14:01:36 +00:00
return (this.poll && this.poll.expired) || false
2019-06-18 20:28:31 +00:00
},
expirationLabel () {
if (this.$store.getters.mergedConfig.useAbsoluteTimeFormat) {
return this.expired ? 'polls.expired_at' : 'polls.expires_at'
} else {
return this.expired ? 'polls.expired' : 'polls.expires_in'
}
},
2019-06-18 20:28:31 +00:00
loggedIn () {
return this.$store.state.users.currentUser
},
showResults () {
return this.poll.voted || this.expired || !this.loggedIn
},
totalVotesCount () {
return this.poll.votes_count
},
containerClass () {
return {
loading: this.loading
}
},
choiceIndices () {
// Convert array of booleans into an array of indices of the
// items that were 'true', so [true, false, false, true] becomes
// [0, 3].
return this.choices
.map((entry, index) => entry && index)
.filter(value => typeof value === 'number')
},
isDisabled () {
const noChoice = this.choiceIndices.length === 0
return this.loading || noChoice
}
},
methods: {
percentageForOption (count) {
return this.totalVotesCount === 0 ? 0 : Math.round(count / this.totalVotesCount * 100)
},
resultTitle (option) {
return `${option.votes_count}/${this.totalVotesCount} ${this.$t('polls.votes')}`
},
2025-03-13 02:10:20 +02:00
activateOption (index, value) {
let result
2019-06-18 20:28:31 +00:00
if (this.poll.multiple) {
2025-03-13 02:10:20 +02:00
result = this.choices || this.options.map(() => false)
2019-06-18 20:28:31 +00:00
} else {
2025-03-13 02:10:20 +02:00
result = this.options.map(() => false)
2019-06-18 20:28:31 +00:00
}
2025-03-13 02:10:20 +02:00
result[index] = value
this.choices = result
2019-06-18 20:28:31 +00:00
},
optionId (index) {
return `poll${this.poll.id}-${index}`
},
vote () {
if (this.choiceIndices.length === 0) return
this.loading = true
2023-04-05 22:30:20 -06:00
usePollsStore().votePoll(
2019-06-18 20:28:31 +00:00
{ id: this.statusId, pollId: this.poll.id, choices: this.choiceIndices }
2025-02-04 15:23:21 +02:00
).then(() => {
2019-06-18 20:28:31 +00:00
this.loading = false
})
}
}
}