import { Form, Input, MarkdownInput, SubmitButton, VariableInput } from '../components/form' import { useRouter } from 'next/router' import * as Yup from 'yup' import { gql, useApolloClient, useMutation } from '@apollo/client' import Countdown from './countdown' import AdvPostForm, { AdvPostInitial, AdvPostSchema } from './adv-post-form' import { MAX_TITLE_LENGTH, MAX_POLL_CHOICE_LENGTH, MAX_POLL_NUM_CHOICES } from '../lib/constants' import TextareaAutosize from 'react-textarea-autosize' import FeeButton, { EditFeeButton } from './fee-button' import Delete from './delete' import { Button } from 'react-bootstrap' export function PollForm ({ item, editThreshold }) { const router = useRouter() const client = useApolloClient() const [upsertPoll] = useMutation( gql` mutation upsertPoll($id: ID, $title: String!, $text: String, $options: [String!]!, $boost: Int, $forward: String) { upsertPoll(id: $id, title: $title, text: $text, options: $options, boost: $boost, forward: $forward) { id } }` ) const PollSchema = Yup.object({ title: Yup.string().required('required').trim() .max(MAX_TITLE_LENGTH, ({ max, value }) => `${Math.abs(max - value.length)} too many`), options: Yup.array().of( Yup.string().trim().test('my-test', 'required', function (value) { return (this.path !== 'options[0]' && this.path !== 'options[1]') || value }).max(MAX_POLL_CHOICE_LENGTH, ({ max, value }) => `${Math.abs(max - value.length)} too many`) ), ...AdvPostSchema(client) }) const initialOptions = item?.poll?.options.map(i => i.option) return (
{ const optionsFiltered = options.slice(initialOptions?.length).filter(word => word.trim().length > 0) const { error } = await upsertPoll({ variables: { id: item?.id, boost: Number(boost), title: title.trim(), options: optionsFiltered, ...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 : 'poll'} > text optional} name='text' as={TextareaAutosize} minRows={2} /> : null} />
{item ? (
router.push(`/items/${item.id}`)}>
) : }
) }