CoastalCommitsPastes/client/pages/new/from/[id].tsx

79 lines
1.5 KiB
TypeScript
Raw Normal View History

2022-04-09 20:48:19 -04:00
import styles from "@styles/Home.module.css"
import NewPost from "@components/new-post"
import Header from "@components/header"
import PageSeo from "@components/page-seo"
import { Page } from "@geist-ui/core"
import Head from "next/head"
import { GetServerSideProps } from "next"
import { Post } from "@lib/types"
import cookie from "cookie"
const NewFromExisting = ({
2022-04-09 20:48:19 -04:00
post,
parentId
}: {
2022-04-09 20:48:19 -04:00
post: Post
parentId: string
}) => {
2022-04-09 20:48:19 -04:00
return (
<Page className={styles.wrapper}>
<PageSeo title="Create a new Drift" />
<Head>
{/* TODO: solve this. */}
{/* eslint-disable-next-line @next/next/no-css-tags */}
<link rel="stylesheet" href="/css/react-datepicker.css" />
</Head>
<Page.Content className={styles.main}>
<NewPost initialPost={post} newPostParent={parentId} />
</Page.Content>
</Page>
)
}
2022-04-09 20:48:19 -04:00
export const getServerSideProps: GetServerSideProps = async ({
req,
params
}) => {
const id = params?.id
const redirect = {
redirect: {
destination: "/new",
permanent: false
}
}
2022-04-09 20:48:19 -04:00
if (!id) {
return redirect
}
2022-04-09 20:48:19 -04:00
const driftToken = cookie.parse(req.headers.cookie || "")[`drift-token`]
2022-04-09 20:48:19 -04:00
const post = await fetch(`${process.env.API_URL}/posts/${id}`, {
method: "GET",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${driftToken}`,
"x-secret-key": process.env.SECRET_KEY || ""
}
})
2022-04-09 20:48:19 -04:00
if (!post.ok) {
return redirect
}
2022-04-09 20:48:19 -04:00
const data = await post.json()
2022-04-09 20:48:19 -04:00
if (!data) {
return redirect
}
2022-04-09 20:48:19 -04:00
return {
props: {
post: data,
parentId: id
}
}
}
export default NewFromExisting