Skip to content
This repository was archived by the owner on Feb 27, 2024. It is now read-only.

Commit c5f5c1f

Browse files
committed
Merge branch 'feature/33-previews' into develop
2 parents 5e9f1b6 + e2e4e2e commit c5f5c1f

File tree

13 files changed

+82
-32
lines changed

13 files changed

+82
-32
lines changed

api/wordpress/_global/getPostTypeById.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import queryPostById from '../posts/queryPostById'
2-
import {initializeWpApollo} from '../connector'
2+
import {initializeWpApollo, createWpApolloClient} from '../connector'
33
import queryPageById from '../pages/queryPageById'
44
import {isHierarchicalPostType} from './postTypes'
55
import formatBlockData from '@/functions/formatBlockData'
@@ -19,14 +19,14 @@ import getMenus from '../menus/getMenus'
1919
* @param {string} postType WP post type.
2020
* @param {number | string} id Post identifier.
2121
* @param {string} idType Type of ID.
22-
* @param {boolean} preview Whether checking if post preview exists.
22+
* @param {string} preview Whether query is for a regular post view (null), a preview check (basic), or full post preview (full).
2323
* @return {object} Object containing Apollo client instance and post data or error object.
2424
*/
2525
export default async function getPostTypeById(
2626
postType,
2727
id,
2828
idType = 'SLUG',
29-
preview = false
29+
preview = null
3030
) {
3131
// Define single post query based on post type.
3232
const postTypeQuery = {
@@ -50,7 +50,9 @@ export default async function getPostTypeById(
5050
const query = postTypeQuery?.[postType] ?? null
5151

5252
// Get/create Apollo instance.
53-
const apolloClient = initializeWpApollo()
53+
const apolloClient = preview
54+
? createWpApolloClient(true)
55+
: initializeWpApollo()
5456

5557
// Set up return object.
5658
const response = {
@@ -99,7 +101,7 @@ export default async function getPostTypeById(
99101
slug: id
100102
}
101103

102-
if (preview || !post || !post?.blocksJSON) {
104+
if ('basic' === preview || !post || !post?.blocksJSON) {
103105
return post
104106
}
105107

api/wordpress/_global/getPostTypeStaticProps.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ export default async function getPostTypeStaticProps(
8282
const {apolloClient, error, ...postData} = await getPostTypeById(
8383
postType,
8484
id,
85-
idType
85+
idType,
86+
isCurrentPostPreview ? 'full' : null
8687
)
8788

8889
const props = {
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Query partial: retrieve comment fields.
2+
const commentsFields = `
3+
edges {
4+
node {
5+
databaseId
6+
content(format: RENDERED)
7+
parentDatabaseId
8+
approved
9+
id
10+
date
11+
parentId
12+
type
13+
}
14+
}
15+
`
16+
export default commentsFields
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import commentsFields from './commentsFields'
2+
3+
// Query partial: retrieve comment post fields.
4+
const commentsPostFields = `
5+
comments(first: 10) {
6+
${commentsFields}
7+
}
8+
`
9+
export default commentsPostFields
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import {gql} from '@apollo/client'
2+
import archivePageInfo from '../_partials/archivePageInfo'
3+
import commentsFields from '../_partials/commentsFields'
4+
5+
// Query: retrieve comments by post databaseId.
6+
const queryCommentsByPostId = gql`
7+
query GET_COMMENTS_BY_POST_ID(
8+
$id: ID!
9+
$first: Int
10+
$last: Int
11+
$after: String
12+
$before: String
13+
$order: OrderEnum = ASC
14+
$orderby: CommentsConnectionOrderbyEnum = COMMENT_DATE
15+
) {
16+
comments(
17+
first: $first
18+
last: $last
19+
after: $after
20+
before: $before
21+
where: {contentId: $id, order: $order, orderby: $orderby}
22+
) {
23+
${archivePageInfo}
24+
${commentsFields}
25+
}
26+
}
27+
`
28+
29+
export default queryCommentsByPostId

api/wordpress/connector.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,17 @@ let wpApolloClient
2929
* @see https://www.apollographql.com/docs/react/api/core/ApolloClient/
3030
*
3131
* @author WebDevStudios
32-
* @return {object} Apollo client instance.
32+
* @param {boolean} auth Whether to include authentication via WP application password.
33+
* @return {object} Apollo client instance.
3334
*/
34-
export function createWpApolloClient() {
35+
export function createWpApolloClient(auth = false) {
3536
return new ApolloClient({
3637
ssrMode: false,
3738
link: new HttpLink({
3839
uri: `${wpApiUrlBase}graphql`,
3940
credentials: '',
4041
headers: {
41-
authorization: `Basic ${wpAuthorization}`
42+
authorization: auth ? `Basic ${wpAuthorization}` : ''
4243
}
4344
}),
4445
cache: new InMemoryCache()

api/wordpress/posts/queryPostById.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import tagsPostFields from '../_partials/tagsPostFields'
66
import categoriesPostFields from '../_partials/categoriesPostFields'
77
import {gql} from '@apollo/client'
88
import defaultPageData from '../_partials/defaultPageData'
9+
import commentsPostFields from '../_partials/commentsPostFields'
910

1011
// Fragment: retrieve single post fields.
1112
const singlePostFragment = gql`
@@ -18,6 +19,7 @@ const singlePostFragment = gql`
1819
${featuredImagePostFields}
1920
${tagsPostFields}
2021
${categoriesPostFields}
22+
${commentsPostFields}
2123
}
2224
`
2325
// Query: retrieve post by specified identifier.

components/blocks/BlockHeadings/BlockHeadings.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import React from 'react'
99
* The core Headings block from Gutenberg.
1010
*
1111
* @author WebDevStudios
12-
* @param {object} props The component attributes as props.
12+
* @param {object} props The component attributes as props.
13+
* @param {object} props.props The block attributes props from WordPress.
14+
* @return {Element} RichText component with heading content.
1315
*/
1416
export default function BlockHeadings({props}) {
1517
const {anchor, align, className, content, level} = props

components/blocks/BlockParagraph/BlockParagraph.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import PropTypes from 'prop-types'
88
* The core Paragraph block from Gutenberg.
99
*
1010
* @author WebDevStudios
11-
* @param {object} props The component attributes as props.
11+
* @param {object} props The component attributes as props.
12+
* @param {object} props.props The block attributes props from WordPress.
13+
* @return {Element} RichText component with paragraph content.
1214
*/
1315
export default function BlockParagraph({props}) {
1416
const {className, align, anchor, content} = props

components/molecules/Blocks/Blocks.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import displayBlock from '@/functions/displayBlock'
77
*
88
* @author WebDevStudios
99
* @param {object} props The component attributes as props.
10-
* @param {array} props.blocks The array of blocks.
10+
* @param {Array} props.blocks The array of blocks.
1111
* @return {Element} The Blocks component.
1212
*/
1313
export default function Blocks({blocks}) {

0 commit comments

Comments
 (0)