feat: [5.5/6]: support multiple choice polls
This commit is contained in:
1 file changed
+57
-23
@@ -18,7 +18,7 @@
|
|||||||
|
|
||||||
let { post }: Props = $props()
|
let { post }: Props = $props()
|
||||||
|
|
||||||
let selected = $state<number>()
|
let selected = $state<number | number[]>()
|
||||||
|
|
||||||
let chosen = $state<number>()
|
let chosen = $state<number>()
|
||||||
let canVote = $state<boolean>(
|
let canVote = $state<boolean>(
|
||||||
@@ -38,19 +38,37 @@
|
|||||||
|
|
||||||
let loading = $state(false)
|
let loading = $state(false)
|
||||||
|
|
||||||
async function castVote(id: number) {
|
async function castVote(id: number | number[]) {
|
||||||
try {
|
try {
|
||||||
const api = client()
|
const api = client()
|
||||||
if (!(api instanceof PiefedClient)) throw new Error('unsupported')
|
if (!(api instanceof PiefedClient)) throw new Error('unsupported')
|
||||||
|
|
||||||
loading = true
|
loading = true
|
||||||
chosen = id
|
|
||||||
|
if (typeof id !== 'number') chosen = id[0]
|
||||||
|
else chosen = id
|
||||||
|
|
||||||
canVote = false
|
canVote = false
|
||||||
|
|
||||||
await api.voteOnPoll({
|
if (typeof id !== 'number') {
|
||||||
post_id: post.id,
|
await Promise.all([
|
||||||
choice_id: id,
|
id.map((i) =>
|
||||||
})
|
api
|
||||||
|
.voteOnPoll({
|
||||||
|
post_id: post.id,
|
||||||
|
choice_id: i,
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
throw err
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
])
|
||||||
|
} else {
|
||||||
|
await api.voteOnPoll({
|
||||||
|
post_id: post.id,
|
||||||
|
choice_id: id,
|
||||||
|
})
|
||||||
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
toast({
|
toast({
|
||||||
content: errorMessage(err as string),
|
content: errorMessage(err as string),
|
||||||
@@ -67,7 +85,9 @@
|
|||||||
<form class="space-y-2" onsubmit={() => castVote(selected!)}>
|
<form class="space-y-2" onsubmit={() => castVote(selected!)}>
|
||||||
<CommonList class="">
|
<CommonList class="">
|
||||||
{#each options.toSorted((a, b) => a.sort_order - b.sort_order) as choice}
|
{#each options.toSorted((a, b) => a.sort_order - b.sort_order) as choice}
|
||||||
{@const active = selected == choice.id}
|
{@const active =
|
||||||
|
selected == choice.id ||
|
||||||
|
(typeof selected !== 'number' && selected?.includes(choice.id))}
|
||||||
{@const percentage = Math.floor(
|
{@const percentage = Math.floor(
|
||||||
(choice.num_votes / totalVotes || 0) * 100,
|
(choice.num_votes / totalVotes || 0) * 100,
|
||||||
)}
|
)}
|
||||||
@@ -93,21 +113,26 @@
|
|||||||
<label
|
<label
|
||||||
class="px-4 py-2 w-full text-left flex flex-row gap-2 items-center"
|
class="px-4 py-2 w-full text-left flex flex-row gap-2 items-center"
|
||||||
>
|
>
|
||||||
<input
|
{#if post.poll.mode == 'single'}
|
||||||
class="appearance-none absolute inset-0 cursor-pointer w-full h-full"
|
<input
|
||||||
name="poll={post.id}"
|
class="appearance-none absolute inset-0 cursor-pointer w-full h-full peer"
|
||||||
value={choice.id}
|
name="poll={post.id}"
|
||||||
bind:group={selected}
|
value={choice.id}
|
||||||
type="radio"
|
bind:group={selected}
|
||||||
disabled={!canVote}
|
type="radio"
|
||||||
/>
|
disabled={!canVote}
|
||||||
<div
|
/>
|
||||||
class={[
|
{:else}
|
||||||
active
|
<input
|
||||||
? 'font-medium text-primary-900 dark:text-primary-100'
|
class="appearance-none absolute inset-0 cursor-pointer w-full h-full peer"
|
||||||
: 'text-slate-600 dark:text-zinc-400',
|
name="poll={post.id}"
|
||||||
]}
|
value={choice.id}
|
||||||
>
|
bind:group={selected}
|
||||||
|
type="checkbox"
|
||||||
|
disabled={!canVote}
|
||||||
|
/>
|
||||||
|
{/if}
|
||||||
|
<div class={['choice-text', 'text-slate-600 dark:text-zinc-400']}>
|
||||||
{choice.choice_text}
|
{choice.choice_text}
|
||||||
</div>
|
</div>
|
||||||
{#if chosen}
|
{#if chosen}
|
||||||
@@ -144,6 +169,15 @@
|
|||||||
<style>
|
<style>
|
||||||
@reference '../../../../app.css';
|
@reference '../../../../app.css';
|
||||||
|
|
||||||
|
input:checked ~ .choice-text {
|
||||||
|
font-weight: var(--font-weight-medium);
|
||||||
|
color: var(--color-primary-900);
|
||||||
|
|
||||||
|
@variant dark {
|
||||||
|
color: var(--color-primary-100);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
li:has(label:focus-within) {
|
li:has(label:focus-within) {
|
||||||
border: 1px solid --alpha(var(--color-primary-900) / 50%);
|
border: 1px solid --alpha(var(--color-primary-900) / 50%);
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user