feat: Add post and comment editing (#12)

* feat: Add editing

* feat: Add comment badges

* fix: make loading more consistent

* misc: remove prop drilling

* feat: Add post editing

* misc: update version

* feat: Add comment deletion
This commit is contained in:
Xylight authored and GitHub committed 2023-07-25 11:12:15 -07:00
1 parent 0327ae4aed
commit 7b1ad247f9
16 files changed
+417 -192

No files matched your search

+15 -1
View File
@@ -7,6 +7,9 @@
import { userSettings } from '$lib/settings.js'
import PostLink from '$lib/components/lemmy/post/PostLink.svelte'
import PostMeta from '$lib/components/lemmy/post/PostMeta.svelte'
import Modal from '$lib/components/ui/modal/Modal.svelte'
import PostForm from '$lib/components/lemmy/post/PostForm.svelte'
import { ToastType, toast } from '$lib/components/ui/toasts/toasts.js'
export let post: PostView
</script>
@@ -80,5 +83,16 @@
{post.post.body}
</p>
{/if}
<PostActions {post} />
<PostActions
{post}
on:edit={(e) => {
post.post.name = e.detail.post.name
post.post.body = e.detail.post.body
post.post.url = e.detail.post.url
toast({
content: 'The post was edited successfully.',
type: ToastType.success,
})
}}
/>
</Card>
@@ -11,6 +11,7 @@
EllipsisHorizontal,
Icon,
Newspaper,
PencilSquare,
Square2Stack,
Trash,
UserCircle,
@@ -22,6 +23,10 @@
import { page } from '$app/stores'
import { user, authData } from '$lib/lemmy.js'
import { ToastType, toast } from '$lib/components/ui/toasts/toasts.js'
import { isMutable } from '$lib/components/lemmy/post/helpers.js'
import { createEventDispatcher } from 'svelte'
import Modal from '$lib/components/ui/modal/Modal.svelte'
import PostForm from '$lib/components/lemmy/post/PostForm.svelte'
export let post: PostView
@@ -57,8 +62,25 @@
})
}
}
const dispatcher = createEventDispatcher<{ edit: PostView }>()
let editing = false
</script>
{#if editing}
<Modal bind:open={editing}>
<PostForm
edit
editingPost={post.post}
on:submit={(e) => {
editing = false
dispatcher('edit', e.detail)
}}
/>
</Modal>
{/if}
<div class="flex flex-row gap-2 items-center">
<PostVote post={post.post} vote={post.my_vote} score={post.counts.score} />
@@ -99,6 +121,12 @@
</MenuButton>
<hr class="w-[90%] mx-auto opacity-100 dark:opacity-10 my-2" />
<li class="mx-4 text-xs opacity-80 text-left my-1 py-1">Actions</li>
{#if $user && $user.person.id == post.creator.id}
<MenuButton on:click={() => (editing = true)}>
<Icon src={PencilSquare} width={16} mini />
Edit
</MenuButton>
{/if}
<MenuButton
on:click={() => {
navigator.clipboard.writeText(
@@ -115,7 +143,7 @@
{post.saved ? 'Unsave' : 'Save'}
</MenuButton>
{/if}
{#if $user?.person.id == post.post.creator_id}
{#if $user && isMutable(post, $user)}
<MenuButton on:click={deletePost} color={Color.dangerSecondary}>
<Icon src={Trash} width={16} mini />
Delete
@@ -0,0 +1,160 @@
<script lang="ts">
import { createEventDispatcher, onMount } from 'svelte'
import { getClient, authData, uploadImage } from '$lib/lemmy.js'
import type { Community, Post, PostView } from 'lemmy-js-client'
import TextInput from '$lib/components/input/TextInput.svelte'
import TextArea from '$lib/components/input/TextArea.svelte'
import SelectMenu from '$lib/components/input/SelectMenu.svelte'
import Dots from '$lib/components/ui/loader/Dots.svelte'
import FileInput from '$lib/components/input/FileInput.svelte'
import Button from '$lib/components/input/Button.svelte'
import { ToastType, toast } from '$lib/components/ui/toasts/toasts.js'
import { goto } from '$app/navigation'
export let edit = false
/**
* The post to edit
*/
export let editingPost: Post | undefined = undefined
let data: {
community: number | null
title: string
body: string
image: FileList | null
url: string | undefined
loading: boolean
} = {
community: null,
title: '',
body: '',
image: null,
url: undefined,
loading: false,
}
const placeholders = [
'Is starting nuclear warfare illegal?',
'A cool photo I took before the destruction of mankind!',
'AITA for ending all contention and causing world peace?',
'BREAKING NEWS: Police break into gun shop, find weapons',
'How do I make sure my pet rock stays alive?',
'My cat just invented a time machine, what do I do?',
'Unpopular opinion: world peace and global happiness would be beneficial to humanity',
'LPT: The smaller weights are easier to lift than the bigger weights in a gym',
'javascript bad',
'ELI5: What is 4 + 8?',
]
const placeholder =
placeholders[Math.floor(Math.random() * placeholders.length)]
const placeholder2 =
placeholders[Math.floor(Math.random() * placeholders.length)]
let communities: Community[] | undefined
const dispatcher = createEventDispatcher<{ submit: PostView }>()
onMount(async () => {
if (editingPost) {
data.url = editingPost.url ?? ''
data.body = editingPost.body ?? ''
data.title = editingPost.name
}
const list = await getClient().listCommunities({
auth: $authData?.token,
type_: 'Subscribed',
sort: 'New',
limit: 40,
})
communities = list.communities.map((c) => c.community)
})
async function submit() {
if (!(!data.community && edit) || !data.title || !$authData) return
data.loading = true
try {
if (edit) {
if (!editingPost) {
throw new Error('Post is being edited but editingPost is null')
}
const post = await getClient().editPost({
auth: $authData.token,
name: data.title,
body: data.body,
url: data.url || undefined,
post_id: editingPost.id,
})
if (!post) throw new Error('Failed to edit post')
console.log(`Edited post ${post?.post_view.post.id}`)
dispatcher('submit', post.post_view)
} else {
let image = data.image ? await uploadImage(data.image[0]) : undefined
data.url = image
const post = await getClient().createPost({
auth: $authData.token,
community_id: data.community!,
name: data.title,
body: data.body,
url: data.url,
})
if (!post) throw new Error('Failed to upload post')
console.log(`Uploaded post ${post?.post_view.post.id}`)
dispatcher('submit', post.post_view)
}
} catch (err) {
toast({ content: err as any, type: ToastType.error })
}
}
</script>
<form on:submit|preventDefault={submit} class="flex flex-col gap-4">
<h1 class="font-bold text-xl">{edit ? 'Edit' : 'Create'} Post</h1>
{#if !edit}
<div>
<span class="block my-1 font-bold text-sm">
Community <span class="text-red-500">*</span>
</span>
{#if communities}
<SelectMenu
bind:selected={data.community}
options={communities.map((c) => c.id)}
optionNames={new Map(communities.map((c) => [c.id, c.title]))}
/>
{:else}
<Dots />
{/if}
</div>
{/if}
<TextInput required label="Title" bind:value={data.title} {placeholder} />
<TextInput
label="URL"
bind:value={data.url}
placeholder="https://notascam.com"
/>
<TextArea label="Body" bind:value={data.body} placeholder={placeholder2} />
<div>
<span class="block my-1 font-bold text-sm">Image</span>
<FileInput
accept="image/jpeg,image/png,image/webp"
image
bind:files={data.image}
/>
</div>
<Button submit color="primary" loading={data.loading} size="lg">
Submit
</Button>
</form>
+8
View File
@@ -0,0 +1,8 @@
import type { CommentView, PersonView, PostView } from 'lemmy-js-client'
export const isMutable = (post: PostView, me: PersonView) =>
(me.person.admin && post.post.local) || me.person.id == post.creator.id
export const isCommentMutable = (comment: CommentView, me: PersonView) =>
(me.person.admin && comment.comment.local) ||
me.person.id == comment.creator.id