From a7f9e01235e2e96f7df25639123b537ef76a3035 Mon Sep 17 00:00:00 2001 From: johnrobertmcc Date: Tue, 1 Mar 2022 12:54:29 -0500 Subject: [PATCH] jsdocs; rmoved console logs --- .../wordpress/postTypes/getPostTypeById.js | 1 - .../postTypes/getPostTypeStaticProps.js | 12 -- .../postTypes/processPostTypeQuery.js | 2 - lib/wordpress/posts/queryCPTById.js | 5 +- pages/api/preview.js | 14 +-- pages/preview/[...slug].js | 105 ++++++++++++++++++ 6 files changed, 116 insertions(+), 23 deletions(-) create mode 100644 pages/preview/[...slug].js diff --git a/functions/wordpress/postTypes/getPostTypeById.js b/functions/wordpress/postTypes/getPostTypeById.js index 9e5abfb21..3bb36d86f 100644 --- a/functions/wordpress/postTypes/getPostTypeById.js +++ b/functions/wordpress/postTypes/getPostTypeById.js @@ -33,7 +33,6 @@ export default async function getPostTypeById( postTypeQuery[postType] = constructCPTQuery(postType) } - // console.log('jr postType query', postTypeQuery) // Fix default ID type for hierarchical posts. idType = !isHierarchical || 'SLUG' !== idType ? idType : 'URI' diff --git a/functions/wordpress/postTypes/getPostTypeStaticProps.js b/functions/wordpress/postTypes/getPostTypeStaticProps.js index ef830c950..478df2f75 100644 --- a/functions/wordpress/postTypes/getPostTypeStaticProps.js +++ b/functions/wordpress/postTypes/getPostTypeStaticProps.js @@ -34,7 +34,6 @@ export default async function getPostTypeStaticProps( } } - // console.log('--------line 37---------------') /* -- Handle Frontend-only routes. -- */ if (Object.keys(frontendPageSeo).includes(postType)) { const {apolloClient, ...routeData} = await getFrontendPage(postType) @@ -62,7 +61,6 @@ export default async function getPostTypeStaticProps( } } - // console.log('--------line 68---------------') // /* -- Handle dynamic archive display. -- */ if (!Object.keys(params).length) { const {apolloClient, ...archiveData} = await getPostTypeArchive(postType) @@ -78,7 +76,6 @@ export default async function getPostTypeStaticProps( }) } - // console.log('--------line 81---------------') /* -- Handle date-based archives. -- */ const year = Array.isArray(params?.slug) && @@ -98,8 +95,6 @@ export default async function getPostTypeStaticProps( !isNaN(params?.slug?.[2]) && parseInt(params?.slug?.[2], 10) const isDateArchive = postType === 'page' && (year || month || day) - // console.log('--------line 101---------------') - // console.log('postType', postType) if (isDateArchive) { const {apolloClient, ...archiveData} = await getPostsDateArchive( @@ -108,9 +103,6 @@ export default async function getPostTypeStaticProps( month ?? null, day ?? null ) - // console.log('--------line 110---------------') - // console.log('archiveData', archiveData) - // console.log('sharedProps', sharedProps) // Merge in query results as Apollo state. return addApolloState(apolloClient, { @@ -125,7 +117,6 @@ export default async function getPostTypeStaticProps( revalidate }) } - // console.log('--------line 124---------------') /* -- Handle individual posts. -- */ @@ -154,13 +145,11 @@ export default async function getPostTypeStaticProps( revalidate }) } - // console.log('--------line 151---------------') /* -- Handle dynamic posts. -- */ // Get post identifier (ID or slug). const postId = Number.isInteger(Number(slug)) ? Number(slug) : slug - // Check if preview mode is active and valid for current post (preview and post IDs or slugs match). const isCurrentPostPreview = preview && @@ -171,7 +160,6 @@ export default async function getPostTypeStaticProps( // Check if viewing a draft post. const isDraft = isCurrentPostPreview && 'draft' === previewData?.post?.status - // console.log('--------line 168---------------') // Set query variables. const id = isDraft ? previewData.post.id : slug diff --git a/functions/wordpress/postTypes/processPostTypeQuery.js b/functions/wordpress/postTypes/processPostTypeQuery.js index 3387f67b5..24d6c75a8 100644 --- a/functions/wordpress/postTypes/processPostTypeQuery.js +++ b/functions/wordpress/postTypes/processPostTypeQuery.js @@ -45,8 +45,6 @@ export default async function processPostTypeQuery( } } - // console.log('preview', preview) - // Execute query. response.post = await apolloClient .query({query, variables}) diff --git a/lib/wordpress/posts/queryCPTById.js b/lib/wordpress/posts/queryCPTById.js index 2a5ea190c..39104556e 100644 --- a/lib/wordpress/posts/queryCPTById.js +++ b/lib/wordpress/posts/queryCPTById.js @@ -7,7 +7,10 @@ import {gql} from '@apollo/client' // Query: retrieve post by specified identifier. /** - * @param postType + * Function used to contruct an Apollo query based on CPT. + * + * @param {string} postType The post type to return. + * @return {string} Returns the Apollo gql query. */ export default function constructCPTQuery(postType) { const queryCPTById = gql` diff --git a/pages/api/preview.js b/pages/api/preview.js index d63c941bb..46294375b 100644 --- a/pages/api/preview.js +++ b/pages/api/preview.js @@ -1,6 +1,5 @@ import getPostTypeById from '@/functions/wordpress/postTypes/getPostTypeById' import {wpPreviewSecret} from '@/lib/wordpress/connector' -import {postTypes} from '@/lib/wordpress/_config/postTypes' /** * Provide post preview functionality. @@ -41,23 +40,24 @@ export default async function preview(req, res) { throw new Error(errorMessage) } - // Set page preview data and enable preview mode. - res.setPreviewData({ + const previewData = { post: { id: post.databaseId, slug: post.slug, status: post.status, - uri: post.uri + uri: post.uri, + postType: postType } - }) + } - const baseRoute = postTypes?.[postType]?.route ?? '' + // Set page preview data and enable preview mode. + res.setPreviewData(previewData) // Redirect to post dynamic route. res.redirect( post.slug && post.uri && post.uri.indexOf('?page_id=') === -1 ? post.uri - : `${baseRoute ? `/${baseRoute}` : ''}/${post.databaseId}` + : `../../../preview/${post.databaseId}` ) } catch (error) { return res.status(error?.status || 401).json({ diff --git a/pages/preview/[...slug].js b/pages/preview/[...slug].js new file mode 100644 index 000000000..0e368ca22 --- /dev/null +++ b/pages/preview/[...slug].js @@ -0,0 +1,105 @@ +import Container from '@/components/atoms/Container' +import RichText from '@/components/atoms/RichText' +import Layout from '@/components/common/Layout' +import Blocks from '@/components/molecules/Blocks' +import Archive from '@/components/organisms/Archive' +import getPagePropTypes from '@/functions/getPagePropTypes' +import getPostTypeStaticPaths from '@/functions/wordpress/postTypes/getPostTypeStaticPaths' +import getPostTypeStaticProps from '@/functions/wordpress/postTypes/getPostTypeStaticProps' +import {PropTypes} from 'prop-types' + +/** + * Render the Page component. + * + * @author WebDevStudios + * @param {object} props The component attributes as props. + * @param {boolean} props.archive Whether displaying single post (false) or archive (true). + * @param {boolean} props.dateArchive Whether displaying single post (false) or date-based archive (true). + * @param {string} props.day Date query: day. + * @param {string} props.month Date query: month. + * @param {object} props.pagination Archive pagination data from WordPress. + * @param {object} props.post Post data from WordPress. + * @param {Array} props.posts Array of post data from WordPress. + * @param {string} props.year Date query: year. + * @return {Element} The Page component. + */ +export default function Page({ + archive, + dateArchive, + day, + month, + pagination, + post, + posts, + year +}) { + if (archive) { + return ( + + + + + + ) + } else if (dateArchive) { + return ( + + + {post?.title} + + + + ) + } + + return ( + + +
+ {post?.title} + +
+
+
+ ) +} + +/** + * Get post static paths. + * + * @author WebDevStudios + * @return {object} Object consisting of array of paths and fallback setting. + */ +export async function getStaticPaths() { + return await getPostTypeStaticPaths('page') +} + +/** + * Get post static props. + * + * @author WebDevStudios + * @param {object} context Context for current post. + * @return {object} Post props. + */ +export async function getStaticProps(context) { + const {params, preview, previewData} = context + const pT = preview ? previewData?.post?.postType : null + return getPostTypeStaticProps(params, pT, preview, previewData) +} + +Page.propTypes = { + ...getPagePropTypes('page'), + dateArchive: PropTypes.bool, + day: PropTypes.string, + month: PropTypes.string, + year: PropTypes.string +}