From d3168c1d1d27f6bc819541de1ecc1f7d8c18b866 Mon Sep 17 00:00:00 2001 From: Xylight Date: Mon, 26 Feb 2024 17:03:36 -0800 Subject: [PATCH] improve: update commentvote --- .../components/lemmy/comment/Comment.svelte | 20 +-- .../lemmy/comment/CommentActions.svelte | 5 +- .../lemmy/comment/CommentVote.svelte | 136 +++++++----------- src/lib/components/lemmy/post/PostVote.svelte | 28 ++-- src/lib/components/util/RelativeDate.svelte | 2 +- src/lib/lemmy/contentview.ts | 11 +- 6 files changed, 82 insertions(+), 120 deletions(-) diff --git a/src/lib/components/lemmy/comment/Comment.svelte b/src/lib/components/lemmy/comment/Comment.svelte index 6eceebd5..88d46257 100644 --- a/src/lib/components/lemmy/comment/Comment.svelte +++ b/src/lib/components/lemmy/comment/Comment.svelte @@ -101,7 +101,7 @@ @@ -109,20 +109,10 @@ OP {/if} - - - • - - {Math.floor( - (node.comment_view.counts.upvotes / - (node.comment_view.counts.upvotes + - node.comment_view.counts.downvotes)) * - 100 - ) || 0}% - - + {#if node.comment_view.comment.updated} diff --git a/src/lib/components/lemmy/comment/CommentActions.svelte b/src/lib/components/lemmy/comment/CommentActions.svelte index af241cf9..b1b6d237 100644 --- a/src/lib/components/lemmy/comment/CommentActions.svelte +++ b/src/lib/components/lemmy/comment/CommentActions.svelte @@ -34,7 +34,8 @@
@@ -93,7 +94,7 @@ {comment.saved ? 'Unsave' : 'Save'} - {#if $profile?.user && $profile.jwt && isCommentMutable(comment, $profile.user.local_user_view)} + {#if $profile?.user && $profile.jwt && $profile.user.local_user_view.person.id == comment.creator.id} { diff --git a/src/lib/components/lemmy/comment/CommentVote.svelte b/src/lib/components/lemmy/comment/CommentVote.svelte index ad70ceb5..7d1abb92 100644 --- a/src/lib/components/lemmy/comment/CommentVote.svelte +++ b/src/lib/components/lemmy/comment/CommentVote.svelte @@ -12,97 +12,69 @@ import { userSettings } from '$lib/settings' import { profile } from '$lib/auth.js' import FormattedNumber from '$lib/components/util/FormattedNumber.svelte' - import { Button } from 'mono-svelte' + import { Button, buttonColor, toast } from 'mono-svelte' + import { vote as voteItem } from '$lib/lemmy/contentview' + import { shouldShowVoteColor } from '../post/PostVote.svelte' + import { fly } from 'svelte/transition' export let vote: number = 0 - export let score: number + export let upvotes: number + export let downvotes: number export let commentId: number - async function upvote() { - if (!$profile?.jwt) return - - const upvoted = vote == 1 - - if (vote == -1) { - score += 2 - } else if (vote == 1) { - score -= 1 - } else if (vote == 0) { - score += 1 + const castVote = async (newVote: number) => { + if (!$profile?.jwt) { + toast({ content: 'You must be logged in to vote.', type: 'warning' }) + return } - vote = Number(!upvoted) - - await getClient() - .likeComment({ - score: upvoted ? 0 : 1, - comment_id: commentId, - }) - .catch((_) => undefined) + vote = newVote + const res = await getClient().likeComment({ + comment_id: commentId, + score: vote, + }) + ;({ upvotes, downvotes } = res.comment_view.counts) } - - async function downvote() { - if (!$profile?.jwt) return - - 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() - .likeComment({ - score: upvoted ? 0 : -1, - comment_id: commentId, - }) - .catch((_) => undefined) - } - - const voteColor = (vote: number) => ` - ${vote == 1 ? '!text-blue-500' : vote == -1 ? '!text-red-500' : ''} - ` -
- - + + {#key upvotes} + + + + {/key} + + +
+ + + + {#key downvotes} + + + + {/key} + +
diff --git a/src/lib/components/lemmy/post/PostVote.svelte b/src/lib/components/lemmy/post/PostVote.svelte index 799fcd14..99a2612e 100644 --- a/src/lib/components/lemmy/post/PostVote.svelte +++ b/src/lib/components/lemmy/post/PostVote.svelte @@ -1,3 +1,17 @@ + + -