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' import Countdown from './countdown' import AdvPostForm, { AdvPostInitial, AdvPostSchema } from './adv-post-form' export const DiscussionSchema = Yup.object({ title: Yup.string().required('required').trim(), ...AdvPostSchema }) export function DiscussionForm ({ item, editThreshold, titleLabel = 'title', textLabel = 'text', buttonText = 'post', adv, handleSubmit }) { const router = useRouter() const [createDiscussion] = useMutation( gql` mutation createDiscussion($title: String!, $text: String, $boost: Int) { createDiscussion(title: $title, text: $text, boost: $boost) { 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 (
{ let id, error if (item) { ({ data: { updateDiscussion: { id } }, error } = await updateDiscussion({ variables: { ...values, id: item.id } })) } else { ({ data: { createDiscussion: { id } }, error } = await createDiscussion({ variables: { boost: Number(boost), ...values } })) } if (error) { throw new Error({ message: error.toString() }) } router.push(`/items/${id}`) })} > {textLabel} optional} name='text' as={TextareaAutosize} minRows={4} hint={editThreshold ? : null} /> {!item && adv && } {item ? 'save' : buttonText} ) }