stacker.news/components/comment-edit.js

64 lines
1.8 KiB
JavaScript
Raw Normal View History

import { Form, MarkdownInput } from '@/components/form'
2021-08-10 22:59:06 +00:00
import { gql, useMutation } from '@apollo/client'
import styles from './reply.module.css'
import { commentSchema } from '@/lib/validate'
import { useToast } from './toast'
import { toastDeleteScheduled } from '@/lib/form'
2023-11-19 20:16:35 +00:00
import { FeeButtonProvider } from './fee-button'
import { ItemButtonBar } from './post'
2021-08-10 22:59:06 +00:00
export default function CommentEdit ({ comment, editThreshold, onSuccess, onCancel }) {
const toaster = useToast()
2023-08-24 00:06:26 +00:00
const [upsertComment] = useMutation(
2021-08-10 22:59:06 +00:00
gql`
2023-08-24 00:06:26 +00:00
mutation upsertComment($id: ID! $text: String!) {
upsertComment(id: $id, text: $text) {
2021-08-10 22:59:06 +00:00
text
deleteScheduledAt
2021-08-10 22:59:06 +00:00
}
}`, {
2023-08-24 00:06:26 +00:00
update (cache, { data: { upsertComment } }) {
2021-08-10 22:59:06 +00:00
cache.modify({
id: `Item:${comment.id}`,
fields: {
text () {
2023-08-24 00:06:26 +00:00
return upsertComment.text
2021-08-10 22:59:06 +00:00
}
}
})
}
}
)
return (
<div className={`${styles.reply} mt-2`}>
2023-11-11 00:18:10 +00:00
<FeeButtonProvider>
<Form
initial={{
text: comment.text
}}
schema={commentSchema}
onSubmit={async (values, { resetForm }) => {
const { data, error } = await upsertComment({ variables: { ...values, id: comment.id } })
2023-11-11 00:18:10 +00:00
if (error) {
throw new Error({ message: error.toString() })
}
toastDeleteScheduled(toaster, data, 'upsertComment', true, values.text)
2023-11-11 00:18:10 +00:00
if (onSuccess) {
onSuccess()
}
}}
>
<MarkdownInput
name='text'
minRows={6}
autoFocus
required
2023-01-12 23:53:09 +00:00
/>
2023-11-19 20:16:35 +00:00
<ItemButtonBar itemId={comment.id} onDelete={onSuccess} hasCancel={false} />
2023-11-11 00:18:10 +00:00
</Form>
</FeeButtonProvider>
2021-08-10 22:59:06 +00:00
</div>
)
}