stacker.news/components/bounty-form.js

141 lines
3.7 KiB
JavaScript
Raw Normal View History

2023-01-26 16:11:55 +00:00
import { Form, Input, MarkdownInput, SubmitButton } 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'
2023-01-26 16:11:55 +00:00
import FeeButton, { EditFeeButton } from './fee-button'
2023-07-24 18:35:05 +00:00
import InputGroup from 'react-bootstrap/InputGroup'
2023-02-08 19:38:04 +00:00
import { bountySchema } 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'
2023-01-26 16:11:55 +00:00
export function BountyForm ({
item,
2023-05-01 20:58:30 +00:00
sub,
2023-01-26 16:11:55 +00:00
editThreshold,
titleLabel = 'title',
bountyLabel = 'bounty',
textLabel = 'text',
buttonText = 'post',
2023-05-11 00:26:07 +00:00
handleSubmit,
children
2023-01-26 16:11:55 +00:00
}) {
const router = useRouter()
const client = useApolloClient()
2023-02-08 19:38:04 +00:00
const schema = bountySchema(client)
2023-01-26 16:11:55 +00:00
const [upsertBounty] = useMutation(
gql`
mutation upsertBounty(
2023-05-01 20:58:30 +00:00
$sub: String
2023-01-26 16:11:55 +00:00
$id: ID
$title: String!
$bounty: Int!
$text: String
$boost: Int
$forward: String
) {
upsertBounty(
2023-05-01 20:58:30 +00:00
sub: $sub
2023-01-26 16:11:55 +00:00
id: $id
title: $title
bounty: $bounty
text: $text
boost: $boost
forward: $forward
) {
id
}
}
`
)
return (
<Form
initial={{
title: item?.title || '',
text: item?.text || '',
bounty: item?.bounty || 1000,
2023-05-11 00:26:07 +00:00
...AdvPostInitial({ forward: item?.fwdUser?.name }),
...SubSelectInitial({ sub: item?.subName || sub?.name })
2023-01-26 16:11:55 +00:00
}}
2023-02-08 19:38:04 +00:00
schema={schema}
2023-01-26 16:11:55 +00:00
onSubmit={
handleSubmit ||
(async ({ boost, bounty, ...values }) => {
const { error } = await upsertBounty({
variables: {
2023-05-05 17:38:56 +00:00
sub: item?.subName || sub?.name,
2023-01-26 16:11:55 +00:00
id: item?.id,
2023-02-08 19:38:04 +00:00
boost: boost ? Number(boost) : undefined,
bounty: bounty ? Number(bounty) : undefined,
2023-01-26 16:11:55 +00:00
...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')
2023-01-26 16:11:55 +00:00
}
})
}
2023-02-08 19:38:04 +00:00
storageKeyPrefix={item ? undefined : 'bounty'}
2023-01-26 16:11:55 +00:00
>
2023-05-11 00:26:07 +00:00
{children}
2023-01-26 16:11:55 +00:00
<Input label={titleLabel} name='title' required autoFocus clear />
<Input
label={bountyLabel} name='bounty' required
append={<InputGroup.Text className='text-monospace'>sats</InputGroup.Text>}
/>
<MarkdownInput
topLevel
label={
<>
2023-07-24 18:35:05 +00:00
{textLabel} <small className='text-muted ms-2'>optional</small>
2023-01-26 16:11:55 +00:00
</>
}
name='text'
minRows={6}
hint={
editThreshold
? (
2023-07-24 18:35:05 +00:00
<div className='text-muted fw-bold'>
2023-01-26 16:11:55 +00:00
<Countdown date={editThreshold} />
</div>
)
: null
}
/>
<AdvPostForm edit={!!item} />
2023-01-26 16:11:55 +00:00
<div className='mt-3'>
{item
? (
<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-26 16:11:55 +00:00
)
: (
<FeeButton
baseFee={1}
parentId={null}
text={buttonText}
ChildButton={SubmitButton}
variant='secondary'
/>
)}
</div>
</Form>
)
}