feat: show upvotes and downvotes
This commit is contained in:
1 parent
77248a0e4f
commit
73ac4ce5ed
3 files changed
+35
-8
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,14 +33,17 @@
|
|||||||
}
|
}
|
||||||
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}>
|
||||||
|
<Popover openOnHover placement="bottom">
|
||||||
<div
|
<div
|
||||||
class=" flex items-center text-sm gap-1 rounded-md border border-slate-200
|
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
|
<Button
|
||||||
aria-label="Upvote"
|
aria-label="Upvote"
|
||||||
@@ -44,7 +56,9 @@
|
|||||||
<Icon src={ChevronUp} mini size="18" />
|
<Icon src={ChevronUp} mini size="18" />
|
||||||
</Button>
|
</Button>
|
||||||
<span
|
<span
|
||||||
class="font-medium transition-colors duration-200 grid {voteColor(vote)}"
|
class="font-medium transition-colors duration-200 grid {voteColor(
|
||||||
|
vote
|
||||||
|
)}"
|
||||||
class:hidden={$profile?.user?.local_user_view.local_user.show_scores ==
|
class:hidden={$profile?.user?.local_user_view.local_user.show_scores ==
|
||||||
false}
|
false}
|
||||||
>
|
>
|
||||||
@@ -71,4 +85,15 @@
|
|||||||
<Icon src={ChevronDown} mini size="18" />
|
<Icon src={ChevronDown} mini size="18" />
|
||||||
</Button>
|
</Button>
|
||||||
</div>
|
</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