Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Shipping log section with the last 3 posts from the blog #97

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
2 changes: 2 additions & 0 deletions packages/backend/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const ensRoutes = require("./routes/ens");
const apiRoutes = require("./routes/api");
const cohortRoutes = require("./routes/cohorts");
const notificationsRoutes = require("./routes/notifications");
const blogRoutes = require("./routes/blog");

const app = express();

Expand All @@ -31,6 +32,7 @@ app.use("/ens", ensRoutes);
app.use("/api", apiRoutes);
app.use("/cohorts", cohortRoutes);
app.use("/notifications", notificationsRoutes);
app.use("/blog", blogRoutes);

app.get("/sign-message", async (req, res) => {
const messageId = req.query.messageId;
Expand Down
40 changes: 40 additions & 0 deletions packages/backend/routes/blog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const express = require("express");
const axios = require("axios");
const xml2js = require("xml2js");

const router = express.Router();

router.get("/posts", async (req, res) => {
try {
const response = await axios.get("https://buidlguidl.substack.com/feed");
const xml = response.data;
const parser = new xml2js.Parser();

parser.parseString(xml, (err, result) => {
if (err) {
console.error("Failed to parse blog feed:", err);
res.status(500).json({ error: "Failed to parse blog feed" });
} else {
const posts = result.rss.channel[0].item.map(item => {
const date = new Date(item.pubDate[0]);
const formattedDate = `${date.toLocaleString('default', { month: 'short' }).toUpperCase()} ${date.getDate()}`;

return {
title: item.title[0],
description: item.description[0],
link: item.link[0],
pubDate: formattedDate,
imageUrl: item.enclosure ? item.enclosure[0].$.url : null
};
});

res.status(200).json(posts.slice(0, 3)); // Return the last 3 posts
}
});
} catch (error) {
console.error("Error fetching blog posts:", error);
res.status(500).json({ error: "Internal Server Error" });
}
});

module.exports = router;
38 changes: 38 additions & 0 deletions packages/react-app/components/home/BlogSection.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import React from 'react';
import { Container, VStack, Box, Heading, Text, Image, Link, Flex } from '@chakra-ui/react';
import useCustomColorModes from "../../hooks/useCustomColorModes";

const BlogSection = ({ posts }) => {
const { baseColor, secondaryFontColor } = useCustomColorModes();

if (!posts || posts.length === 0) {
return null;
}

return (
<Container maxW="container.lg" mb="50px">
<Heading fontWeight="500" mb={8} mt={20}>
Shipping Log
</Heading>

{posts.map((post, index) => (
<Link key={index} href={post.link} isExternal>
<Box bg="transparent" w="full" mb={4}>
<Box bg={baseColor} rounded="md" shadow="md" w="full">
<Flex alignItems="stretch" justifyContent="space-between" w="full">
<VStack align="start" spacing={4} flex="1" p={6}>
<Heading as="h3" size="md">{post.title}</Heading>
<Text fontSize="sm" noOfLines={3}>{post.description}</Text>
<Text color={secondaryFontColor} fontSize="sm">{post.pubDate}</Text>
</VStack>
<Image display={{ base: "none", md: "block" }} maxW="200px" src={post.imageUrl} alt={post.title} objectFit="cover" />
</Flex>
</Box>
</Box>
</Link>
))}
</Container>
);
};

export default BlogSection;
12 changes: 12 additions & 0 deletions packages/react-app/data/api/blog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import axios from "axios";
import { SERVER_URL as serverUrl } from "../../constants";

export const getRecentPosts = async () => {
try {
const response = await axios.get(`${serverUrl}/blog/posts`);
return response.data;
} catch (error) {
console.error("Error fetching recent posts:", error);
return [];
}
};
7 changes: 6 additions & 1 deletion packages/react-app/pages/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import HeroSection from "../components/home/HeroSection";
import ActivitySection from "../components/home/ActivitySection";
import { getAllBuilds, getAllEvents } from "../data/api";
import RecentBuildsSection from "../components/home/RecentBuildsSection";
import { getRecentPosts } from "../data/api/blog";
import BlogSection from "../components/home/BlogSection";

export default function Index({ bgStats, events, builds }) {
export default function Index({ bgStats, events, builds, posts }) {
return (
<>
<MetaSeo
Expand All @@ -17,6 +19,7 @@ export default function Index({ bgStats, events, builds }) {
<HeroSection {...bgStats} />
<RecentBuildsSection builds={builds} />
<ActivitySection events={events} />
<BlogSection posts={posts} />
</>
);
}
Expand All @@ -25,6 +28,7 @@ export async function getStaticProps() {
const stats = await getStats();
const events = await getAllEvents(null, 10);
const builds = (await getAllBuilds()).sort((a, b) => b.submittedTimestamp - a.submittedTimestamp).slice(0, 4);
const posts = await getRecentPosts();

return {
props: {
Expand All @@ -38,6 +42,7 @@ export async function getStaticProps() {
},
events,
builds,
posts,
},
// 2 hours caching
revalidate: 7200,
Expand Down