2021-04-14 00:57:32 +00:00
|
|
|
import Button from 'react-bootstrap/Button'
|
|
|
|
import { Form, Input, SubmitButton } from '../components/form'
|
2021-04-12 18:05:09 +00:00
|
|
|
import { useRouter } from 'next/router'
|
|
|
|
import Link from 'next/link'
|
2021-04-14 00:57:32 +00:00
|
|
|
import * as Yup from 'yup'
|
|
|
|
import { gql, useMutation } from '@apollo/client'
|
2021-05-06 21:15:22 +00:00
|
|
|
import LayoutCenter from '../components/layout-center'
|
2021-05-20 19:11:58 +00:00
|
|
|
import { ensureProtocol } from '../lib/url'
|
2021-05-25 00:08:56 +00:00
|
|
|
import { useMe } from '../components/me'
|
2021-04-14 00:57:32 +00:00
|
|
|
|
|
|
|
export const DiscussionSchema = Yup.object({
|
|
|
|
title: Yup.string().required('required').trim()
|
|
|
|
})
|
2021-04-12 18:05:09 +00:00
|
|
|
|
|
|
|
export function DiscussionForm () {
|
2021-04-14 00:57:32 +00:00
|
|
|
const router = useRouter()
|
|
|
|
const [createDiscussion] = useMutation(
|
|
|
|
gql`
|
|
|
|
mutation createDiscussion($title: String!, $text: String) {
|
|
|
|
createDiscussion(title: $title, text: $text) {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
)
|
|
|
|
|
2021-04-12 18:05:09 +00:00
|
|
|
return (
|
2021-04-14 00:57:32 +00:00
|
|
|
<Form
|
|
|
|
initial={{
|
|
|
|
title: '',
|
|
|
|
text: ''
|
|
|
|
}}
|
|
|
|
schema={DiscussionSchema}
|
|
|
|
onSubmit={async (values) => {
|
|
|
|
const { data: { createDiscussion: { id } }, error } = await createDiscussion({ variables: values })
|
|
|
|
if (error) {
|
|
|
|
throw new Error({ message: error.toString() })
|
|
|
|
}
|
|
|
|
router.push(`items/${id}`)
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<Input
|
|
|
|
label='title'
|
|
|
|
name='title'
|
|
|
|
required
|
|
|
|
autoFocus
|
|
|
|
/>
|
|
|
|
<Input
|
|
|
|
label={<>text <small className='text-muted ml-2'>optional</small></>}
|
|
|
|
name='text'
|
|
|
|
as='textarea'
|
|
|
|
rows={4}
|
|
|
|
/>
|
|
|
|
<SubmitButton variant='secondary' className='mt-2'>post</SubmitButton>
|
2021-04-12 18:05:09 +00:00
|
|
|
</Form>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-04-14 00:57:32 +00:00
|
|
|
export const LinkSchema = Yup.object({
|
|
|
|
title: Yup.string().required('required').trim(),
|
2021-05-20 19:11:58 +00:00
|
|
|
url: Yup.string().test({
|
|
|
|
name: 'url',
|
|
|
|
test: (value) => {
|
|
|
|
try {
|
|
|
|
value = ensureProtocol(value)
|
|
|
|
const valid = new URL(value)
|
|
|
|
return Boolean(valid)
|
|
|
|
} catch {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
},
|
|
|
|
message: 'invalid url'
|
|
|
|
}).required('required')
|
2021-04-14 00:57:32 +00:00
|
|
|
})
|
|
|
|
|
2021-04-12 18:05:09 +00:00
|
|
|
export function LinkForm () {
|
2021-04-14 00:57:32 +00:00
|
|
|
const router = useRouter()
|
|
|
|
const [createLink] = useMutation(
|
|
|
|
gql`
|
|
|
|
mutation createLink($title: String!, $url: String!) {
|
|
|
|
createLink(title: $title, url: $url) {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
)
|
|
|
|
|
2021-04-12 18:05:09 +00:00
|
|
|
return (
|
2021-04-14 00:57:32 +00:00
|
|
|
<Form
|
|
|
|
initial={{
|
|
|
|
title: '',
|
|
|
|
url: ''
|
|
|
|
}}
|
|
|
|
schema={LinkSchema}
|
|
|
|
onSubmit={async (values) => {
|
|
|
|
const { data: { createLink: { id } }, error } = await createLink({ variables: values })
|
|
|
|
if (error) {
|
|
|
|
throw new Error({ message: error.toString() })
|
|
|
|
}
|
|
|
|
router.push(`items/${id}`)
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<Input
|
|
|
|
label='title'
|
|
|
|
name='title'
|
|
|
|
required
|
|
|
|
autoFocus
|
|
|
|
/>
|
|
|
|
<Input
|
|
|
|
label='url'
|
|
|
|
name='url'
|
|
|
|
required
|
|
|
|
/>
|
|
|
|
<SubmitButton variant='secondary' className='mt-2'>post</SubmitButton>
|
2021-04-12 18:05:09 +00:00
|
|
|
</Form>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function PostForm () {
|
|
|
|
const router = useRouter()
|
2021-05-25 00:08:56 +00:00
|
|
|
const me = useMe()
|
2021-04-12 18:05:09 +00:00
|
|
|
|
|
|
|
if (!router.query.type) {
|
|
|
|
return (
|
|
|
|
<div className='align-items-center'>
|
|
|
|
<Link href='/post?type=link'>
|
2021-04-14 00:57:32 +00:00
|
|
|
<Button variant='secondary'>link</Button>
|
2021-04-12 18:05:09 +00:00
|
|
|
</Link>
|
|
|
|
<span className='mx-3 font-weight-bold text-muted'>or</span>
|
|
|
|
<Link href='/post?type=discussion'>
|
2021-05-06 21:15:22 +00:00
|
|
|
<Button variant='secondary'>discussion</Button>
|
2021-04-12 18:05:09 +00:00
|
|
|
</Link>
|
2021-05-25 00:08:56 +00:00
|
|
|
{me?.freePosts
|
|
|
|
? <div className='text-center font-weight-bold mt-3 text-success'>{me.freePosts} free posts left</div>
|
|
|
|
: null}
|
2021-04-12 18:05:09 +00:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (router.query.type === 'discussion') {
|
|
|
|
return <DiscussionForm />
|
|
|
|
} else {
|
|
|
|
return <LinkForm />
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default function Post () {
|
|
|
|
return (
|
2021-05-06 21:15:22 +00:00
|
|
|
<LayoutCenter>
|
|
|
|
<PostForm />
|
|
|
|
</LayoutCenter>
|
2021-04-12 18:05:09 +00:00
|
|
|
)
|
|
|
|
}
|