misc: switch to universal save function
This commit is contained in:
1 parent
435c529abc
commit
5de92f7ae3
3 files changed
+75
-50
No files matched your search
@@ -32,33 +32,11 @@
|
|||||||
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'
|
||||||
|
|
||||||
export let comment: CommentView
|
export let comment: CommentView
|
||||||
export let replying: boolean = false
|
export let replying: boolean = false
|
||||||
|
|
||||||
async function save() {
|
|
||||||
if (!$profile?.jwt) return
|
|
||||||
|
|
||||||
const saved = comment.saved
|
|
||||||
|
|
||||||
comment.saved = !saved
|
|
||||||
|
|
||||||
try {
|
|
||||||
await getClient().saveComment({
|
|
||||||
auth: $profile.jwt,
|
|
||||||
comment_id: comment.comment.id,
|
|
||||||
save: !saved,
|
|
||||||
})
|
|
||||||
} catch (error) {
|
|
||||||
comment.saved = saved
|
|
||||||
|
|
||||||
toast({
|
|
||||||
content: error as any,
|
|
||||||
type: 'error',
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const dispatcher = createEventDispatcher<{ edit: CommentView }>()
|
const dispatcher = createEventDispatcher<{ edit: CommentView }>()
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
@@ -110,7 +88,12 @@
|
|||||||
<span>Edit</span>
|
<span>Edit</span>
|
||||||
</MenuButton>
|
</MenuButton>
|
||||||
{/if}
|
{/if}
|
||||||
<MenuButton on:click={save}>
|
<MenuButton
|
||||||
|
on:click={async () => {
|
||||||
|
if ($profile?.jwt)
|
||||||
|
comment.saved = await save(comment, !comment.saved, $profile.jwt)
|
||||||
|
}}
|
||||||
|
>
|
||||||
<Icon src={comment.saved ? BookmarkSlash : Bookmark} mini size="16" />
|
<Icon src={comment.saved ? BookmarkSlash : Bookmark} mini size="16" />
|
||||||
<span>{comment.saved ? 'Unsave' : 'Save'}</span>
|
<span>{comment.saved ? 'Unsave' : 'Save'}</span>
|
||||||
</MenuButton>
|
</MenuButton>
|
||||||
|
|||||||
@@ -31,6 +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'
|
||||||
|
|
||||||
export let post: PostView
|
export let post: PostView
|
||||||
|
|
||||||
@@ -58,31 +59,6 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function save(post: PostView) {
|
|
||||||
if (!$profile?.jwt) return false
|
|
||||||
|
|
||||||
const saved = post.saved
|
|
||||||
|
|
||||||
post.saved = !saved
|
|
||||||
|
|
||||||
try {
|
|
||||||
await getClient().savePost({
|
|
||||||
auth: $profile.jwt,
|
|
||||||
post_id: post.post.id,
|
|
||||||
save: !saved,
|
|
||||||
})
|
|
||||||
} catch (error) {
|
|
||||||
post.saved = saved
|
|
||||||
|
|
||||||
toast({
|
|
||||||
content: error as any,
|
|
||||||
type: 'error',
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
return post.saved
|
|
||||||
}
|
|
||||||
|
|
||||||
const dispatcher = createEventDispatcher<{ edit: PostView }>()
|
const dispatcher = createEventDispatcher<{ edit: PostView }>()
|
||||||
|
|
||||||
let editing = false
|
let editing = false
|
||||||
@@ -180,7 +156,12 @@
|
|||||||
Copy Link
|
Copy Link
|
||||||
</MenuButton>
|
</MenuButton>
|
||||||
{#if $profile?.jwt}
|
{#if $profile?.jwt}
|
||||||
<MenuButton on:click={async () => (post.saved = await save(post))}>
|
<MenuButton
|
||||||
|
on:click={async () => {
|
||||||
|
if ($profile?.jwt)
|
||||||
|
post.saved = await save(post, !post.saved, $profile.jwt)
|
||||||
|
}}
|
||||||
|
>
|
||||||
<Icon src={post.saved ? BookmarkSlash : Bookmark} width={16} mini />
|
<Icon src={post.saved ? BookmarkSlash : Bookmark} width={16} mini />
|
||||||
{post.saved ? 'Unsave' : 'Save'}
|
{post.saved ? 'Unsave' : 'Save'}
|
||||||
</MenuButton>
|
</MenuButton>
|
||||||
|
|||||||
@@ -0,0 +1,61 @@
|
|||||||
|
import { getClient } from '$lib/lemmy.js'
|
||||||
|
import { isComment, isPost } from '$lib/lemmy/item.js'
|
||||||
|
import type { CommentView, Person, PostView } from 'lemmy-js-client'
|
||||||
|
|
||||||
|
type Submission = PostView | CommentView
|
||||||
|
|
||||||
|
export interface ContentView {
|
||||||
|
type: 'post' | 'comment'
|
||||||
|
title?: string
|
||||||
|
body: string
|
||||||
|
creator: Person
|
||||||
|
id: number
|
||||||
|
}
|
||||||
|
|
||||||
|
const isSubmission = (item: Submission | ContentView): item is Submission =>
|
||||||
|
!('type' in item)
|
||||||
|
|
||||||
|
export const contentView = (item: Submission): ContentView => {
|
||||||
|
if (isComment(item))
|
||||||
|
return {
|
||||||
|
type: 'comment',
|
||||||
|
body: item.comment.content,
|
||||||
|
creator: item.creator,
|
||||||
|
id: item.comment.id,
|
||||||
|
}
|
||||||
|
else
|
||||||
|
return {
|
||||||
|
type: 'post',
|
||||||
|
body: item.post.body ?? item.post.name,
|
||||||
|
title: item.post.name,
|
||||||
|
creator: item.creator,
|
||||||
|
id: item.post.id,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function save(
|
||||||
|
item: ContentView | Submission,
|
||||||
|
save: boolean,
|
||||||
|
jwt: string
|
||||||
|
): Promise<boolean> {
|
||||||
|
if (isSubmission(item)) item = contentView(item)
|
||||||
|
|
||||||
|
if (item.type == 'post') {
|
||||||
|
return (
|
||||||
|
await getClient().savePost({
|
||||||
|
auth: jwt,
|
||||||
|
post_id: item.id,
|
||||||
|
save: save,
|
||||||
|
})
|
||||||
|
).post_view.saved
|
||||||
|
} else if (item.type == 'comment') {
|
||||||
|
return (
|
||||||
|
await getClient().saveComment({
|
||||||
|
auth: jwt,
|
||||||
|
comment_id: item.id,
|
||||||
|
save: save,
|
||||||
|
})
|
||||||
|
).comment_view.saved
|
||||||
|
}
|
||||||
|
return save
|
||||||
|
}
|
||||||
Reference in new issue
Block a user