ac45fdc234
* Use HODL invoices * Fix expiry check comparing string with Date * Fix unconfirmed user balance for HODL invoices This is done by syncing the data from LND to the Invoice table. If the columns is_held and msatsReceived are set, the frontend is told that we're ready to execute the action. We then update the user balance in the same tx as the action. We need to still keep checking the invoice for expiration though. * Fix worker acting upon deleted invoices * Prevent usage of invoice after expiration * Use onComplete from <Countdown> to show expired status * Remove unused lnd argument * Fix item destructuring from query * Fix balance added to every stacker * Fix hmac required * Fix invoices not used when logged in * refactor: move invoiceable code into form * renamed invoiceHash, invoiceHmac to hash, hmac since it's less verbose all over the place * form now supports `invoiceable` in its props * form then wraps `onSubmit` with `useInvoiceable` and passes optional invoice options * Show expired if expired and canceled * Also use useCallback for zapping * Always expire modal invoices after 3m * little styling thing --------- Co-authored-by: ekzyis <ek@stacker.news> Co-authored-by: keyan <keyan.kousha+huumn@gmail.com> Co-authored-by: Keyan <34140557+huumn@users.noreply.github.com>
151 lines
4.7 KiB
JavaScript
151 lines
4.7 KiB
JavaScript
import { Form, Input, MarkdownInput, SubmitButton } from '../components/form'
|
|
import { useRouter } from 'next/router'
|
|
import { gql, useApolloClient, useLazyQuery, useMutation } from '@apollo/client'
|
|
import Countdown from './countdown'
|
|
import AdvPostForm, { AdvPostInitial } from './adv-post-form'
|
|
import FeeButton, { EditFeeButton } from './fee-button'
|
|
import { ITEM_FIELDS } from '../fragments/items'
|
|
import AccordianItem from './accordian-item'
|
|
import Item from './item'
|
|
import Delete from './delete'
|
|
import Button from 'react-bootstrap/Button'
|
|
import { discussionSchema } from '../lib/validate'
|
|
import { SubSelectInitial } from './sub-select-form'
|
|
import CancelButton from './cancel-button'
|
|
import { useCallback } from 'react'
|
|
import { normalizeForwards } from '../lib/form'
|
|
|
|
export function DiscussionForm ({
|
|
item, sub, editThreshold, titleLabel = 'title',
|
|
textLabel = 'text', buttonText = 'post',
|
|
handleSubmit, children
|
|
}) {
|
|
const router = useRouter()
|
|
const client = useApolloClient()
|
|
const schema = discussionSchema(client)
|
|
// if Web Share Target API was used
|
|
const shareTitle = router.query.title
|
|
|
|
// const me = useMe()
|
|
const [upsertDiscussion] = useMutation(
|
|
gql`
|
|
mutation upsertDiscussion($sub: String, $id: ID, $title: String!, $text: String, $boost: Int, $forward: [ItemForwardInput], $hash: String, $hmac: String) {
|
|
upsertDiscussion(sub: $sub, id: $id, title: $title, text: $text, boost: $boost, forward: $forward, hash: $hash, hmac: $hmac) {
|
|
id
|
|
}
|
|
}`
|
|
)
|
|
|
|
const onSubmit = useCallback(
|
|
async ({ boost, ...values }) => {
|
|
const { error } = await upsertDiscussion({
|
|
variables: {
|
|
sub: item?.subName || sub?.name,
|
|
id: item?.id,
|
|
boost: boost ? Number(boost) : undefined,
|
|
...values,
|
|
forward: normalizeForwards(values.forward)
|
|
}
|
|
})
|
|
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')
|
|
}
|
|
}, [upsertDiscussion, router]
|
|
)
|
|
|
|
const [getRelated, { data: relatedData }] = useLazyQuery(gql`
|
|
${ITEM_FIELDS}
|
|
query related($title: String!) {
|
|
related(title: $title, minMatch: "75%", limit: 3) {
|
|
items {
|
|
...ItemFields
|
|
}
|
|
}
|
|
}`)
|
|
|
|
const related = relatedData?.related?.items || []
|
|
|
|
// const cost = linkOrImg ? 10 : me?.freePosts ? 0 : 1
|
|
|
|
return (
|
|
<Form
|
|
initial={{
|
|
title: item?.title || shareTitle || '',
|
|
text: item?.text || '',
|
|
...AdvPostInitial({ forward: normalizeForwards(item?.forwards) }),
|
|
...SubSelectInitial({ sub: item?.subName || sub?.name })
|
|
}}
|
|
schema={schema}
|
|
invoiceable
|
|
onSubmit={handleSubmit || onSubmit}
|
|
storageKeyPrefix={item ? undefined : 'discussion'}
|
|
>
|
|
{children}
|
|
<Input
|
|
label={titleLabel}
|
|
name='title'
|
|
required
|
|
autoFocus
|
|
clear
|
|
onChange={async (formik, e) => {
|
|
if (e.target.value) {
|
|
getRelated({
|
|
variables: { title: e.target.value }
|
|
})
|
|
}
|
|
}}
|
|
/>
|
|
<MarkdownInput
|
|
topLevel
|
|
label={<>{textLabel} <small className='text-muted ms-2'>optional</small></>}
|
|
name='text'
|
|
minRows={6}
|
|
hint={editThreshold
|
|
? <div className='text-muted fw-bold'><Countdown date={editThreshold} /></div>
|
|
: null}
|
|
/>
|
|
<AdvPostForm edit={!!item} />
|
|
<div className='mt-3'>
|
|
{item
|
|
? (
|
|
<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'>
|
|
<CancelButton />
|
|
<EditFeeButton
|
|
paidSats={item.meSats}
|
|
parentId={null} text='save' ChildButton={SubmitButton} variant='secondary'
|
|
/>
|
|
</div>
|
|
</div>)
|
|
: <FeeButton
|
|
baseFee={1} parentId={null} text={buttonText}
|
|
ChildButton={SubmitButton} variant='secondary'
|
|
/>}
|
|
</div>
|
|
{!item &&
|
|
<div className={`mt-3 ${related.length > 0 ? '' : 'invisible'}`}>
|
|
<AccordianItem
|
|
header={<div style={{ fontWeight: 'bold', fontSize: '92%' }}>similar</div>}
|
|
body={
|
|
<div>
|
|
{related.map((item, i) => (
|
|
<Item item={item} key={item.id} />
|
|
))}
|
|
</div>
|
|
}
|
|
/>
|
|
</div>}
|
|
</Form>
|
|
)
|
|
}
|