From df1f1a483a3331157ebd7af2d4a7cd734d7f3e22 Mon Sep 17 00:00:00 2001 From: keyan Date: Wed, 10 May 2023 19:26:07 -0500 Subject: [PATCH] require sub selection, allow editting --- api/resolvers/price.js | 7 +- components/bounty-form.js | 8 +- components/discussion-form.js | 7 +- components/form.js | 12 ++- components/job-form.js | 2 +- components/layout-center.module.css | 1 + components/link-form.js | 7 +- components/poll-form.js | 7 +- components/post.js | 87 +++++++++++++++++++++ components/sub-select-form.js | 55 ++++++++++++++ lib/validate.js | 18 ++++- pages/items/[id]/edit.js | 28 ++++--- pages/post.js | 114 +--------------------------- pages/~/[sub]/post.js | 76 +------------------ pages/~/[sub]/recent/index.js | 2 +- 15 files changed, 217 insertions(+), 214 deletions(-) create mode 100644 components/post.js create mode 100644 components/sub-select-form.js diff --git a/api/resolvers/price.js b/api/resolvers/price.js index 2baedfc7..c04e7df9 100644 --- a/api/resolvers/price.js +++ b/api/resolvers/price.js @@ -21,15 +21,16 @@ async function getPrice (fiat) { const expired = createdAt + expiresIn < Date.now() if (expired) fetchPrice(fiat).catch(console.error) // update cache return price // serve stale price (this on the SSR critical path) + } else { + fetchPrice(fiat).catch(console.error) } - return await fetchPrice(fiat) + return null } export default { Query: { price: async (parent, { fiatCurrency }, ctx) => { - const price = await getPrice(fiatCurrency) - return price + return await getPrice(fiatCurrency) } } } diff --git a/components/bounty-form.js b/components/bounty-form.js index f43a8616..4ada054e 100644 --- a/components/bounty-form.js +++ b/components/bounty-form.js @@ -7,6 +7,7 @@ import AdvPostForm, { AdvPostInitial } from './adv-post-form' import FeeButton, { EditFeeButton } from './fee-button' import { InputGroup } from 'react-bootstrap' import { bountySchema } from '../lib/validate' +import { SubSelectInitial } from './sub-select-form' export function BountyForm ({ item, @@ -17,7 +18,8 @@ export function BountyForm ({ textLabel = 'text', buttonText = 'post', adv, - handleSubmit + handleSubmit, + children }) { const router = useRouter() const client = useApolloClient() @@ -54,7 +56,8 @@ export function BountyForm ({ title: item?.title || '', text: item?.text || '', bounty: item?.bounty || 1000, - ...AdvPostInitial({ forward: item?.fwdUser?.name }) + ...AdvPostInitial({ forward: item?.fwdUser?.name }), + ...SubSelectInitial({ sub: item?.subName || sub?.name }) }} schema={schema} onSubmit={ @@ -83,6 +86,7 @@ export function BountyForm ({ } storageKeyPrefix={item ? undefined : 'bounty'} > + {children} { @@ -71,6 +73,7 @@ export function DiscussionForm ({ })} storageKeyPrefix={item ? undefined : 'discussion'} > + {children} { - field.onChange(e) + if (field?.onChange) { + field.onChange(e) + } if (onChange) { onChange(formik, e) } }} custom + isInvalid={invalid} > {items.map(item => )} + + {meta.touched && meta.error} + ) } diff --git a/components/job-form.js b/components/job-form.js index 5c97f1f8..b84d8543 100644 --- a/components/job-form.js +++ b/components/job-form.js @@ -50,7 +50,7 @@ export default function JobForm ({ item, sub }) { return ( <>
{ @@ -106,6 +108,7 @@ export function LinkForm ({ item, sub, editThreshold }) { }} storageKeyPrefix={item ? undefined : 'link'} > + {children} { @@ -61,6 +63,7 @@ export function PollForm ({ item, sub, editThreshold }) { }} storageKeyPrefix={item ? undefined : 'poll'} > + {children} + + {me?.freePosts && me?.sats < 1 + ?
{me.freePosts} free posts left
+ : null} + + + + or + + + +
+ more
} + body={ +
+ + + + or + + + +
+ + + +
+
+ } + /> + + + ) + } + + let FormType = JobForm + if (type === 'discussion') { + FormType = DiscussionForm + } else if (type === 'link') { + FormType = LinkForm + } else if (type === 'poll') { + FormType = PollForm + } else if (type === 'bounty') { + FormType = BountyForm + } + + return {children} +} + +export default function Post ({ sub }) { + const router = useRouter() + let type = router.query.type + + if (sub?.postTypes?.length === 1) { + type = sub.postTypes[0].toLowerCase() + } + + return ( + <> + + {sub?.name !== 'jobs' && } + + + ) +} diff --git a/components/sub-select-form.js b/components/sub-select-form.js new file mode 100644 index 00000000..f4406bef --- /dev/null +++ b/components/sub-select-form.js @@ -0,0 +1,55 @@ +import { useRouter } from 'next/router' +import { Select } from './form' +import Info from './info' + +export function SubSelectInitial ({ sub }) { + const router = useRouter() + sub = sub || router.query.sub || 'pick sub' + + return { + sub + } +} + +export default function SubSelect ({ label, sub, setSub, item, ...props }) { + const router = useRouter() + + const SubInfo = () => ( + +
+
The sub your post will go in ...
+
    +
  • If it's bitcoin related, put it in the bitcoin sub.
  • +
  • If it's nostr related, put it in the nostr sub.
  • +
  • If it's a job, put it in the jobs sub.
  • +
+
+
+ ) + + return ( + - // todo move the form values to the other sub's post form - router.push({ - pathname: `/~${e.target.value}/post`, - query: router.query?.type ? { type: router.query.type } : undefined - })} - name='sub' - size='sm' - items={router.query?.type ? ['bitcoin', 'nostr'] : ['bitcoin', 'nostr', 'jobs']} - /> - -
-
The sub your post will go in ...
-
    -
  • If it's bitcoin related, put it in the bitcoin sub.
  • -
  • If it's nostr related, put it in the nostr sub.
  • -
  • If it's a job, put it in the jobs sub.
  • -
-
-
- - {children} - - ) -} - -export default function Post () { +export default function PostPage () { return ( - - + ) } diff --git a/pages/~/[sub]/post.js b/pages/~/[sub]/post.js index 61bd8ec1..66c9cc5e 100644 --- a/pages/~/[sub]/post.js +++ b/pages/~/[sub]/post.js @@ -1,85 +1,15 @@ import { getGetServerSideProps } from '../../../api/ssrApollo' import { SUB } from '../../../fragments/subs' import LayoutCenter from '../../../components/layout-center' -import JobForm from '../../../components/job-form' -import Link from 'next/link' -import { Button } from 'react-bootstrap' -import AccordianItem from '../../../components/accordian-item' -import { useMe } from '../../../components/me' -import { useRouter } from 'next/router' -import { DiscussionForm } from '../../../components/discussion-form' -import { LinkForm } from '../../../components/link-form' -import { PollForm } from '../../../components/poll-form' -import { BountyForm } from '../../../components/bounty-form' -import { SubSelect } from '../../post' +import Post from '../../../components/post' export const getServerSideProps = getGetServerSideProps(SUB, null, data => !data.sub) -export function PostForm ({ type, sub }) { - const me = useMe() - - const prefix = sub?.name ? `/~${sub.name}` : '' - - if (!type) { - return ( -
- {me?.freePosts && me?.sats < 1 - ?
{me.freePosts} free posts left
- : null} - - - - or - - - -
- more
} - body={ -
- - - - or - - - -
- } - /> -
- - ) - } - - if (type === 'discussion') { - return - } else if (type === 'link') { - return - } else if (type === 'poll') { - return - } else if (type === 'bounty') { - return - } else { - return - } -} - -export default function Post ({ data: { sub } }) { - const router = useRouter() - let type = router.query.type - - if (sub.postTypes.length === 1) { - type = sub.postTypes[0].toLowerCase() - } - +export default function PostPage ({ data: { sub } }) { return ( - {sub.name !== 'jobs' && } - + ) } diff --git a/pages/~/[sub]/recent/index.js b/pages/~/[sub]/recent/index.js index ac903935..73437a08 100644 --- a/pages/~/[sub]/recent/index.js +++ b/pages/~/[sub]/recent/index.js @@ -12,7 +12,7 @@ export const getServerSideProps = getGetServerSideProps(SUB_ITEMS, variables, export default function Sub ({ data: { sub, items: { items, cursor } } }) { return ( - + {sub?.name !== 'jobs' && }