feat: Make number change on vote

This commit is contained in:
Xyphyn committed 2023-07-05 13:09:01 -07:00
1 parent 1842139af9
commit f037d800c5
2 files changed
+41 -23

No files matched your search

+22 -16
View File
@@ -13,6 +13,16 @@
const upvoted = vote == 1
if (vote == -1) {
score += 2
} else if (vote == 1) {
score -= 1
} else if (vote == 0) {
score += 1
}
vote = Number(!upvoted)
await getClient()
.likePost({
score: upvoted ? 0 : 1,
@@ -27,6 +37,16 @@
const upvoted = vote == -1
if (vote == -1) {
score += 1
} else if (vote == 1) {
score -= 2
} else if (vote == 0) {
score -= 1
}
vote = -Number(!upvoted)
await getClient()
.likePost({
score: upvoted ? 0 : -1,
@@ -50,27 +70,13 @@
: // no vote
'bg-slate-100 dark:bg-zinc-800 hover:bg-slate-200 dark:hover:bg-zinc-700'}"
>
<button
aria-label="Upvote"
class="p-0.5 px-1"
on:click={() => {
vote = vote == 1 ? 0 : 1
upvote()
}}
>
<button aria-label="Upvote" class="p-0.5 px-1" on:click={upvote}>
<Icon src={ChevronUp} mini width={20} height={20} />
</button>
<span class="text-sm">
<FormattedNumber number={score} />
</span>
<button
aria-label="Downvote"
class="p-0.5 px-1"
on:click={() => {
vote = vote == -1 ? 0 : -1
downvote()
}}
>
<button aria-label="Downvote" class="p-0.5 px-1" on:click={downvote}>
<Icon src={ChevronDown} mini width={20} height={20} />
</button>
</div>
@@ -25,11 +25,21 @@
export let open = true
export let replying = false
let score = node.comment_view.counts.score
async function upvote() {
if (!$authData) return
const upvoted = node.comment_view.my_vote == 1
if (node.comment_view.my_vote == -1) {
score += 2
} else if (node.comment_view.my_vote == 1) {
score -= 1
} else if (node.comment_view.my_vote == 0) {
score += 1
}
node.comment_view.my_vote = Number(!upvoted)
await getClient()
@@ -46,6 +56,14 @@
const upvoted = node.comment_view.my_vote == -1
if (node.comment_view.my_vote == -1) {
score += 1
} else if (node.comment_view.my_vote == 1) {
score -= 2
} else if (node.comment_view.my_vote == 0) {
score -= 1
}
node.comment_view.my_vote = -Number(!upvoted)
await getClient()
@@ -71,12 +89,6 @@
<span class="text-sm">
<UserLink avatar user={node.comment_view.creator} />
</span>
<span class="text-sm opacity-60 md:inline hidden">
{node.comment_view.counts.score} point{node.comment_view.counts.score ==
1
? ''
: 's'}
</span>
<span class="text-sm opacity-60">
<RelativeDate date={new Date(node.comment_view.comment.published)} />
</span>
@@ -117,7 +129,7 @@
<Icon src={ArrowUp} width={16} mini />
</button>
<span class="text-sm font-medium">
{node.comment_view.counts.score}
{score}
</span>
<button
on:click={downvote}