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}
|
||||
bind:vote={post.my_vote}
|
||||
bind:score={post.counts.score}
|
||||
bind:upvotes={post.counts.upvotes}
|
||||
bind:downvotes={post.counts.downvotes}
|
||||
/>
|
||||
|
||||
<Button
|
||||
|
||||
@@ -1,16 +1,25 @@
|
||||
<script lang="ts">
|
||||
import FormattedNumber from '$lib/components/util/FormattedNumber.svelte'
|
||||
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 { 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 { fly } from 'svelte/transition'
|
||||
|
||||
export let post: Post
|
||||
export let vote: number = 0
|
||||
export let score: number
|
||||
export let upvotes: number = 0
|
||||
export let downvotes: number = 0
|
||||
|
||||
let oldScore = score
|
||||
|
||||
@@ -24,51 +33,67 @@
|
||||
}
|
||||
oldScore = score
|
||||
vote = newVote
|
||||
score = await voteItem(post, newVote, $profile.jwt)
|
||||
const res = await voteItem(post, newVote, $profile.jwt)
|
||||
;({ upvotes, downvotes, score } = res)
|
||||
}
|
||||
</script>
|
||||
|
||||
<slot {vote} {score}>
|
||||
<div
|
||||
class=" flex items-center text-sm gap-1 rounded-md border border-slate-200
|
||||
<Popover openOnHover placement="bottom">
|
||||
<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"
|
||||
>
|
||||
<Button
|
||||
aria-label="Upvote"
|
||||
class={vote == 1 ? voteColor(vote) : ''}
|
||||
on:click={async () => castVote(vote == 1 ? 0 : 1)}
|
||||
size="square-sm"
|
||||
color="tertiary"
|
||||
alignment="center"
|
||||
slot="target"
|
||||
>
|
||||
<Icon src={ChevronUp} mini size="18" />
|
||||
</Button>
|
||||
<span
|
||||
class="font-medium transition-colors duration-200 grid {voteColor(vote)}"
|
||||
class:hidden={$profile?.user?.local_user_view.local_user.show_scores ==
|
||||
false}
|
||||
>
|
||||
{#key score}
|
||||
<span
|
||||
in:fly={{ y: score > oldScore ? 4 : -4, duration: 200 }}
|
||||
out:fly={{ y: score > oldScore ? -4 : 4, duration: 200 }}
|
||||
style="grid-column: 1; grid-row: 1;"
|
||||
>
|
||||
<FormattedNumber number={score} />
|
||||
</span>
|
||||
{/key}
|
||||
</span>
|
||||
<Button
|
||||
aria-label="Downvote"
|
||||
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>
|
||||
<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>
|
||||
<span
|
||||
class="font-medium transition-colors duration-200 grid {voteColor(
|
||||
vote
|
||||
)}"
|
||||
class:hidden={$profile?.user?.local_user_view.local_user.show_scores ==
|
||||
false}
|
||||
>
|
||||
{#key score}
|
||||
<span
|
||||
in:fly={{ y: score > oldScore ? 4 : -4, duration: 200 }}
|
||||
out:fly={{ y: score > oldScore ? -4 : 4, duration: 200 }}
|
||||
style="grid-column: 1; grid-row: 1;"
|
||||
>
|
||||
<FormattedNumber number={score} />
|
||||
</span>
|
||||
{/key}
|
||||
</span>
|
||||
<Button
|
||||
aria-label="Downvote"
|
||||
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>
|
||||
@@ -114,7 +114,7 @@ export async function vote(
|
||||
item: ContentView | Submission,
|
||||
vote: number,
|
||||
jwt: string
|
||||
): Promise<number> {
|
||||
): Promise<{ upvotes: number; downvotes: number; score: number }> {
|
||||
if (isSubmission(item)) item = contentItem(item)
|
||||
|
||||
if (item.type == 'post') {
|
||||
@@ -123,16 +123,16 @@ export async function vote(
|
||||
post_id: item.id,
|
||||
score: vote,
|
||||
})
|
||||
).post_view.counts.score
|
||||
).post_view.counts
|
||||
} else if (item.type == 'comment') {
|
||||
return (
|
||||
await getClient().likeComment({
|
||||
comment_id: item.id,
|
||||
score: vote,
|
||||
})
|
||||
).comment_view.counts.score
|
||||
).comment_view.counts
|
||||
}
|
||||
return 0
|
||||
return { upvotes: 0, downvotes: 0, score: 0 }
|
||||
}
|
||||
|
||||
export async function markAsRead(
|
||||
|
||||
Reference in new issue
Block a user