stacker.news/components/poll-form.js

112 lines
3.7 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 { gql, useApolloClient, useMutation } from '@apollo/client'
import Countdown from './countdown'
2023-02-08 19:38:04 +00:00
import AdvPostForm, { AdvPostInitial } from './adv-post-form'
import { MAX_POLL_NUM_CHOICES } from '../lib/constants'
2022-08-18 18:15:24 +00:00
import FeeButton, { EditFeeButton } from './fee-button'
2023-01-12 23:53:09 +00:00
import Delete from './delete'
import { Button } from 'react-bootstrap'
2023-02-08 19:38:04 +00:00
import { pollSchema } from '../lib/validate'
2023-05-11 00:26:07 +00:00
import { SubSelectInitial } from './sub-select-form'
2023-06-12 19:34:10 +00:00
import CancelButton from './cancel-button'
2022-07-30 13:25:46 +00:00
2023-05-11 00:26:07 +00:00
export function PollForm ({ item, sub, editThreshold, children }) {
2022-07-30 13:25:46 +00:00
const router = useRouter()
const client = useApolloClient()
2023-02-08 19:38:04 +00:00
const schema = pollSchema(client)
2022-07-30 13:25:46 +00:00
const [upsertPoll] = useMutation(
gql`
2023-05-01 20:58:30 +00:00
mutation upsertPoll($sub: String, $id: ID, $title: String!, $text: String,
2022-07-30 13:25:46 +00:00
$options: [String!]!, $boost: Int, $forward: String) {
2023-05-01 20:58:30 +00:00
upsertPoll(sub: $sub, id: $id, title: $title, text: $text,
2022-07-30 13:25:46 +00:00
options: $options, boost: $boost, forward: $forward) {
id
}
}`
)
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 || ['', ''],
2023-05-11 00:26:07 +00:00
...AdvPostInitial({ forward: item?.fwdUser?.name }),
...SubSelectInitial({ sub: item?.subName || sub?.name })
2022-07-30 13:25:46 +00:00
}}
2023-02-08 19:38:04 +00:00
schema={schema}
2022-07-30 13:25:46 +00:00
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,
2023-05-05 17:38:56 +00:00
sub: item?.subName || sub?.name,
2023-02-08 19:38:04 +00:00
boost: boost ? Number(boost) : undefined,
2022-07-30 13:25:46 +00:00
title: title.trim(),
options: optionsFiltered,
...values
}
})
if (error) {
throw new Error({ message: error.toString() })
}
if (item) {
await router.push(`/items/${item.id}`)
} else {
const prefix = sub?.name ? `/~${sub.name}` : ''
2023-05-01 20:58:30 +00:00
await router.push(prefix + '/recent')
2022-07-30 13:25:46 +00:00
}
}}
storageKeyPrefix={item ? undefined : 'poll'}
>
2023-05-11 00:26:07 +00:00
{children}
2022-07-30 13:25:46 +00:00
<Input
label='title'
name='title'
required
/>
<MarkdownInput
topLevel
label={<>text <small className='text-muted ml-2'>optional</small></>}
name='text'
minRows={2}
/>
<VariableInput
label='choices'
name='options'
2022-08-18 18:15:24 +00:00
readOnlyLen={initialOptions?.length}
max={MAX_POLL_NUM_CHOICES}
2023-01-07 00:53:09 +00:00
min={2}
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
2023-01-12 23:53:09 +00:00
? (
<div className='d-flex justify-content-between'>
<Delete itemId={item.id} onDelete={() => router.push(`/items/${item.id}`)}>
<Button variant='grey-medium'>delete</Button>
</Delete>
<div className='d-flex'>
2023-06-12 19:34:10 +00:00
<CancelButton />
<EditFeeButton
paidSats={item.meSats}
parentId={null} text='save' ChildButton={SubmitButton} variant='secondary'
/>
</div>
2023-01-12 23:53:09 +00:00
</div>)
2022-08-18 18:15:24 +00:00
: <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>
)
}