feat: switch to universal delete function

This commit is contained in:
Xyphyn committed 2023-08-19 13:48:15 -07:00
1 parent 5de92f7ae3
commit 6a4d4077b4
3 files changed
+47 -21

No files matched your search

@@ -32,7 +32,7 @@
import ModerationMenu from '$lib/components/lemmy/moderation/ModerationMenu.svelte' import ModerationMenu from '$lib/components/lemmy/moderation/ModerationMenu.svelte'
import CommentModerationMenu from '$lib/components/lemmy/moderation/CommentModerationMenu.svelte' import CommentModerationMenu from '$lib/components/lemmy/moderation/CommentModerationMenu.svelte'
import { profile } from '$lib/auth.js' import { profile } from '$lib/auth.js'
import { save } from '$lib/lemmy/contentview.js' import { deleteItem, save } from '$lib/lemmy/contentview.js'
export let comment: CommentView export let comment: CommentView
export let replying: boolean = false export let replying: boolean = false
@@ -101,27 +101,16 @@
<MenuButton <MenuButton
color="dangerSecondary" color="dangerSecondary"
on:click={async () => { on:click={async () => {
if (!$profile?.jwt) return if ($profile?.jwt)
try { comment.comment.deleted = await deleteItem(
await getClient().deleteComment({ comment,
auth: $profile.jwt, !comment.comment.deleted,
comment_id: comment.comment.id, $profile.jwt
deleted: true, )
})
toast({
content: 'The comment was deleted.',
type: 'success',
})
} catch (error) {
toast({
content: 'Failed to delete comment',
type: 'error',
})
}
}} }}
> >
<Icon src={Trash} mini size="16" /> <Icon src={Trash} mini size="16" />
<span>Delete</span> <span>{comment.comment.deleted ? 'Restore' : 'Delete'}</span>
</MenuButton> </MenuButton>
{/if} {/if}
{#if $profile.jwt && $profile.user?.local_user_view.person.id != comment.creator.id} {#if $profile.jwt && $profile.user?.local_user_view.person.id != comment.creator.id}
@@ -31,7 +31,7 @@
import ModerationMenu from '$lib/components/lemmy/moderation/ModerationMenu.svelte' import ModerationMenu from '$lib/components/lemmy/moderation/ModerationMenu.svelte'
import { profile } from '$lib/auth.js' import { profile } from '$lib/auth.js'
import Spinner from '$lib/components/ui/loader/Spinner.svelte' import Spinner from '$lib/components/ui/loader/Spinner.svelte'
import { save } from '$lib/lemmy/contentview.js' import { deleteItem, save } from '$lib/lemmy/contentview.js'
export let post: PostView export let post: PostView
@@ -166,7 +166,17 @@
{post.saved ? 'Unsave' : 'Save'} {post.saved ? 'Unsave' : 'Save'}
</MenuButton> </MenuButton>
{#if $profile.user && post.creator.id == $profile.user.local_user_view.person.id} {#if $profile.user && post.creator.id == $profile.user.local_user_view.person.id}
<MenuButton on:click={deletePost} color="dangerSecondary"> <MenuButton
on:click={async () => {
if ($profile?.jwt)
post.post.deleted = await deleteItem(
post,
!post.post.deleted,
$profile.jwt
)
}}
color="dangerSecondary"
>
<Icon src={Trash} width={16} mini /> <Icon src={Trash} width={16} mini />
{post.post.deleted ? 'Restore' : 'Delete'} {post.post.deleted ? 'Restore' : 'Delete'}
</MenuButton> </MenuButton>
+27
View File
@@ -59,3 +59,30 @@ export async function save(
} }
return save return save
} }
export async function deleteItem(
item: ContentView | Submission,
deleted: boolean,
jwt: string
): Promise<boolean> {
if (isSubmission(item)) item = contentView(item)
if (item.type == 'post') {
return (
await getClient().deletePost({
auth: jwt,
post_id: item.id,
deleted: deleted,
})
).post_view.post.deleted
} else if (item.type == 'comment') {
return (
await getClient().deleteComment({
auth: jwt,
comment_id: item.id,
deleted: deleted,
})
).comment_view.post.deleted
}
return deleted
}