stacker.news/components/discussion-form.js

82 lines
2.4 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'
2022-04-19 18:32:39 +00:00
import { gql, useApolloClient, useMutation } from '@apollo/client'
2021-08-11 20:13:10 +00:00
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()
2022-04-19 18:32:39 +00:00
const client = useApolloClient()
const [upsertDiscussion] = useMutation(
2021-08-11 20:13:10 +00:00
gql`
mutation upsertDiscussion($id: ID, $title: String!, $text: String, $boost: Int, $forward: String) {
upsertDiscussion(id: $id, title: $title, text: $text, boost: $boost, forward: $forward) {
2021-08-11 20:13:10 +00:00
id
}
}`
)
2022-04-19 18:32:39 +00:00
const DiscussionSchema = Yup.object({
title: Yup.string().required('required').trim(),
...AdvPostSchema(client)
})
2021-08-11 20:13:10 +00:00
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 }) => {
const { error } = await upsertDiscussion({
variables: { id: item?.id, 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
2022-01-07 16:50:41 +00:00
if (item) {
2022-03-10 18:25:16 +00:00
await router.push(`/items/${item.id}`)
2022-01-07 16:50:41 +00:00
} else {
2022-03-10 18:25:16 +00:00
await router.push('/recent')
2022-01-07 16:50:41 +00:00
}
2021-09-23 17:42:00 +00:00
})}
2022-01-07 18:28:23 +00:00
storageKeyPrefix={item ? undefined : 'discussion'}
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}
2021-11-12 22:39:52 +00:00
minRows={6}
2021-08-11 20:13:10 +00:00
hint={editThreshold
2021-10-29 17:56:31 +00:00
? <div className='text-muted font-weight-bold'><Countdown date={editThreshold} /></div>
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>
)
}