feat: [2/3]: add poll vote functionality
This commit is contained in:
7 files changed
+123
-41
No files matched your search
@@ -235,6 +235,7 @@ export abstract class BaseClient {
|
|||||||
abstract getTopics?(form: types.GetTopics): Promise<types.GetTopicsResponse>
|
abstract getTopics?(form: types.GetTopics): Promise<types.GetTopicsResponse>
|
||||||
abstract assignFlair?(form: types.AssignFlair): Promise<types.PostView>
|
abstract assignFlair?(form: types.AssignFlair): Promise<types.PostView>
|
||||||
abstract listMedia(form: types.ListMedia): Promise<types.ListMediaResponse>
|
abstract listMedia(form: types.ListMedia): Promise<types.ListMediaResponse>
|
||||||
|
abstract voteOnPoll?(form: types.PollVote): Promise<types.PostView>
|
||||||
}
|
}
|
||||||
|
|
||||||
export type NullableFnArg<T, Fallback = never> = T extends (
|
export type NullableFnArg<T, Fallback = never> = T extends (
|
||||||
|
|||||||
@@ -1020,4 +1020,14 @@ export class PiefedClient implements BaseClient {
|
|||||||
async listMedia(): ReturnType<BaseClient['listMedia']> {
|
async listMedia(): ReturnType<BaseClient['listMedia']> {
|
||||||
throw new Error('unsupported')
|
throw new Error('unsupported')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async voteOnPoll(
|
||||||
|
params: NullableFnArg<BaseClient['voteOnPoll']>[0],
|
||||||
|
): NullableFnReturn<BaseClient['voteOnPoll']> {
|
||||||
|
const response = (
|
||||||
|
await this.#client.POST('/api/alpha/post/poll_vote', { body: params })
|
||||||
|
).data!
|
||||||
|
|
||||||
|
return toPostView(response)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Vendored
+1
@@ -5888,6 +5888,7 @@ export interface components {
|
|||||||
}
|
}
|
||||||
PollVoteRequest: {
|
PollVoteRequest: {
|
||||||
post_id: number
|
post_id: number
|
||||||
|
choice_id: number
|
||||||
}
|
}
|
||||||
PollVoteResponse: {
|
PollVoteResponse: {
|
||||||
banned_from_community: boolean
|
banned_from_community: boolean
|
||||||
|
|||||||
@@ -2450,3 +2450,8 @@ export interface PostEvent {
|
|||||||
[key: string]: unknown
|
[key: string]: unknown
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface PollVote {
|
||||||
|
post_id: number
|
||||||
|
choice_id: number
|
||||||
|
}
|
||||||
@@ -581,7 +581,12 @@
|
|||||||
"created": "Created {{default}}"
|
"created": "Created {{default}}"
|
||||||
},
|
},
|
||||||
"poll": {
|
"poll": {
|
||||||
"votes": "{{votes:number}} votes"
|
"votes": "{{votes:number}} votes",
|
||||||
|
"results": {
|
||||||
|
"show": "Show results",
|
||||||
|
"hide": "Hide results",
|
||||||
|
"ends": "Ends {{time}}"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"comment": {
|
"comment": {
|
||||||
|
|||||||
@@ -203,7 +203,7 @@
|
|||||||
onhide={() => {
|
onhide={() => {
|
||||||
posts = posts.toSpliced(row, 1)
|
posts = posts.toSpliced(row, 1)
|
||||||
}}
|
}}
|
||||||
class="px-3 sm:px-6 hover:bg-slate-100/50 hover:dark:bg-zinc-900/50 transition-colors"
|
class="px-3 sm:px-6 hover:bg-slate-100/30 hover:dark:bg-zinc-900/30 transition-colors"
|
||||||
></Post>
|
></Post>
|
||||||
</li>
|
</li>
|
||||||
{/snippet}
|
{/snippet}
|
||||||
|
|||||||
@@ -1,11 +1,14 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
|
import { client } from '$lib/api/client.svelte'
|
||||||
|
import { PiefedClient } from '$lib/api/piefed/adapter'
|
||||||
import type { Post, PostPoll } from '$lib/api/types'
|
import type { Post, PostPoll } from '$lib/api/types'
|
||||||
|
import { errorMessage } from '$lib/app/error'
|
||||||
import { t } from '$lib/app/i18n'
|
import { t } from '$lib/app/i18n'
|
||||||
import { CommonList } from '$lib/ui/layout'
|
import { CommonList } from '$lib/ui/layout'
|
||||||
import EndPlaceholder from '$lib/ui/layout/EndPlaceholder.svelte'
|
import EndPlaceholder from '$lib/ui/layout/EndPlaceholder.svelte'
|
||||||
import { publishedToDate } from '$lib/ui/util/date'
|
import { publishedToDate } from '$lib/ui/util/date'
|
||||||
import RelativeDate from '$lib/ui/util/RelativeDate.svelte'
|
import { formatRelativeDate } from '$lib/ui/util/RelativeDate.svelte'
|
||||||
import { CheckBadge, Icon } from 'svelte-hero-icons/dist'
|
import { Button, toast } from 'mono-svelte'
|
||||||
import { expoOut } from 'svelte/easing'
|
import { expoOut } from 'svelte/easing'
|
||||||
import { slide } from 'svelte/transition'
|
import { slide } from 'svelte/transition'
|
||||||
|
|
||||||
@@ -15,67 +18,101 @@
|
|||||||
|
|
||||||
let { post }: Props = $props()
|
let { post }: Props = $props()
|
||||||
|
|
||||||
let chosen = $state<number>()
|
|
||||||
let totalVotes = $derived(
|
|
||||||
post.poll.choices.reduce((a, b) => a + b.num_votes, 0),
|
|
||||||
)
|
|
||||||
|
|
||||||
let selected = $state<number>()
|
let selected = $state<number>()
|
||||||
|
|
||||||
// $effect used instead of $derived.by to emphasize
|
let chosen = $state<number>()
|
||||||
// that there is a side effect used (newChoice/prevChoice vote manipulation)
|
let canVote = $state<boolean>(
|
||||||
$effect(() => {
|
!(
|
||||||
if (selected) {
|
post.poll.end_poll &&
|
||||||
castVote(selected)
|
publishedToDate(post.poll.end_poll).getTime() < Date.now()
|
||||||
}
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
let options = $derived(
|
||||||
|
post.poll.choices.map((i) => ({
|
||||||
|
...i,
|
||||||
|
num_votes: chosen == i.id ? i.num_votes + 1 : i.num_votes,
|
||||||
|
})),
|
||||||
|
)
|
||||||
|
let totalVotes = $derived(options.reduce((a, b) => a + b.num_votes, 0))
|
||||||
|
|
||||||
|
let loading = $state(false)
|
||||||
|
|
||||||
|
async function castVote(id: number) {
|
||||||
|
try {
|
||||||
|
const api = client()
|
||||||
|
if (!(api instanceof PiefedClient)) throw new Error('unsupported')
|
||||||
|
|
||||||
|
loading = true
|
||||||
|
chosen = id
|
||||||
|
canVote = false
|
||||||
|
|
||||||
|
await api.voteOnPoll({
|
||||||
|
post_id: post.id,
|
||||||
|
choice_id: id,
|
||||||
})
|
})
|
||||||
|
} catch (err) {
|
||||||
async function castVote(choice: number) {
|
toast({
|
||||||
if (chosen == choice) return
|
content: errorMessage(err as string),
|
||||||
|
type: 'error',
|
||||||
const prevChoice = post.poll.choices.find((i) => i.id == chosen)
|
})
|
||||||
const newChoice = post.poll.choices.find((i) => i.id == choice)!
|
chosen = undefined
|
||||||
|
canVote = true
|
||||||
if (prevChoice) {
|
} finally {
|
||||||
prevChoice.num_votes--
|
loading = false
|
||||||
}
|
}
|
||||||
|
|
||||||
chosen = choice
|
|
||||||
newChoice.num_votes++
|
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<form class="space-y-2">
|
<form class="space-y-2">
|
||||||
<CommonList class="">
|
<CommonList class="">
|
||||||
{#each post.poll.choices as choice}
|
{#each options.toSorted((a, b) => a.sort_order - b.sort_order) as choice}
|
||||||
<li class="relative z-10 overflow-hidden">
|
{@const active = selected == choice.id}
|
||||||
|
{@const percentage = Math.floor(
|
||||||
|
(choice.num_votes / totalVotes || 0) * 100,
|
||||||
|
)}
|
||||||
|
<li
|
||||||
|
class="relative z-10 overflow-hidden has-disabled:pointer-events-none"
|
||||||
|
role="progressbar"
|
||||||
|
aria-valuemin="0"
|
||||||
|
aria-valuemax="100"
|
||||||
|
aria-valuenow={percentage}
|
||||||
|
>
|
||||||
{#if chosen}
|
{#if chosen}
|
||||||
<div
|
<div
|
||||||
in:slide={{ axis: 'x', duration: 500, easing: expoOut }}
|
in:slide={{ axis: 'x', duration: 500, easing: expoOut }}
|
||||||
class="bg-primary-900/5 dark:bg-primary-100/10 absolute inset-0 -z-10 p-0!"
|
class={[
|
||||||
style="width: {(choice.num_votes / totalVotes) *
|
'absolute inset-0 -z-10 p-0!',
|
||||||
100}%; transition: all 0.5s cubic-bezier(0.075, 0.82, 0.165, 1);"
|
active
|
||||||
|
? 'bg-primary-900/10 dark:bg-primary-100/10'
|
||||||
|
: 'bg-primary-900/5 dark:bg-primary-100/5',
|
||||||
|
]}
|
||||||
|
style="width: {percentage}%; transition: all 0.5s cubic-bezier(0.075, 0.82, 0.165, 1);"
|
||||||
></div>
|
></div>
|
||||||
{/if}
|
{/if}
|
||||||
<label
|
<label
|
||||||
class="px-4 py-2 w-full text-left font-medium flex flex-row gap-2 items-center"
|
class="px-4 py-2 w-full text-left flex flex-row gap-2 items-center"
|
||||||
>
|
>
|
||||||
<input
|
<input
|
||||||
class="appearance-none absolute inset-0 cursor-pointer"
|
class="appearance-none absolute inset-0 cursor-pointer w-full h-full"
|
||||||
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}
|
||||||
/>
|
/>
|
||||||
<div class={[chosen == choice.id ? 'opacity-100' : 'opacity-10']}>
|
<div
|
||||||
<Icon src={CheckBadge} size="24" mini />
|
class={[
|
||||||
</div>
|
active
|
||||||
<div>
|
? 'font-medium text-primary-900 dark:text-primary-100'
|
||||||
|
: 'text-slate-600 dark:text-zinc-400',
|
||||||
|
]}
|
||||||
|
>
|
||||||
{choice.choice_text}
|
{choice.choice_text}
|
||||||
</div>
|
</div>
|
||||||
{#if chosen}
|
{#if chosen}
|
||||||
<div class="ml-auto">
|
<div class="ml-auto">
|
||||||
{Math.floor((choice.num_votes / totalVotes) * 100)}%
|
{percentage}%
|
||||||
</div>
|
</div>
|
||||||
{/if}
|
{/if}
|
||||||
</label>
|
</label>
|
||||||
@@ -85,9 +122,32 @@
|
|||||||
<EndPlaceholder size="md" margin="bottom-sm">
|
<EndPlaceholder size="md" margin="bottom-sm">
|
||||||
{$t('post.poll.votes', { votes: totalVotes })}
|
{$t('post.poll.votes', { votes: totalVotes })}
|
||||||
{#snippet action()}
|
{#snippet action()}
|
||||||
{#if post.poll.latest_vote}
|
{#if (selected && canVote) || loading}
|
||||||
<RelativeDate date={publishedToDate(post.poll.latest_vote)} />
|
<Button
|
||||||
|
class="w-24"
|
||||||
|
onclick={() => castVote(selected!)}
|
||||||
|
color="primary"
|
||||||
|
{loading}
|
||||||
|
>
|
||||||
|
{$t('form.submit')}
|
||||||
|
</Button>
|
||||||
|
{:else if post.poll.end_poll}
|
||||||
|
{$t('post.poll.results.ends', {
|
||||||
|
time: formatRelativeDate(publishedToDate(post.poll.end_poll), {}),
|
||||||
|
})}
|
||||||
{/if}
|
{/if}
|
||||||
{/snippet}
|
{/snippet}
|
||||||
</EndPlaceholder>
|
</EndPlaceholder>
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
|
<style>
|
||||||
|
@reference '../../../../app.css';
|
||||||
|
|
||||||
|
li:has(label:focus-within) {
|
||||||
|
border: 1px solid --alpha(var(--color-primary-900) / 50%);
|
||||||
|
|
||||||
|
@variant dark {
|
||||||
|
border-color: --alpha(var(--color-primary-100) / 50%);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</style>
|
||||||
Reference in new issue
Block a user