accords-library.com/src/pages/news/[slug].tsx

75 lines
2.0 KiB
TypeScript
Raw Normal View History

import PostPage from "components/PostPage";
import { GetPostQuery } from "graphql/generated";
import { getReadySdk } from "graphql/sdk";
import { AppStaticProps, getAppStaticProps } from "helpers/getAppStaticProps";
import { Post } from "helpers/types";
import {
GetStaticPathsContext,
GetStaticPathsResult,
GetStaticPropsContext,
} from "next";
2022-03-17 16:05:34 +00:00
2022-04-03 08:34:21 +00:00
interface Props extends AppStaticProps {
2022-05-07 09:19:28 +00:00
post: Post;
postId: Exclude<
GetPostQuery["posts"],
null | undefined
>["data"][number]["id"];
2022-03-17 16:05:34 +00:00
}
2022-04-03 08:34:21 +00:00
export default function LibrarySlug(props: Props): JSX.Element {
const { post, langui, languages, currencies } = props;
2022-03-17 16:05:34 +00:00
return (
2022-05-07 09:19:28 +00:00
<PostPage
currencies={currencies}
languages={languages}
langui={langui}
post={post}
returnHref="/news"
returnTitle={langui.news}
displayCredits
displayThumbnailHeader
displayToc
2022-03-17 16:05:34 +00:00
/>
);
}
export async function getStaticProps(
context: GetStaticPropsContext
2022-04-03 08:34:21 +00:00
): Promise<{ notFound: boolean } | { props: Props }> {
const sdk = getReadySdk();
const slug = context.params?.slug ? context.params.slug.toString() : "";
const post = await sdk.getPost({
slug: slug,
language_code: context.locale ?? "en",
});
if (!post.posts?.data[0].attributes) return { notFound: true };
2022-04-03 08:34:21 +00:00
const props: Props = {
2022-03-17 16:05:34 +00:00
...(await getAppStaticProps(context)),
post: post.posts.data[0].attributes,
postId: post.posts.data[0].id,
2022-03-17 16:05:34 +00:00
};
return {
props: props,
};
}
2022-03-17 16:05:34 +00:00
export async function getStaticPaths(
context: GetStaticPathsContext
): Promise<GetStaticPathsResult> {
const sdk = getReadySdk();
const posts = await sdk.getPostsSlugs();
const paths: GetStaticPathsResult["paths"] = [];
if (posts.posts)
posts.posts.data.map((item) => {
context.locales?.map((local) => {
if (item.attributes)
paths.push({ params: { slug: item.attributes.slug }, locale: local });
});
2022-03-17 16:05:34 +00:00
});
return {
paths,
fallback: "blocking",
2022-03-17 16:05:34 +00:00
};
}