stacker.news/components/poll-form.js

122 lines
4.0 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'
2023-07-24 18:35:05 +00:00
import Button from 'react-bootstrap/Button'
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'
import { useCallback } from 'react'
2023-08-09 23:45:59 +00:00
import { useInvoiceable } from './invoice'
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,
$options: [String!]!, $boost: Int, $forward: String, $invoiceHash: String) {
2023-05-01 20:58:30 +00:00
upsertPoll(sub: $sub, id: $id, title: $title, text: $text,
options: $options, boost: $boost, forward: $forward, invoiceHash: $invoiceHash) {
2022-07-30 13:25:46 +00:00
id
}
}`
)
const submitUpsertPoll = useCallback(
async (_, boost, title, options, values, invoiceHash) => {
const optionsFiltered = options.slice(initialOptions?.length).filter(word => word.trim().length > 0)
const { error } = await upsertPoll({
variables: {
id: item?.id,
sub: item?.subName || sub?.name,
boost: boost ? Number(boost) : undefined,
title: title.trim(),
options: optionsFiltered,
...values,
invoiceHash
}
})
if (error) {
throw new Error({ message: error.toString() })
}
if (item) {
await router.push(`/items/${item.id}`)
} else {
const prefix = sub?.name ? `/~${sub.name}` : ''
await router.push(prefix + '/recent')
}
}, [upsertPoll, router])
2023-08-09 23:45:59 +00:00
const invoiceableUpsertPoll = useInvoiceable(submitUpsertPoll)
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}
onSubmit={async ({ boost, title, options, cost, ...values }) => {
2023-08-09 23:45:59 +00:00
await invoiceableUpsertPoll(cost, boost, title, options, values)
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
2023-07-24 18:35:05 +00:00
label={<>text <small className='text-muted ms-2'>optional</small></>}
2022-07-30 13:25:46 +00:00
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
2023-07-24 18:35:05 +00:00
? <div className='text-muted fw-bold'><Countdown date={editThreshold} /></div>
2022-07-30 13:25:46 +00:00
: 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>
)
}