misc: switch to universal vote function

This commit is contained in:
Xyphyn committed 2023-08-19 14:06:16 -07:00
1 parent 8915759031
commit 76b9864a3b
9 files changed
+134 -102

No files matched your search

@@ -12,7 +12,7 @@
import MenuButton from '$lib/components/ui/menu/MenuButton.svelte'
import { Fire, Icon, ShieldExclamation, Trash } from 'svelte-hero-icons'
import { Color } from '$lib/ui/colors.js'
import { isComment } from '$lib/lemmy/item.js'
import { isCommentView } from '$lib/lemmy/item.js'
import { profile } from '$lib/auth.js'
import ShieldIcon from '$lib/components/lemmy/moderation/ShieldIcon.svelte'
@@ -36,7 +36,7 @@
</li>
<MenuButton color="success" on:click={() => remove(item)}>
<Icon src={Trash} size="16" mini />
{#if isComment(item)}
{#if isCommentView(item)}
{item.comment.removed ? 'Restore' : 'Remove'}
{:else}
{item.post.removed ? 'Restore' : 'Remove'}
@@ -15,7 +15,7 @@
Trash,
} from 'svelte-hero-icons'
import { Color } from '$lib/ui/colors.js'
import { isComment, isPost } from '$lib/lemmy/item.js'
import { isCommentView, isPostView } from '$lib/lemmy/item.js'
import { toast } from '$lib/components/ui/toasts/toasts.js'
import { profile } from '$lib/auth.js'
@@ -27,7 +27,7 @@
$: acting = locking || pinning
async function lock(lock: boolean) {
if (!$profile?.jwt || !isPost(item)) return
if (!$profile?.jwt || !isPostView(item)) return
locking = true
try {
@@ -56,7 +56,7 @@
}
async function pin(pinned: boolean, toInstance: boolean = false) {
if (!$profile?.jwt || !isPost(item)) return
if (!$profile?.jwt || !isPostView(item)) return
pinning = true
@@ -133,7 +133,8 @@
<MenuButton
color="success"
on:click={() => pin(isPost(item) ? !item.post.featured_community : false)}
on:click={() =>
pin(isPostView(item) ? !item.post.featured_community : false)}
loading={pinning}
disabled={pinning}
>
@@ -147,7 +148,7 @@
</MenuButton>
<MenuButton color="dangerSecondary" on:click={() => remove(item)}>
<Icon src={Trash} size="16" mini />
{#if isComment(item)}
{#if isCommentView(item)}
{item.comment.removed ? 'Restore' : 'Remove'}
{:else}
{item.post.removed ? 'Restore' : 'Remove'}
@@ -176,7 +177,7 @@
<MenuButton
color="success"
on:click={() =>
pin(isPost(item) ? !item.post.featured_local : false, true)}
pin(isPostView(item) ? !item.post.featured_local : false, true)}
>
<Icon src={InformationCircle} size="16" mini />
<div class="flex flex-col text-left">
@@ -6,7 +6,7 @@
import Modal from '$lib/components/ui/modal/Modal.svelte'
import { toast } from '$lib/components/ui/toasts/toasts.js'
import { getClient } from '$lib/lemmy.js'
import { isComment, isPost } from '$lib/lemmy/item.js'
import { isCommentView, isPostView } from '$lib/lemmy/item.js'
import type { CommentView, PostView } from 'lemmy-js-client'
import { profile } from '$lib/auth.js'
import Checkbox from '$lib/components/input/Checkbox.svelte'
@@ -27,7 +27,7 @@
let loading = false
$: removed = item
? isComment(item)
? isCommentView(item)
? item.comment.removed
: item.post.removed
: false
@@ -56,7 +56,7 @@
try {
if (purge) {
if (isComment(item)) {
if (isCommentView(item)) {
await getClient(undefined, fetch).purgeComment({
auth: $profile.jwt,
comment_id: item.comment.id,
@@ -94,7 +94,7 @@
.createPrivateMessage({
auth: $profile.jwt,
content: replyReason,
recipient_id: isComment(item)
recipient_id: isCommentView(item)
? item.comment.creator_id
: item.post.creator_id,
})
@@ -110,7 +110,7 @@
auth: $profile.jwt,
content: replyReason,
post_id: item.post.id,
parent_id: isComment(item) ? item.comment.id : undefined,
parent_id: isCommentView(item) ? item.comment.id : undefined,
})
.catch(() => {
toast({
@@ -121,7 +121,7 @@
}
}
if (isComment(item)) {
if (isCommentView(item)) {
await getClient().removeComment({
auth: $profile.jwt,
comment_id: item.comment.id,
@@ -129,7 +129,7 @@
reason: reason || undefined,
})
item.comment.removed = !removed
} else if (isPost(item)) {
} else if (isPostView(item)) {
await getClient().removePost({
auth: $profile.jwt,
post_id: item.post.id,
@@ -180,7 +180,7 @@
on:submit|preventDefault={remove}
class="flex flex-col gap-4 list-none"
>
{#if isComment(item)}
{#if isCommentView(item)}
<Comment
node={{
children: [],
@@ -190,7 +190,7 @@
postId={item.post.id}
actions={false}
/>
{:else if isPost(item)}
{:else if isPostView(item)}
<Post actions={false} post={item} />
{/if}
@@ -1,15 +1,15 @@
import type { Submission } from '$lib/lemmy/contentview.js'
import type { SubmissionView } from '$lib/lemmy/contentview.js'
import type { Community, MyUserInfo, Person } from 'lemmy-js-client'
import { writable } from 'svelte/store'
interface Modals {
reporting: {
open: boolean
item: Submission | undefined
item: SubmissionView | undefined
}
removing: {
open: boolean
item: Submission | undefined
item: SubmissionView | undefined
purge: boolean
}
banning: {
@@ -38,7 +38,7 @@ export let modals = writable<Modals>({
},
})
export function report(item: Submission) {
export function report(item: SubmissionView) {
modals.update((m) => ({
...m,
reporting: {
@@ -48,7 +48,7 @@ export function report(item: Submission) {
}))
}
export function remove(item: Submission, purge: boolean = false) {
export function remove(item: SubmissionView, purge: boolean = false) {
modals.update((m) => ({
...m,
removing: {
+22 -58
View File
@@ -13,64 +13,12 @@
Icon,
} from 'svelte-hero-icons'
import { profile } from '$lib/auth.js'
import { slide } from 'svelte/transition'
import { vote as voteItem } from '$lib/lemmy/contentview.js'
export let post: Post
export let vote: number = 0
export let score: number
async function upvote() {
if (!$profile?.jwt) return
if (vote == undefined) vote = 0
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,
auth: $profile.jwt,
post_id: post.id,
})
.catch((_) => undefined)
}
async function downvote() {
if (!$profile?.jwt) return
if (vote == undefined) vote = 0
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,
auth: $profile.jwt,
post_id: post.id,
})
.catch((_) => undefined)
}
const voteColor = (vote: number) =>
vote == 1
? $userSettings.revertColors
@@ -83,13 +31,17 @@
: ''
</script>
<slot {upvote} {downvote} {vote} {score}>
<slot {vote} {score}>
{#if $userSettings.newVote}
<div class="flex items-center text-sm rounded-md border-zinc-700">
<Button
aria-label="Upvote"
class={vote == 1 ? voteColor(vote) : ''}
on:click={upvote}
on:click={async () => {
if (!$profile?.jwt) return
score = await voteItem(post, vote == 1 ? 0 : 1, $profile.jwt)
vote = vote == 1 ? 0 : 1
}}
size="square-md"
color="tertiary"
alignment="center"
@@ -102,7 +54,11 @@
<Button
aria-label="Downvote"
class={vote == -1 ? voteColor(vote) : ''}
on:click={downvote}
on:click={async () => {
if (!$profile?.jwt) return
score = await voteItem(post, vote == -1 ? 0 : -1, $profile.jwt)
vote = vote == -1 ? 0 : -1
}}
size="square-md"
color="tertiary"
>
@@ -117,7 +73,11 @@
<Button
aria-label="Upvote"
class={vote == 1 ? voteColor(vote) : ''}
on:click={upvote}
on:click={async () => {
if (!$profile?.jwt) return
score = await voteItem(post, vote == 1 ? 0 : 1, $profile.jwt)
vote = vote == 1 ? 0 : 1
}}
size="square-sm"
color="tertiary"
alignment="center"
@@ -132,7 +92,11 @@
<Button
aria-label="Downvote"
class={vote == -1 ? voteColor(vote) : ''}
on:click={downvote}
on:click={async () => {
if (!$profile?.jwt) return
score = await voteItem(post, vote == -1 ? 0 : -1, $profile.jwt)
vote = vote == -1 ? 0 : -1
}}
size="square-sm"
color="tertiary"
>