stacker.news/components/discussion-form.js

132 lines
4.1 KiB
JavaScript
Raw Normal View History

2021-08-11 20:13:10 +00:00
import { Form, Input, MarkdownInput, SubmitButton } from '../components/form'
import { useRouter } from 'next/router'
2023-01-12 20:30:17 +00:00
import { gql, useApolloClient, useLazyQuery, useMutation } from '@apollo/client'
2021-08-11 20:13:10 +00:00
import TextareaAutosize from 'react-textarea-autosize'
2021-08-11 20:34:10 +00:00
import Countdown from './countdown'
2023-02-08 19:38:04 +00:00
import AdvPostForm, { AdvPostInitial } from './adv-post-form'
2022-08-18 18:15:24 +00:00
import FeeButton, { EditFeeButton } from './fee-button'
2023-01-12 20:30:17 +00:00
import { ITEM_FIELDS } from '../fragments/items'
import AccordianItem from './accordian-item'
import Item from './item'
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 { discussionSchema } from '../lib/validate'
2021-08-11 20:13:10 +00:00
2021-09-23 17:42:00 +00:00
export function DiscussionForm ({
2023-05-01 20:58:30 +00:00
item, sub, editThreshold, titleLabel = 'title',
2021-09-23 17:42:00 +00:00
textLabel = 'text', buttonText = 'post',
adv, handleSubmit
}) {
2021-08-11 20:13:10 +00:00
const router = useRouter()
2022-04-19 18:32:39 +00:00
const client = useApolloClient()
2023-02-08 19:38:04 +00:00
const schema = discussionSchema(client)
2022-08-10 15:06:31 +00:00
// const me = useMe()
const [upsertDiscussion] = useMutation(
2021-08-11 20:13:10 +00:00
gql`
2023-05-01 20:58:30 +00:00
mutation upsertDiscussion($sub: String, $id: ID, $title: String!, $text: String, $boost: Int, $forward: String) {
upsertDiscussion(sub: $sub, id: $id, title: $title, text: $text, boost: $boost, forward: $forward) {
2021-08-11 20:13:10 +00:00
id
}
}`
)
2023-01-12 20:30:17 +00:00
const [getRelated, { data: relatedData }] = useLazyQuery(gql`
${ITEM_FIELDS}
query related($title: String!) {
related(title: $title, minMatch: "75%", limit: 3) {
items {
...ItemFields
}
}
}`, {
fetchPolicy: 'network-only'
})
const related = relatedData?.related?.items || []
2022-08-10 15:06:31 +00:00
// const cost = linkOrImg ? 10 : me?.freePosts ? 0 : 1
2021-08-11 20:13:10 +00:00
return (
<Form
initial={{
title: item?.title || '',
2021-09-11 21:52:19 +00:00
text: item?.text || '',
2022-08-18 18:15:24 +00:00
...AdvPostInitial({ forward: item?.fwdUser?.name })
2021-08-11 20:13:10 +00:00
}}
2023-02-08 19:38:04 +00:00
schema={schema}
2021-09-23 17:42:00 +00:00
onSubmit={handleSubmit || (async ({ boost, ...values }) => {
const { error } = await upsertDiscussion({
2023-05-01 20:58:30 +00:00
variables: { sub: item?.sub?.name || sub?.name, id: item?.id, boost: boost ? Number(boost) : undefined, ...values }
})
2021-08-11 20:13:10 +00:00
if (error) {
throw new Error({ message: error.toString() })
}
2021-09-23 17:42:00 +00:00
2022-01-07 16:50:41 +00:00
if (item) {
2022-03-10 18:25:16 +00:00
await router.push(`/items/${item.id}`)
2022-01-07 16:50:41 +00:00
} else {
2023-05-01 20:58:30 +00:00
const prefix = sub?.name ? `/~${sub.name}/` : ''
await router.push(prefix + '/recent')
2022-01-07 16:50:41 +00:00
}
2021-09-23 17:42:00 +00:00
})}
2022-01-07 18:28:23 +00:00
storageKeyPrefix={item ? undefined : 'discussion'}
2021-08-11 20:13:10 +00:00
>
<Input
2021-09-23 17:42:00 +00:00
label={titleLabel}
2021-08-11 20:13:10 +00:00
name='title'
required
autoFocus
2022-08-25 18:46:07 +00:00
clear
2023-01-12 20:30:17 +00:00
onChange={async (formik, e) => {
if (e.target.value) {
getRelated({
variables: { title: e.target.value }
})
}
}}
2021-08-11 20:13:10 +00:00
/>
<MarkdownInput
topLevel
2021-09-23 17:42:00 +00:00
label={<>{textLabel} <small className='text-muted ml-2'>optional</small></>}
2021-08-11 20:13:10 +00:00
name='text'
as={TextareaAutosize}
2021-11-12 22:39:52 +00:00
minRows={6}
2021-08-11 20:13:10 +00:00
hint={editThreshold
2021-10-29 17:56:31 +00:00
? <div className='text-muted font-weight-bold'><Countdown date={editThreshold} /></div>
2021-08-11 20:13:10 +00:00
: null}
/>
2022-08-18 18:15:24 +00:00
{adv && <AdvPostForm edit={!!item} />}
2022-08-10 15:06:31 +00:00
<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>
<EditFeeButton
paidSats={item.meSats}
parentId={null} text='save' ChildButton={SubmitButton} variant='secondary'
/>
</div>)
2022-08-10 15:06:31 +00:00
: <FeeButton
2022-09-27 21:19:15 +00:00
baseFee={1} parentId={null} text={buttonText}
2022-08-10 15:06:31 +00:00
ChildButton={SubmitButton} variant='secondary'
/>}
</div>
2023-01-12 23:53:09 +00:00
{!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>
2023-01-12 20:30:17 +00:00
}
2023-01-12 23:53:09 +00:00
/>
</div>}
2021-08-11 20:13:10 +00:00
</Form>
)
}