import { Form, Input, MarkdownInput, SubmitButton } from '../components/form' import { useRouter } from 'next/router' import * as Yup from 'yup' import { gql, useApolloClient, 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 client = useApolloClient() const [upsertDiscussion] = useMutation( gql` mutation upsertDiscussion($id: ID, $title: String!, $text: String, $boost: Int, $forward: String) { upsertDiscussion(id: $id, title: $title, text: $text, boost: $boost, forward: $forward) { id } }` ) const DiscussionSchema = Yup.object({ title: Yup.string().required('required').trim(), ...AdvPostSchema(client) }) return (
{ const { error } = await upsertDiscussion({ variables: { id: item?.id, boost: Number(boost), ...values } }) if (error) { throw new Error({ message: error.toString() }) } if (item) { await router.push(`/items/${item.id}`) } else { await router.push('/recent') } })} storageKeyPrefix={item ? undefined : 'discussion'} > {textLabel} optional} name='text' as={TextareaAutosize} minRows={6} hint={editThreshold ?
: null} /> {!item && adv && } {item ? 'save' : buttonText} ) }