2022-03-21 20:20:41 -04:00
|
|
|
import type { GetStaticPaths, GetStaticProps } from "next";
|
2022-03-07 23:42:44 -05:00
|
|
|
|
2022-03-22 23:06:15 -04:00
|
|
|
import type { Post } from "@lib/types";
|
2022-03-21 23:30:45 -04:00
|
|
|
import PostPage from "@components/post-page";
|
2022-03-06 19:46:59 -05:00
|
|
|
|
2022-03-22 23:06:15 -04:00
|
|
|
export type PostProps = {
|
2022-03-21 21:51:19 -04:00
|
|
|
post: Post
|
2022-03-21 17:20:20 -04:00
|
|
|
}
|
2022-03-07 01:16:08 -05:00
|
|
|
|
2022-03-22 23:06:15 -04:00
|
|
|
const PostView = ({ post }: PostProps) => {
|
|
|
|
return <PostPage post={post} />
|
2022-03-06 19:46:59 -05:00
|
|
|
}
|
|
|
|
|
2022-03-21 20:20:41 -04:00
|
|
|
export const getStaticPaths: GetStaticPaths = async () => {
|
|
|
|
const posts = await fetch(process.env.API_URL + `/posts/`, {
|
|
|
|
method: "GET",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
"x-secret-key": process.env.SECRET_KEY || "",
|
2022-03-07 01:16:08 -05:00
|
|
|
}
|
2022-03-21 20:20:41 -04:00
|
|
|
})
|
2022-03-06 19:46:59 -05:00
|
|
|
|
2022-03-21 20:20:41 -04:00
|
|
|
const json = await posts.json()
|
2022-03-24 17:53:57 -04:00
|
|
|
const filtered = json.filter((post: Post) => post.visibility === "public" || post.visibility === "unlisted")
|
|
|
|
const paths = filtered.map((post: Post) => ({
|
2022-03-21 20:20:41 -04:00
|
|
|
params: { id: post.id }
|
|
|
|
}))
|
2022-03-11 21:48:40 -05:00
|
|
|
|
2022-03-21 20:20:41 -04:00
|
|
|
return { paths, fallback: 'blocking' }
|
|
|
|
}
|
2022-03-11 21:48:40 -05:00
|
|
|
|
2022-03-21 20:20:41 -04:00
|
|
|
export const getStaticProps: GetStaticProps = async ({ params }) => {
|
|
|
|
const post = await fetch(process.env.API_URL + `/posts/${params?.id}`, {
|
|
|
|
method: "GET",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
"x-secret-key": process.env.SECRET_KEY || "",
|
2022-03-19 20:15:17 -04:00
|
|
|
}
|
2022-03-21 20:20:41 -04:00
|
|
|
})
|
2022-03-12 23:13:35 -05:00
|
|
|
|
2022-03-26 03:05:05 -04:00
|
|
|
if (!post.ok) {
|
|
|
|
return {
|
|
|
|
redirect: {
|
|
|
|
destination: "/404",
|
|
|
|
},
|
|
|
|
props: {
|
|
|
|
post: null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-15 22:49:41 -04:00
|
|
|
return {
|
|
|
|
props: {
|
2022-03-21 20:20:41 -04:00
|
|
|
post: await post.json()
|
|
|
|
},
|
2022-03-15 22:49:41 -04:00
|
|
|
}
|
2022-03-06 19:46:59 -05:00
|
|
|
}
|
|
|
|
|
2022-03-21 23:30:45 -04:00
|
|
|
export default PostView
|
2022-03-21 18:55:21 -04:00
|
|
|
|