Files
photon-tbw/src/lib/components/lemmy/comment/CommentForm.svelte
T
2023-07-06 11:54:53 -07:00

70 lines
1.7 KiB
Svelte

<script lang="ts">
import Button from '$lib/components/input/Button.svelte'
import { Color } from '$lib/ui/colors.js'
import type { CommentResponse } from 'lemmy-js-client'
import { authData, getClient, user } from '$lib/lemmy.js'
import { createEventDispatcher } from 'svelte'
import { ToastType, addToast } from '$lib/components/ui/toasts/toasts.js'
import TextArea from '$lib/components/input/TextArea.svelte'
export let postId: number
export let parentId: number | undefined = undefined
const dispatch = createEventDispatcher<{ comment: CommentResponse }>()
let newComment = ''
let loading = false
async function submit() {
if (!$user || !$authData || newComment == '') return
loading = true
try {
const response = await getClient().createComment({
auth: $authData.token,
content: newComment,
post_id: postId,
parent_id: parentId,
})
dispatch('comment', response)
newComment = ''
addToast(
'Success',
'Your comment was added. You may need to refresh to see it.',
ToastType.success
)
} catch (err) {
console.error(err)
addToast(
'Error',
'Failed to comment. (Sometimes this error appears even when successful.)',
ToastType.error
)
}
loading = false
}
</script>
<div class="max-w-lg flex flex-col gap-2">
<TextArea
rows={3}
placeholder="What are you thinking?"
class="bg-slate-100 dark:bg-zinc-900"
bind:value={newComment}
/>
<div class="w-24 ml-auto flex flex-row gap-2">
<Button
large
on:click={submit}
color={Color.accent}
{loading}
disabled={loading}
>
Submit
</Button>
</div>
</div>