From 56e05acf69c40dac8fc946d9b19802e7be828cd0 Mon Sep 17 00:00:00 2001 From: Xylight Date: Sat, 15 Nov 2025 21:58:00 -0800 Subject: [PATCH] feat: [5.5/6]: support multiple choice polls --- src/lib/feature/post/media/PostPoll.svelte | 80 +++++++++++++++------- 1 file changed, 57 insertions(+), 23 deletions(-) diff --git a/src/lib/feature/post/media/PostPoll.svelte b/src/lib/feature/post/media/PostPoll.svelte index a6cdd156..fd5c1ce9 100644 --- a/src/lib/feature/post/media/PostPoll.svelte +++ b/src/lib/feature/post/media/PostPoll.svelte @@ -18,7 +18,7 @@ let { post }: Props = $props() - let selected = $state() + let selected = $state() let chosen = $state() let canVote = $state( @@ -38,19 +38,37 @@ let loading = $state(false) - async function castVote(id: number) { + async function castVote(id: number | number[]) { try { const api = client() if (!(api instanceof PiefedClient)) throw new Error('unsupported') loading = true - chosen = id + + if (typeof id !== 'number') chosen = id[0] + else chosen = id + canVote = false - await api.voteOnPoll({ - post_id: post.id, - choice_id: id, - }) + 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({ + post_id: post.id, + choice_id: id, + }) + } } catch (err) { toast({ content: errorMessage(err as string), @@ -67,7 +85,9 @@
castVote(selected!)}> {#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( (choice.num_votes / totalVotes || 0) * 100, )} @@ -93,21 +113,26 @@