2022-11-30 05:29:02 +09:00
|
|
|
<script setup lang="ts">
|
2022-12-28 20:49:47 +09:00
|
|
|
import type { Status } from 'masto'
|
2022-11-30 05:29:02 +09:00
|
|
|
|
2022-12-28 20:49:47 +09:00
|
|
|
const { status } = defineProps<{
|
|
|
|
status: Status
|
2022-11-30 05:29:02 +09:00
|
|
|
}>()
|
2022-12-28 20:49:47 +09:00
|
|
|
const poll = reactive({ ...status.poll! })
|
2022-11-30 05:29:02 +09:00
|
|
|
|
|
|
|
function toPercentage(num: number) {
|
|
|
|
const percentage = 100 * num
|
|
|
|
return `${percentage.toFixed(1).replace(/\.?0+$/, '')}%`
|
|
|
|
}
|
2022-12-02 11:18:36 +09:00
|
|
|
const timeAgoOptions = useTimeAgoOptions()
|
|
|
|
const expiredTimeAgo = useTimeAgo(poll.expiresAt!, timeAgoOptions)
|
2022-12-08 19:07:54 +09:00
|
|
|
const expiredTimeFormatted = useFormattedDateTime(poll.expiresAt!)
|
2022-12-08 19:56:23 +09:00
|
|
|
const { formatHumanReadableNumber } = useHumanReadableNumber()
|
2022-11-30 05:29:02 +09:00
|
|
|
|
|
|
|
const masto = useMasto()
|
|
|
|
async function vote(e: Event) {
|
|
|
|
const formData = new FormData(e.target as HTMLFormElement)
|
|
|
|
const choices = formData.getAll('choices') as string[]
|
|
|
|
|
|
|
|
// Update the poll optimistically
|
|
|
|
for (const [index, option] of poll.options.entries()) {
|
|
|
|
if (choices.includes(String(index)))
|
|
|
|
option.votesCount = (option.votesCount || 0) + 1
|
|
|
|
}
|
|
|
|
poll.voted = true
|
|
|
|
poll.votesCount++
|
|
|
|
poll.votersCount = (poll.votersCount || 0) + 1
|
2022-12-28 20:49:47 +09:00
|
|
|
cacheStatus({ ...status, poll }, undefined, true)
|
|
|
|
|
|
|
|
await masto.poll.vote(poll.id, { choices })
|
2022-11-30 05:29:02 +09:00
|
|
|
}
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<template>
|
|
|
|
<div flex flex-col w-full items-stretch gap-3>
|
2022-11-30 15:35:12 +09:00
|
|
|
<form v-if="!poll.voted && !poll.expired" flex flex-col gap-4 accent-primary @click.stop="noop" @submit.prevent="vote">
|
2022-11-30 05:29:02 +09:00
|
|
|
<label v-for="(option, index) of poll.options" :key="index" flex items-center gap-2 px-2>
|
|
|
|
<input name="choices" :value="index" :type="poll.multiple ? 'checkbox' : 'radio'">
|
|
|
|
{{ option.title }}
|
|
|
|
</label>
|
|
|
|
<button btn-solid>
|
2022-12-05 03:49:35 +09:00
|
|
|
{{ $t('action.vote') }}
|
2022-11-30 05:29:02 +09:00
|
|
|
</button>
|
|
|
|
</form>
|
|
|
|
<template v-else>
|
2022-12-05 03:04:40 +09:00
|
|
|
<div v-for="(option, index) of poll.options" :key="index" py-1 relative :style="{ '--bar-width': toPercentage((option.votesCount || 0) / poll.votesCount) }">
|
|
|
|
<div flex justify-between pb-2 w-full>
|
|
|
|
<span inline-flex align-items>
|
|
|
|
{{ option.title }}
|
|
|
|
<span v-if="poll.voted && poll.ownVotes?.includes(index)" ml-2 mt-1 inline-block i-ri:checkbox-circle-line />
|
|
|
|
</span>
|
|
|
|
<span text-primary-active> {{ poll.votesCount ? toPercentage((option.votesCount || 0) / (poll.votesCount)) : '0%' }}</span>
|
2022-11-30 05:29:02 +09:00
|
|
|
</div>
|
2022-12-05 03:04:40 +09:00
|
|
|
<div class="bg-gray/40" rounded-l-sm rounded-r-lg h-5px w-full>
|
|
|
|
<div bg-primary-active h-full class="w-[var(--bar-width)]" />
|
2022-11-30 05:29:02 +09:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
<div text-sm>
|
2022-12-08 19:56:23 +09:00
|
|
|
{{ $t('status.poll.count', formatHumanReadableNumber(poll.votersCount ?? 0)) }}
|
2022-12-05 03:58:21 +09:00
|
|
|
·
|
2022-12-08 19:07:54 +09:00
|
|
|
<CommonTooltip :content="expiredTimeFormatted" class="inline-block" placement="right">
|
|
|
|
<time :datetime="poll.expiresAt!">{{ $t(poll.expired ? 'status.poll.finished' : 'status.poll.ends', [expiredTimeAgo]) }}</time>
|
|
|
|
</CommonTooltip>
|
2022-11-30 05:29:02 +09:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</template>
|