From 8e894cfeb99a28b3d0cde6c7b0a793f79057c623 Mon Sep 17 00:00:00 2001 From: DidierRLopes Date: Sun, 24 Mar 2024 12:54:32 -0400 Subject: [PATCH] add latest blogposts in main homepage --- src/pages/index.js | 61 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/src/pages/index.js b/src/pages/index.js index 397e91531..9bdd8e513 100644 --- a/src/pages/index.js +++ b/src/pages/index.js @@ -3,6 +3,8 @@ import Layout from '@theme/Layout'; import ExecutionEnvironment from '@docusaurus/ExecutionEnvironment'; import { Carousel } from 'react-responsive-carousel'; import "react-responsive-carousel/lib/styles/carousel.min.css"; // requires a loader +import useDocusaurusContext from '@docusaurus/useDocusaurusContext'; + export default function Home() { const [isDesktop, setIsDesktop] = useState(false); @@ -13,6 +15,29 @@ export default function Home() { } }, []); + const { siteConfig } = useDocusaurusContext(); + const { url: siteUrl } = siteConfig; + const [posts, setPosts] = useState([]); + + useEffect(() => { + fetch(`${siteUrl}/blog/feed.json`) + .then((response) => { + if (!response.ok) { + throw new Error(`HTTP error! status: ${response.status}`); + } + return response.json(); + }) + .then((data) => { + const posts = data.items.slice(0, 3); + setPosts(posts); + }) + .catch((error) => { + console.log('There was a problem with the fetch operation: ' + error.message); + }); + }, []); + + console.log(posts); + return ( +
+

+ Latest Blog Posts +

+ {isDesktop ? ( +
+ {posts.map((post) => ( +
+ + {post.title} +

{post.title}

+
+
+ ))} +
+ ) : ( +
+ {posts.map((post) => ( +
+ + {post.title} +

{post.title}

+
+
+ ))} +
+ )} +
);