From 4024d2c05cb0efc65ed2e30489fe0d0f79f290c1 Mon Sep 17 00:00:00 2001 From: Stan Girard Date: Mon, 23 Oct 2023 22:00:42 +0200 Subject: [PATCH] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20blog?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit added link to rollback --- .../pages/blog/{[id] => [slug]}/index.tsx | 23 ++++++++---- frontend/pages/blog/index.tsx | 36 +++++++++++-------- 2 files changed, 38 insertions(+), 21 deletions(-) rename frontend/pages/blog/{[id] => [slug]}/index.tsx (85%) diff --git a/frontend/pages/blog/[id]/index.tsx b/frontend/pages/blog/[slug]/index.tsx similarity index 85% rename from frontend/pages/blog/[id]/index.tsx rename to frontend/pages/blog/[slug]/index.tsx index 3eebc7a24..cf4c3dab9 100644 --- a/frontend/pages/blog/[id]/index.tsx +++ b/frontend/pages/blog/[slug]/index.tsx @@ -41,6 +41,7 @@ type BlogPostAttributes = { createdAt: string; updatedAt: string; publishedAt: string; + slug: string; seo: SeoAttributes; }; @@ -56,7 +57,7 @@ export const getStaticPaths: GetStaticPaths = async () => { throw new Error('Network response was not ok'); } const data: { data: BlogPost[] } = await response.json(); - const paths = data.data.map(post => ({ params: { id: post.id.toString() } })); + const paths = data.data.map(post => ({ params: { slug: post.attributes.slug } })); return { paths, @@ -71,14 +72,21 @@ export const getStaticPaths: GetStaticPaths = async () => { } }; -export const getStaticProps = async (context: { params: { id: string } }) => { +export const getStaticProps = async (context: { params: { slug: string } }) => { try { - const response = await fetch(`https://cms.quivr.app/api/blogs/${context.params.id}?populate=seo,seo.metaImage`); - const data: { data: BlogPost } = await response.json(); + const response = await fetch(`https://cms.quivr.app/api/blogs?slug=${context.params.slug}&populate=seo,seo.metaImage`); + const data: { data: BlogPost[] } = await response.json(); + + // Find the blog post with the exact slug match + const blogPost = data.data.find(post => post.attributes.slug === context.params.slug); + + if (!blogPost) { + throw new Error('No blog post found for the provided slug'); + } return { props: { - post: data.data, + post: blogPost, }, }; } catch (error) { @@ -107,10 +115,11 @@ const BlogPostDetail = ({ post }: InferGetStaticPropsType
diff --git a/frontend/pages/blog/index.tsx b/frontend/pages/blog/index.tsx index d4e717060..0f28ddada 100644 --- a/frontend/pages/blog/index.tsx +++ b/frontend/pages/blog/index.tsx @@ -12,8 +12,11 @@ type BlogPostAttributes = { imageUrl: string; title: string; description: string; + slug: string; + publishedAt: string; seo: { metaTitle: string; + metaDescription: string; metaImage: { data: { attributes: { @@ -36,13 +39,15 @@ type BlogPost = { // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types export const getStaticProps = async () => { try { - const resulting = await fetch("https://cms.quivr.app/api/blogs?populate=seo,seo.metaImage"); - // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment + const resulting = await fetch("https://cms.quivr.app/api/blogs?populate=seo,seo.metaImage,slug"); const data: { data: BlogPost[] } = await resulting.json(); + // Sort blogs by publishedAt in descending order + const sortedBlogs = data.data.sort((a, b) => new Date(b.attributes.publishedAt).getTime() - new Date(a.attributes.publishedAt).getTime()); + return { props: { - result: data.data, + result: sortedBlogs, }, }; } catch (error) { @@ -67,10 +72,11 @@ const Blog = ({ result }: InferGetStaticPropsType) => {
@@ -81,16 +87,18 @@ const Blog = ({ result }: InferGetStaticPropsType) => {
{result.map(post => (
- Blog Post Image + + Blog Post Image +

{post.attributes.seo.metaTitle}

-

{post.attributes.description}

- +

{post.attributes.seo.metaDescription}

+ Read More