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

Improview #40

Merged
merged 10 commits into from
Oct 1, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
tidier code
  • Loading branch information
phillysnow committed Sep 27, 2021
commit 603b5f8e9406a5eb474567c92e3a924d0d05e710
2 changes: 1 addition & 1 deletion pages/_document.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ class MyDocument extends Document {
rel="stylesheet"
/>
<link rel="icon" href="/favicon.png" type="image/png" />
<script src={`//static.cdn.prismic.io/prismic.js?repo=${repoName}&new=true`} />
</Head>
<style jsx global>{ reset }</style>
<style jsx global>{ globals }</style>
<body>
<Main />
<NextScript />
<script async defer src={`//static.cdn.prismic.io/prismic.js?repo=${repoName}&new=true`} />
</body>
</Html>
)
Expand Down
10 changes: 5 additions & 5 deletions pages/blog/[uid].js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ import { BackButton, SliceZone } from "components/post";
import { Client } from "utils/prismicHelpers";
import { queryRepeatableDocuments } from 'utils/queries'
import useUpdatePreviewRef from 'utils/useUpdatePreviewRef'
// import useUpdateToolbarDocs from 'utils/useUpdateToolbarDocs'
import { postStyles } from "styles";

/**
* Post page component
*/
const Post = ({ post, preview }) => {
const Post = ({ post, previewRef }) => {

useUpdatePreviewRef(preview, post.id)
useUpdatePreviewRef(previewRef, post.id)
// useUpdateToolbarDocs(articleToolbarDocs(post.uid, previewRef), [post])

if (post && post.data) {
const hasTitle = RichText.asText(post.data.title).length !== 0;
Expand Down Expand Up @@ -50,9 +52,7 @@ export async function getStaticProps({ params, previewData }) {
const post = await Client().getByUID("post", params.uid, ref ? { ref } : null) || {}
phillysnow marked this conversation as resolved.
Show resolved Hide resolved
return {
props: {
preview: {
activeRef: ref,
},
previewRef: ref,
post
}
}
Expand Down
8 changes: 3 additions & 5 deletions pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import useUpdatePreviewRef from 'utils/useUpdatePreviewRef'
/**
* Homepage component
*/
const Home = ({ doc, posts, preview }) => {
const Home = ({ doc, posts, previewRef }) => {

useUpdatePreviewRef(preview, doc.id)
useUpdatePreviewRef(previewRef, doc.id)

if (doc && doc.data) {
return (
Expand Down Expand Up @@ -53,9 +53,7 @@ export async function getStaticProps({ previewData }) {
props: {
doc,
posts: posts ? posts.results : [],
preview: {
activeRef: ref,
}
previewRef: ref,
}
}
}
Expand Down
8 changes: 0 additions & 8 deletions utils/useGetExitPreviewRoute.js

This file was deleted.

20 changes: 8 additions & 12 deletions utils/useUpdatePreviewRef.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,37 @@
import { useEffect } from 'react'
import { useRouter } from 'next/router'
import Cookies from 'js-cookie'
import useGetExitPreviewRoute from './useGetExitPreviewRoute'
import { repoName } from 'prismic-configuration'

function tryAndGetPreviewCookieObject(previewCookie) {
if (!previewCookie) return null
const prismic = previewCookie[`${repoName}.prismic.io`]

return prismic
function useGetExitPreviewRoute() {
phillysnow marked this conversation as resolved.
Show resolved Hide resolved
const router = useRouter()
phillysnow marked this conversation as resolved.
Show resolved Hide resolved
const defaultPreviewExitUrl = '/api/exit-preview'
const linkUrl = router.asPath ? `${defaultPreviewExitUrl}?currentUrl=${router.asPath}` : defaultPreviewExitUrl
return linkUrl
}

export default function useUpdatePreview(preview, documentId) {
export default function useUpdatePreview(previewRef, documentId) {
const router = useRouter()
const previewExitRoute = useGetExitPreviewRoute()
phillysnow marked this conversation as resolved.
Show resolved Hide resolved
useEffect(() => {
const rawPreviewCookie = Cookies.get('io.prismic.preview')
const previewCookie = rawPreviewCookie ? JSON.parse(rawPreviewCookie) : null

const previewCookieObject = tryAndGetPreviewCookieObject(previewCookie)
const previewCookieObject = previewCookie ? previewCookie[`${repoName}.prismic.io`] : null

const previewCookieRef = previewCookieObject && previewCookieObject.preview
? previewCookieObject.preview
: null

if (router.isPreview) {
if (rawPreviewCookie && previewCookieRef) {
if (preview.activeRef !== previewCookieRef) {
console.log('RD1')
if (previewRef !== previewCookieRef) {
return router.push(`/api/preview?token=${previewCookieRef}&documentId=${documentId}`)
}
} else {
console.log('RD2')
return router.push(previewExitRoute)
}
} else if (rawPreviewCookie && previewCookieRef) {
console.log('RD3')
return router.push(`/api/preview?token=${previewCookieRef}&documentId=${documentId}`)
}
return undefined
Expand Down
9 changes: 9 additions & 0 deletions utils/useUpdateToolbarDocs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { useEffect } from 'react'

const useUpdateToolbarDocs = (docQuery, deps) => {
useEffect(() => {
docQuery()
}, deps)
}

export default useUpdateToolbarDocs