stacker.news/components/comment-edit.js

70 lines
1.9 KiB
JavaScript
Raw Normal View History

2023-11-11 00:18:10 +00:00
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'
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'
2023-11-11 00:18:10 +00:00
import FeeButton, { FeeButtonProvider } from './fee-button'
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`}>
2023-11-11 00:18:10 +00:00
<FeeButtonProvider>
<Form
initial={{
text: comment.text
}}
schema={commentSchema}
onSubmit={async (values, { resetForm }) => {
const { error } = await upsertComment({ variables: { ...values, id: comment.id } })
if (error) {
throw new Error({ message: error.toString() })
}
if (onSuccess) {
onSuccess()
}
}}
>
<MarkdownInput
name='text'
minRows={6}
autoFocus
required
2023-01-12 23:53:09 +00:00
/>
2023-11-11 00:18:10 +00:00
<div className='d-flex justify-content-between'>
<Delete itemId={comment.id} onDelete={onSuccess} type='comment'>
<Button variant='grey-medium'>delete</Button>
</Delete>
<div className='d-flex mt-3'>
<FeeButton
text='save'
variant='secondary'
/>
</div>
</div>
</Form>
</FeeButtonProvider>
2021-08-10 22:59:06 +00:00
</div>
)
}