stacker.news/components/poll-form.js

108 lines
3.5 KiB
JavaScript
Raw Normal View History

2022-07-30 13:25:46 +00:00
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'
2022-08-18 18:15:24 +00:00
import { MAX_TITLE_LENGTH, MAX_POLL_CHOICE_LENGTH, MAX_POLL_NUM_CHOICES } from '../lib/constants'
2022-07-30 13:25:46 +00:00
import TextareaAutosize from 'react-textarea-autosize'
2022-08-18 18:15:24 +00:00
import FeeButton, { EditFeeButton } from './fee-button'
2022-07-30 13:25:46 +00:00
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)
})
2022-08-18 18:15:24 +00:00
const initialOptions = item?.poll?.options.map(i => i.option)
2022-07-30 13:25:46 +00:00
return (
<Form
initial={{
title: item?.title || '',
2022-08-18 18:15:24 +00:00
text: item?.text || '',
options: initialOptions || ['', ''],
...AdvPostInitial({ forward: item?.fwdUser?.name })
2022-07-30 13:25:46 +00:00
}}
schema={PollSchema}
onSubmit={async ({ boost, title, options, ...values }) => {
2022-08-18 18:15:24 +00:00
const optionsFiltered = options.slice(initialOptions?.length).filter(word => word.trim().length > 0)
2022-07-30 13:25:46 +00:00
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'}
>
<Input
label='title'
name='title'
required
/>
<MarkdownInput
topLevel
label={<>text <small className='text-muted ml-2'>optional</small></>}
name='text'
as={TextareaAutosize}
minRows={2}
/>
<VariableInput
label='choices'
name='options'
2022-08-18 18:15:24 +00:00
readOnlyLen={initialOptions?.length}
max={MAX_POLL_NUM_CHOICES}
2022-07-30 13:25:46 +00:00
hint={editThreshold
? <div className='text-muted font-weight-bold'><Countdown date={editThreshold} /></div>
: null}
/>
2022-08-18 18:15:24 +00:00
<AdvPostForm edit={!!item} />
<div className='mt-3'>
{item
? <EditFeeButton
2022-09-27 21:19:15 +00:00
paidSats={item.meSats}
2022-08-18 18:15:24 +00:00
parentId={null} text='save' ChildButton={SubmitButton} variant='secondary'
/>
: <FeeButton
2022-09-27 21:19:15 +00:00
baseFee={1} parentId={null} text='post'
2022-08-18 18:15:24 +00:00
ChildButton={SubmitButton} variant='secondary'
/>}
</div>
2022-07-30 13:25:46 +00:00
</Form>
)
}