Skip to content

Commit

Permalink
use query params to chose contentful client
Browse files Browse the repository at this point in the history
  • Loading branch information
DiogoSoaress committed Mar 21, 2024
1 parent 823b18d commit 59dcd0f
Showing 1 changed file with 10 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,22 @@ const fetchDraftContent = async (slug: string, isPreview: boolean) => {

const Page = () => {
const router = useRouter()
const { slug, secret } = router.query as { slug: string; secret: string }
const [blogPost, setBlogPost] = useState<BlogPostEntry | null>(null)
const { query, isReady } = router
const [isPreview, setIsPreview] = useState<boolean>(false)

const isPreview = Array.isArray(slug) && slug.includes('preview')
const postSlug = Array.isArray(slug) ? slug[0] : slug
const { slug, secret } = query as { slug: string; secret: string }
const [blogPost, setBlogPost] = useState<BlogPostEntry | null>(null)

useEffect(() => {
if (postSlug) {
fetchDraftContent(postSlug, isPreview).then((content) => {
if (slug && isReady) {
const isSecretValid = secret === process.env.NEXT_PUBLIC_CONTENTFUL_PREVIEW_SECRET
setIsPreview(isSecretValid)

fetchDraftContent(slug, isSecretValid).then((content) => {
setBlogPost(content)
})
}
}, [secret, postSlug, isPreview])
}, [isReady, secret, slug])

return blogPost ? <BlogPost blogPost={blogPost} isPreview={isPreview} /> : <div>Loading...</div>
}
Expand Down

0 comments on commit 59dcd0f

Please sign in to comment.