stacker.news/components/discussion-form.js

96 lines
2.7 KiB
JavaScript
Raw Normal View History

2021-08-11 20:13:10 +00:00
import { Form, Input, MarkdownInput, SubmitButton } from '../components/form'
import { useRouter } from 'next/router'
import * as Yup from 'yup'
import { gql, useMutation } from '@apollo/client'
import ActionTooltip from '../components/action-tooltip'
import TextareaAutosize from 'react-textarea-autosize'
2021-08-11 20:34:10 +00:00
import Countdown from './countdown'
2021-09-11 21:52:19 +00:00
import AdvPostForm, { AdvPostInitial, AdvPostSchema } from './adv-post-form'
2021-08-11 20:13:10 +00:00
export const DiscussionSchema = Yup.object({
2021-09-11 21:52:19 +00:00
title: Yup.string().required('required').trim(),
...AdvPostSchema
2021-08-11 20:13:10 +00:00
})
2021-09-23 17:42:00 +00:00
export function DiscussionForm ({
item, editThreshold, titleLabel = 'title',
textLabel = 'text', buttonText = 'post',
adv, handleSubmit
}) {
2021-08-11 20:13:10 +00:00
const router = useRouter()
const [createDiscussion] = useMutation(
gql`
2021-09-11 21:52:19 +00:00
mutation createDiscussion($title: String!, $text: String, $boost: Int) {
createDiscussion(title: $title, text: $text, boost: $boost) {
2021-08-11 20:13:10 +00:00
id
}
}`
)
const [updateDiscussion] = useMutation(
gql`
mutation updateDiscussion($id: ID!, $title: String!, $text: String!) {
updateDiscussion(id: $id, title: $title, text: $text) {
id
}
}`, {
update (cache, { data: { updateDiscussion } }) {
cache.modify({
id: `Item:${item.id}`,
fields: {
title () {
return updateDiscussion.title
},
text () {
return updateDiscussion.text
}
}
})
}
}
)
return (
<Form
initial={{
title: item?.title || '',
2021-09-11 21:52:19 +00:00
text: item?.text || '',
...AdvPostInitial
2021-08-11 20:13:10 +00:00
}}
schema={DiscussionSchema}
2021-09-23 17:42:00 +00:00
onSubmit={handleSubmit || (async ({ boost, ...values }) => {
2021-08-11 20:13:10 +00:00
let id, error
if (item) {
({ data: { updateDiscussion: { id } }, error } = await updateDiscussion({ variables: { ...values, id: item.id } }))
} else {
2021-09-11 21:52:19 +00:00
({ data: { createDiscussion: { id } }, error } = await createDiscussion({ variables: { boost: Number(boost), ...values } }))
2021-08-11 20:13:10 +00:00
}
if (error) {
throw new Error({ message: error.toString() })
}
2021-09-23 17:42:00 +00:00
2021-08-11 20:13:10 +00:00
router.push(`/items/${id}`)
2021-09-23 17:42:00 +00:00
})}
2021-08-11 20:13:10 +00:00
>
<Input
2021-09-23 17:42:00 +00:00
label={titleLabel}
2021-08-11 20:13:10 +00:00
name='title'
required
autoFocus
/>
<MarkdownInput
2021-09-23 17:42:00 +00:00
label={<>{textLabel} <small className='text-muted ml-2'>optional</small></>}
2021-08-11 20:13:10 +00:00
name='text'
as={TextareaAutosize}
minRows={4}
hint={editThreshold
2021-08-11 20:34:10 +00:00
? <Countdown date={editThreshold} />
2021-08-11 20:13:10 +00:00
: null}
/>
2021-09-23 17:42:00 +00:00
{!item && adv && <AdvPostForm />}
2021-09-12 16:55:38 +00:00
<ActionTooltip>
2021-09-23 17:42:00 +00:00
<SubmitButton variant='secondary' className='mt-3'>{item ? 'save' : buttonText}</SubmitButton>
2021-09-12 16:55:38 +00:00
</ActionTooltip>
2021-08-11 20:13:10 +00:00
</Form>
)
}