Skip to content

Commit

Permalink
Add static rendering of blog posts
Browse files Browse the repository at this point in the history
Opens the door to trigger rendering from spanner
  • Loading branch information
archey347 committed Jan 18, 2025
1 parent 0a83f44 commit 400331c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
10 changes: 7 additions & 3 deletions src/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import type { Metadata } from "next";
import SpannerBlog from "../components/spanner_blog"
import SpannerBlog from "@/components/blog/blog";
import { getPosts } from "@/components/blog/api_client";

export const metadata: Metadata = {
title: "Wye & Welsh LRC | Home",
};

export default function Home() {
export default async function Home() {
// This is fine because we're statically exporting anyway
let posts = await getPosts();

return (
<main>
<h1 className="text-2xl font-bold">Home</h1>
Expand All @@ -14,7 +18,7 @@ export default function Home() {

<h2 className="text-xl font-bold mt-2">Latest News</h2>

<SpannerBlog />
<SpannerBlog staticPosts={posts}/>
</main>
);
}
3 changes: 3 additions & 0 deletions src/components/blog/api_client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function getPosts() {
return fetch('https://spanner.wwlrc.co.uk/api/clubs/c/2/blog_posts').then((res) => res.json()).then((res) => { return res.rows })
}
20 changes: 8 additions & 12 deletions src/components/spanner_blog.jsx → src/components/blog/blog.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
"use client";

import { useState, useEffect } from 'react'
import { getPosts } from './api_client';

export default function SpannerBlog() {
const [posts, setPosts] = useState([])
export default function SpannerBlog({ staticPosts }: any) {
const [posts, setPosts] = useState(staticPosts)
const [isLoading, setLoading] = useState(true)

useEffect(() => {
fetch('https://spanner.wwlrc.co.uk/api/clubs/c/2/blog_posts')
.then((res) => res.json())
.then((data) => {
setPosts(data.rows)
setLoading(false)
})
}, [])
getPosts().then((posts) => {
setPosts(posts)
})
}, [])

if (isLoading) return <p>Loading...</p>
if(!posts) return <p>No Blog Posts :/</p>

return (<div>
{posts.map((post, id) => (
{posts.map((post: any, id: number) => (
<div key={id}>
<h3 className="text-l font-bold mt-2">{post.title}</h3>
<div dangerouslySetInnerHTML={{ __html: post.content }}></div>
Expand Down

0 comments on commit 400331c

Please sign in to comment.