feat: show upvotes and downvotes
This commit is contained in:
1 parent
77248a0e4f
commit
73ac4ce5ed
3 files changed
+74
-47
No files matched your search
@@ -81,6 +81,8 @@
|
|||||||
post={post.post}
|
post={post.post}
|
||||||
bind:vote={post.my_vote}
|
bind:vote={post.my_vote}
|
||||||
bind:score={post.counts.score}
|
bind:score={post.counts.score}
|
||||||
|
bind:upvotes={post.counts.upvotes}
|
||||||
|
bind:downvotes={post.counts.downvotes}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<Button
|
<Button
|
||||||
|
|||||||
@@ -1,16 +1,25 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import FormattedNumber from '$lib/components/util/FormattedNumber.svelte'
|
import FormattedNumber from '$lib/components/util/FormattedNumber.svelte'
|
||||||
import type { Post } from 'lemmy-js-client'
|
import type { Post } from 'lemmy-js-client'
|
||||||
import { ChevronDown, ChevronUp, Icon, Key } from 'svelte-hero-icons'
|
import {
|
||||||
|
ArrowDown,
|
||||||
|
ArrowUp,
|
||||||
|
ChevronDown,
|
||||||
|
ChevronUp,
|
||||||
|
Icon,
|
||||||
|
Key,
|
||||||
|
} from 'svelte-hero-icons'
|
||||||
import { profile } from '$lib/auth.js'
|
import { profile } from '$lib/auth.js'
|
||||||
import { vote as voteItem } from '$lib/lemmy/contentview.js'
|
import { vote as voteItem } from '$lib/lemmy/contentview.js'
|
||||||
import { Button, toast } from 'mono-svelte'
|
import { Button, Popover, toast } from 'mono-svelte'
|
||||||
import { site } from '$lib/lemmy.js'
|
import { site } from '$lib/lemmy.js'
|
||||||
import { fly } from 'svelte/transition'
|
import { fly } from 'svelte/transition'
|
||||||
|
|
||||||
export let post: Post
|
export let post: Post
|
||||||
export let vote: number = 0
|
export let vote: number = 0
|
||||||
export let score: number
|
export let score: number
|
||||||
|
export let upvotes: number = 0
|
||||||
|
export let downvotes: number = 0
|
||||||
|
|
||||||
let oldScore = score
|
let oldScore = score
|
||||||
|
|
||||||
@@ -24,51 +33,67 @@
|
|||||||
}
|
}
|
||||||
oldScore = score
|
oldScore = score
|
||||||
vote = newVote
|
vote = newVote
|
||||||
score = await voteItem(post, newVote, $profile.jwt)
|
const res = await voteItem(post, newVote, $profile.jwt)
|
||||||
|
;({ upvotes, downvotes, score } = res)
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<slot {vote} {score}>
|
<slot {vote} {score}>
|
||||||
<div
|
<Popover openOnHover placement="bottom">
|
||||||
class=" flex items-center text-sm gap-1 rounded-md border border-slate-200
|
<div
|
||||||
|
class=" flex items-center text-sm gap-1 rounded-md border border-slate-200
|
||||||
dark:border-zinc-800 bg-white dark:bg-zinc-900 px-1 h-8 cursor-pointer"
|
dark:border-zinc-800 bg-white dark:bg-zinc-900 px-1 h-8 cursor-pointer"
|
||||||
>
|
slot="target"
|
||||||
<Button
|
|
||||||
aria-label="Upvote"
|
|
||||||
class={vote == 1 ? voteColor(vote) : ''}
|
|
||||||
on:click={async () => castVote(vote == 1 ? 0 : 1)}
|
|
||||||
size="square-sm"
|
|
||||||
color="tertiary"
|
|
||||||
alignment="center"
|
|
||||||
>
|
>
|
||||||
<Icon src={ChevronUp} mini size="18" />
|
<Button
|
||||||
</Button>
|
aria-label="Upvote"
|
||||||
<span
|
class={vote == 1 ? voteColor(vote) : ''}
|
||||||
class="font-medium transition-colors duration-200 grid {voteColor(vote)}"
|
on:click={async () => castVote(vote == 1 ? 0 : 1)}
|
||||||
class:hidden={$profile?.user?.local_user_view.local_user.show_scores ==
|
size="square-sm"
|
||||||
false}
|
color="tertiary"
|
||||||
>
|
alignment="center"
|
||||||
{#key score}
|
>
|
||||||
<span
|
<Icon src={ChevronUp} mini size="18" />
|
||||||
in:fly={{ y: score > oldScore ? 4 : -4, duration: 200 }}
|
</Button>
|
||||||
out:fly={{ y: score > oldScore ? -4 : 4, duration: 200 }}
|
<span
|
||||||
style="grid-column: 1; grid-row: 1;"
|
class="font-medium transition-colors duration-200 grid {voteColor(
|
||||||
>
|
vote
|
||||||
<FormattedNumber number={score} />
|
)}"
|
||||||
</span>
|
class:hidden={$profile?.user?.local_user_view.local_user.show_scores ==
|
||||||
{/key}
|
false}
|
||||||
</span>
|
>
|
||||||
<Button
|
{#key score}
|
||||||
aria-label="Downvote"
|
<span
|
||||||
class="{vote == -1 ? voteColor(vote) : ''} {$site?.site_view.local_site
|
in:fly={{ y: score > oldScore ? 4 : -4, duration: 200 }}
|
||||||
.enable_downvotes
|
out:fly={{ y: score > oldScore ? -4 : 4, duration: 200 }}
|
||||||
? ''
|
style="grid-column: 1; grid-row: 1;"
|
||||||
: 'pointer-events-none opacity-50'}"
|
>
|
||||||
on:click={async () => castVote(vote == -1 ? 0 : -1)}
|
<FormattedNumber number={score} />
|
||||||
size="square-sm"
|
</span>
|
||||||
color="tertiary"
|
{/key}
|
||||||
>
|
</span>
|
||||||
<Icon src={ChevronDown} mini size="18" />
|
<Button
|
||||||
</Button>
|
aria-label="Downvote"
|
||||||
</div>
|
class="{vote == -1 ? voteColor(vote) : ''} {$site?.site_view.local_site
|
||||||
|
.enable_downvotes
|
||||||
|
? ''
|
||||||
|
: 'pointer-events-none opacity-50'}"
|
||||||
|
on:click={async () => castVote(vote == -1 ? 0 : -1)}
|
||||||
|
size="square-sm"
|
||||||
|
color="tertiary"
|
||||||
|
>
|
||||||
|
<Icon src={ChevronDown} mini size="18" />
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
<div class="flex items-center gap-2">
|
||||||
|
<div class="flex items-center font-medium text-blue-500 gap-0.5">
|
||||||
|
<Icon src={ArrowUp} mini size="18" title="Upvotes" />
|
||||||
|
{upvotes}
|
||||||
|
</div>
|
||||||
|
<div class="flex items-center font-medium text-red-500 gap-0.5">
|
||||||
|
<Icon src={ArrowDown} mini size="18" title="Downvotes" />
|
||||||
|
{downvotes}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</Popover>
|
||||||
</slot>
|
</slot>
|
||||||
@@ -114,7 +114,7 @@ export async function vote(
|
|||||||
item: ContentView | Submission,
|
item: ContentView | Submission,
|
||||||
vote: number,
|
vote: number,
|
||||||
jwt: string
|
jwt: string
|
||||||
): Promise<number> {
|
): Promise<{ upvotes: number; downvotes: number; score: number }> {
|
||||||
if (isSubmission(item)) item = contentItem(item)
|
if (isSubmission(item)) item = contentItem(item)
|
||||||
|
|
||||||
if (item.type == 'post') {
|
if (item.type == 'post') {
|
||||||
@@ -123,16 +123,16 @@ export async function vote(
|
|||||||
post_id: item.id,
|
post_id: item.id,
|
||||||
score: vote,
|
score: vote,
|
||||||
})
|
})
|
||||||
).post_view.counts.score
|
).post_view.counts
|
||||||
} else if (item.type == 'comment') {
|
} else if (item.type == 'comment') {
|
||||||
return (
|
return (
|
||||||
await getClient().likeComment({
|
await getClient().likeComment({
|
||||||
comment_id: item.id,
|
comment_id: item.id,
|
||||||
score: vote,
|
score: vote,
|
||||||
})
|
})
|
||||||
).comment_view.counts.score
|
).comment_view.counts
|
||||||
}
|
}
|
||||||
return 0
|
return { upvotes: 0, downvotes: 0, score: 0 }
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function markAsRead(
|
export async function markAsRead(
|
||||||
|
|||||||
Reference in new issue
Block a user