-
Notifications
You must be signed in to change notification settings - Fork 30
/
page.js
59 lines (52 loc) · 1.52 KB
/
page.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import HomePageContainer from '@/components/HomePageContainer'
// daily.zooniverse.org most recent 4 posts
const DAILY_ZOO_FEED =
'https://public-api.wordpress.com/rest/v1.1/sites/57182749/posts?number=4'
// blog.zooniverse.org most recent 4 posts
const ZOO_BLOG_FEED =
'https://public-api.wordpress.com/rest/v1.1/sites/36711287/posts?number=4'
/** Grab the data we want from each post */
function parseFeedPost(post) {
return {
id: post.ID, // number
title: post.title, // string
excerpt: post.excerpt, // string but text is wrapped in <p></p>
created_at: new Date(post.date), // string such as '2024-02-02T15:00:00+00:00'
url: post.URL, // string
imageSrc: post.featured_image // ?? these are always '', might need to grab attached image instead
}
}
async function fetchBlogFeed(url) {
try {
const response = await fetch(url)
if (response.ok) {
const feed = await response.json()
return feed.posts
}
return []
} catch (error) {
console.error(error)
return []
}
}
async function getBlogPosts(url) {
let posts = []
try {
const feed = await fetchBlogFeed(url)
posts = feed.map(post => parseFeedPost(post))
return posts
} catch (error) {
console.error(error)
}
return posts
}
export default async function HomePage() {
const dailyZooPosts = await getBlogPosts(DAILY_ZOO_FEED)
const zooBlogPosts = await getBlogPosts(ZOO_BLOG_FEED)
return (
<HomePageContainer
dailyZooPosts={dailyZooPosts}
zooBlogPosts={zooBlogPosts}
/>
)
}