-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: live updates on blog posts and blog landing page (#353)
* feat: hydrate blog post content on the client side * use public vars for Contentful Delivery API * hydrate Blog landing page on the client side * refactor: move isPressReleasePost to utils file * hydrate all posts on rendering the blog home * refactor: pass fetching logic to hooks * refactor: simplify data fetching hooks * modify useAllPosts * add error handling * include FIXME comment * Revert "add error handling" This reverts commit a6d4058. * refactor: move util functions out of the component files * improve FIXME description
- Loading branch information
1 parent
59cd346
commit ae356c2
Showing
15 changed files
with
114 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { type TypePostSkeleton } from '@/contentful/types' | ||
import client from '@/lib/contentful' | ||
import { type EntryCollection } from 'contentful' | ||
import { useEffect, useState } from 'react' | ||
|
||
// FIXME: This hook should make use of useSWR instead of useState but encountered issues with comparing the fallback data and fetched data | ||
export const useAllPosts = (fallbackData: EntryCollection<TypePostSkeleton, undefined, string>) => { | ||
const [localAllPosts, setLocalAllPosts] = useState<EntryCollection<TypePostSkeleton, undefined, string>>(fallbackData) | ||
|
||
useEffect(() => { | ||
client | ||
.getEntries<TypePostSkeleton>({ | ||
content_type: 'post', | ||
order: ['-fields.date'], | ||
}) | ||
.then((entry) => { | ||
setLocalAllPosts(entry) | ||
}) | ||
}, []) | ||
|
||
return { localAllPosts } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import useSWR from 'swr' | ||
import client from '@/lib/contentful' | ||
import { type BlogHomeEntry } from '@/components/Blog/BlogHome' | ||
import { type TypeBlogHomeSkeleton } from '@/contentful/types' | ||
|
||
const blogHomeFetcher = (id: string) => client.getEntry<TypeBlogHomeSkeleton>(id) | ||
|
||
export const useBlogHome = (id: string, fallbackData: BlogHomeEntry) => | ||
useSWR(id, blogHomeFetcher, { | ||
fallbackData, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import useSWR from 'swr' | ||
import client from '@/lib/contentful' | ||
import { type BlogPostEntry } from '@/components/Blog/Post' | ||
import { type TypePostSkeleton } from '@/contentful/types' | ||
|
||
const postFetcher = (id: string) => client.getEntry<TypePostSkeleton>(id) | ||
|
||
export const useBlogPost = (id: string, fallbackData: BlogPostEntry) => useSWR(id, postFetcher, { fallbackData }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
import * as contentful from 'contentful' | ||
|
||
const client = contentful.createClient({ | ||
space: process.env.CONTENTFUL_SPACE_ID, | ||
accessToken: process.env.CONTENTFUL_ACCESS_TOKEN, | ||
space: process.env.NEXT_PUBLIC_CONTENTFUL_SPACE_ID, | ||
accessToken: process.env.NEXT_PUBLIC_CONTENTFUL_ACCESS_TOKEN, | ||
}) | ||
|
||
export default client |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import type { BlogPostEntry } from '@/components/Blog/Post' | ||
|
||
export const isDraft = (post: BlogPostEntry) => post.fields.isDraft |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import type { TypePostSkeleton } from '@/contentful/types' | ||
import type { Entry } from 'contentful' | ||
|
||
export const isSelectedCategory = (post: Entry<TypePostSkeleton, undefined, string>, selectedCategory: string) => | ||
post.fields.category === selectedCategory |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters