stacker.news/pages/items/[id]/edit.js

46 lines
1.1 KiB
JavaScript
Raw Normal View History

2021-08-11 20:13:10 +00:00
import { ITEM_FIELDS } from '../../../fragments/items'
import { gql } from '@apollo/client'
2021-09-30 16:03:43 +00:00
import getSSRApolloClient from '../../../api/ssrApollo'
2021-08-11 20:13:10 +00:00
import { DiscussionForm } from '../../../components/discussion-form'
import { LinkForm } from '../../../components/link-form'
import LayoutCenter from '../../../components/layout-center'
export async function getServerSideProps ({ req, params: { id } }) {
2021-09-30 16:03:43 +00:00
const client = await getSSRApolloClient(req)
const { error, data: { item } } = await client.query({
2021-08-11 20:13:10 +00:00
query:
gql`
${ITEM_FIELDS}
{
item(id: ${id}) {
...ItemFields
text
}
}`
})
2021-09-30 15:46:58 +00:00
if (error || !item) {
2021-08-11 20:13:10 +00:00
return {
notFound: true
}
}
return {
props: {
item
}
}
}
export default function PostEdit ({ item }) {
const editThreshold = new Date(item.createdAt).getTime() + 10 * 60000
return (
<LayoutCenter>
{item.url
? <LinkForm item={item} editThreshold={editThreshold} />
: <DiscussionForm item={item} editThreshold={editThreshold} />}
</LayoutCenter>
)
}