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 }) {
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
}
}`, {
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`}>
<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 }) => {
2023-08-24 00:06:26 +00:00
const { error } = await upsertComment({ variables: { ...values, id: comment.id } })
2021-08-10 22:59:06 +00:00
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} type='comment'>
2023-01-12 23:53:09 +00:00
<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>
)
}