feat: [5.5/6]: support multiple choice polls

This commit is contained in:
Xylight committed 2025-11-15 21:58:00 -08:00
1 parent a44c4760e4
commit 56e05acf69
1 file changed
+46 -12
+46 -12
View File
@@ -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
if (typeof id !== 'number') {
await Promise.all([
id.map((i) =>
api
.voteOnPoll({
post_id: post.id,
choice_id: i,
})
.catch((err) => {
throw err
}),
),
])
} else {
await api.voteOnPoll({ await api.voteOnPoll({
post_id: post.id, post_id: post.id,
choice_id: 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"
> >
{#if post.poll.mode == 'single'}
<input <input
class="appearance-none absolute inset-0 cursor-pointer w-full h-full" class="appearance-none absolute inset-0 cursor-pointer w-full h-full peer"
name="poll={post.id}" name="poll={post.id}"
value={choice.id} value={choice.id}
bind:group={selected} bind:group={selected}
type="radio" type="radio"
disabled={!canVote} disabled={!canVote}
/> />
<div {:else}
class={[ <input
active class="appearance-none absolute inset-0 cursor-pointer w-full h-full peer"
? 'font-medium text-primary-900 dark:text-primary-100' name="poll={post.id}"
: 'text-slate-600 dark:text-zinc-400', 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%);