stacker.news/components/comment-edit.js

66 lines
1.8 KiB
JavaScript
Raw Normal View History

2021-08-10 22:59:06 +00:00
import { Form, MarkdownInput, SubmitButton } from '../components/form'
import { gql, useMutation } from '@apollo/client'
import styles from './reply.module.css'
2022-08-18 18:15:24 +00:00
import { EditFeeButton } from './fee-button'
2023-07-24 18:35:05 +00:00
import Button from 'react-bootstrap/Button'
2023-01-12 23:53:09 +00:00
import Delete from './delete'
2023-02-08 19:38:04 +00:00
import { commentSchema } from '../lib/validate'
2021-08-10 22:59:06 +00:00
export default function CommentEdit ({ comment, editThreshold, onSuccess, onCancel }) {
const [updateComment] = useMutation(
gql`
mutation updateComment($id: ID! $text: String!) {
updateComment(id: $id, text: $text) {
text
}
}`, {
update (cache, { data: { updateComment } }) {
cache.modify({
id: `Item:${comment.id}`,
fields: {
text () {
return updateComment.text
}
}
})
}
}
)
return (
<div className={`${styles.reply} mt-2`}>
<Form
initial={{
text: comment.text
}}
2023-02-08 19:38:04 +00:00
schema={commentSchema}
2021-08-10 22:59:06 +00:00
onSubmit={async (values, { resetForm }) => {
const { error } = await updateComment({ variables: { ...values, id: comment.id } })
if (error) {
throw new Error({ message: error.toString() })
}
if (onSuccess) {
onSuccess()
}
}}
>
<MarkdownInput
name='text'
2021-11-13 16:28:58 +00:00
minRows={6}
2021-08-10 22:59:06 +00:00
autoFocus
required
/>
2023-01-12 23:53:09 +00:00
<div className='d-flex justify-content-between'>
<Delete itemId={comment.id} onDelete={onSuccess}>
<Button variant='grey-medium'>delete</Button>
</Delete>
<EditFeeButton
paidSats={comment.meSats}
parentId={comment.parentId} text='save' ChildButton={SubmitButton} variant='secondary'
/>
</div>
2021-08-10 22:59:06 +00:00
</Form>
</div>
)
}