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

Commit 1b979f3

Browse files
committed
Merge branch 'staging' of https://github.com/WebDevStudios/nextjs-wordpress-starter into feature/component-handling
2 parents 0d23d22 + 69af453 commit 1b979f3

35 files changed

+533
-1112
lines changed

api/frontend/wp/comments/mutationAddComment.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const mutationAddComment = gql`
1818
postId: $postId,
1919
content: $content
2020
) @rest(type: "Comments", path: "${wpDataEndpoints.postComment}?{args}") {
21+
success
2122
comment {
2223
${commentsFields}
2324
}

api/wordpress/_global/getPostTypeArchive.js

Lines changed: 7 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,21 @@
11
import {initializeWpApollo} from '../connector'
22
import queryPostsArchive from '../posts/queryPostsArchive'
33
import {postTypes} from './postTypes'
4-
import queryEventsArchive from '../events/queryEventsArchive'
5-
import queryCareersArchive from '../careers/queryCareersArchive'
6-
import queryServicesArchive from '../services/queryServicesArchive'
74
import queryTeamsArchive from '../teams/queryTeamsArchive'
8-
import queryPortfoliosArchive from '../portfolios/queryPortfoliosArchive'
9-
import queryTestimonialsArchive from '../testimonials/queryTestimonialsArchive'
105
import formatDefaultSeoData from '@/functions/formatDefaultSeoData'
116
import getMenus from '../menus/getMenus'
127

138
// Define SEO for archives.
14-
export const archiveSeo = {
15-
career: {
16-
title: 'Careers',
17-
description: ''
18-
},
19-
event: {
20-
title: 'Events',
21-
description: ''
22-
},
23-
portfolio: {
24-
title: 'Portfolio',
25-
description: ''
26-
},
9+
export const archiveQuerySeo = {
2710
post: {
11+
query: queryPostsArchive,
2812
title: 'Blog',
2913
description: ''
3014
},
31-
service: {
32-
title: 'Services',
33-
description: ''
34-
},
3515
team: {
16+
query: queryTeamsArchive,
3617
title: 'Team Members',
3718
description: ''
38-
},
39-
testimonial: {
40-
title: 'Testimonials',
41-
description: ''
4219
}
4320
}
4421

@@ -52,7 +29,7 @@ export const archiveSeo = {
5229
* @param {string} cursor Start/end cursor for pagination.
5330
* @param {boolean} getNext Whether to retrieve next set of posts (true) or previous set (false).
5431
* @param {number} perPage Number of posts per page.
55-
* @return {object} Object containing Apollo client instance and post archive data or error object.
32+
* @return {object} Object containing Apollo client instance and post archive data or error object.
5633
*/
5734
export default async function getPostTypeArchive(
5835
postType,
@@ -62,19 +39,8 @@ export default async function getPostTypeArchive(
6239
getNext = true,
6340
perPage = 10
6441
) {
65-
// Define single post query based on post type.
66-
const postTypeQuery = {
67-
career: queryCareersArchive,
68-
event: queryEventsArchive,
69-
portfolio: queryPortfoliosArchive,
70-
post: queryPostsArchive,
71-
service: queryServicesArchive,
72-
team: queryTeamsArchive,
73-
testimonial: queryTestimonialsArchive
74-
}
75-
7642
// Retrieve post type query.
77-
const query = postTypeQuery?.[postType] ?? null
43+
const query = archiveQuerySeo?.[postType] ?? null
7844

7945
// Get/create Apollo instance.
8046
const apolloClient = initializeWpApollo()
@@ -144,10 +110,10 @@ export default async function getPostTypeArchive(
144110
}`
145111
}
146112
: {
147-
title: `${archiveSeo?.[postType]?.title} - ${
113+
title: `${archiveQuerySeo?.[postType]?.title} - ${
148114
response.defaultSeo?.openGraph?.siteName ?? ''
149115
}`,
150-
metaDesc: archiveSeo?.[postType]?.description,
116+
metaDesc: archiveQuerySeo?.[postType]?.description,
151117
canonical: `${response.defaultSeo?.openGraph?.url ?? ''}/${
152118
postTypes?.[postType]?.route
153119
}`,
Lines changed: 4 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,8 @@
11
import queryPostById from '../posts/queryPostById'
2-
import {initializeWpApollo, createWpApolloClient} from '../connector'
32
import queryPageById from '../pages/queryPageById'
43
import {isHierarchicalPostType} from './postTypes'
5-
import formatBlockData from '@/functions/formatBlockData'
6-
import queryEventById from '../events/queryEventById'
7-
import queryCareerById from '../careers/queryCareerById'
8-
import queryServiceById from '../services/queryServiceById'
94
import queryTeamById from '../teams/queryTeamById'
10-
import queryPortfolioById from '../portfolios/queryPortfolioById'
11-
import queryTestimonialById from '../testimonials/queryTestimonialById'
12-
import formatDefaultSeoData from '@/functions/formatDefaultSeoData'
13-
import getMenus from '../menus/getMenus'
5+
import processPostTypeQuery from './processPostTypeQuery'
146

157
/**
168
* Retrieve single post by specified identifier.
@@ -20,7 +12,7 @@ import getMenus from '../menus/getMenus'
2012
* @param {number | string} id Post identifier.
2113
* @param {string} idType Type of ID.
2214
* @param {string} preview Whether query is for a regular post view (null), a preview check (basic), or full post preview (full).
23-
* @return {object} Object containing Apollo client instance and post data or error object.
15+
* @return {object} Object containing Apollo client instance and post data or error object.
2416
*/
2517
export default async function getPostTypeById(
2618
postType,
@@ -30,14 +22,9 @@ export default async function getPostTypeById(
3022
) {
3123
// Define single post query based on post type.
3224
const postTypeQuery = {
33-
career: queryCareerById,
34-
event: queryEventById,
3525
page: queryPageById,
36-
portfolio: queryPortfolioById,
3726
post: queryPostById,
38-
service: queryServiceById,
39-
team: queryTeamById,
40-
testimonial: queryTestimonialById
27+
team: queryTeamById
4128
}
4229

4330
// Check if post type is hierarchical.
@@ -49,77 +36,5 @@ export default async function getPostTypeById(
4936
// Retrieve post type query.
5037
const query = postTypeQuery?.[postType] ?? null
5138

52-
// Get/create Apollo instance.
53-
const apolloClient = preview
54-
? createWpApolloClient(true)
55-
: initializeWpApollo()
56-
57-
// Set up return object.
58-
const response = {
59-
apolloClient,
60-
error: false,
61-
errorMessage: null
62-
}
63-
64-
// If no query is set for given post type, return error message.
65-
if (!query) {
66-
return {
67-
apolloClient,
68-
error: true,
69-
errorMessage: `Post type \`${postType}\` is not supported.`
70-
}
71-
}
72-
73-
// Execute query.
74-
response.post = await apolloClient
75-
.query({query, variables: {id, idType}})
76-
.then((res) => {
77-
const {homepageSettings, siteSeo, menus, ...postData} = res.data
78-
79-
// Retrieve menus.
80-
response.menus = getMenus(menus)
81-
82-
// Retrieve default SEO data.
83-
response.defaultSeo = formatDefaultSeoData({homepageSettings, siteSeo})
84-
85-
const post = postData?.[postType]
86-
87-
// Set error props if data not found.
88-
if (!post) {
89-
response.error = true
90-
response.errorMessage = `An error occurred while trying to retrieve data for ${postType} "${id}."`
91-
92-
return null
93-
}
94-
95-
return post
96-
})
97-
.then(async (post) => {
98-
// Add slug/ID to post.
99-
const newPost = {
100-
...post,
101-
slug: id
102-
}
103-
104-
if ('basic' === preview || !post || !post?.blocksJSON) {
105-
return post
106-
}
107-
108-
// Handle blocks.
109-
newPost.blocks = await formatBlockData(
110-
JSON.parse(newPost.blocksJSON) ?? []
111-
)
112-
113-
delete newPost.blocksJSON
114-
115-
return newPost
116-
})
117-
.catch((error) => {
118-
response.error = true
119-
response.errorMessage = error.message
120-
121-
return null
122-
})
123-
124-
return response
39+
return processPostTypeQuery(postType, id, query, {id, idType}, preview)
12540
}

api/wordpress/_global/getPostTypeStaticProps.js

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import {algoliaIndexName} from '@/api/algolia/connector'
22
import getPostTypeById from './getPostTypeById'
3-
import getPostTypeArchive from './getPostTypeArchive'
3+
import getPostTypeArchive, {archiveQuerySeo} from './getPostTypeArchive'
44
import {addApolloState} from '@/api/apolloConfig'
55
import getFrontendPage, {frontendPageSeo} from './getFrontendPage'
6+
import getSettingsCustomPage, {
7+
customPageQuerySeo
8+
} from './getSettingsCustomPage'
9+
import getPostTypeTaxonomyArchive from './getPostTypeTaxonomyArchive'
610

711
/**
812
* Retrieve static props by post type.
@@ -12,7 +16,7 @@ import getFrontendPage, {frontendPageSeo} from './getFrontendPage'
1216
* @param {string} postType Post Type.
1317
* @param {boolean} preview Whether requesting preview of post.
1418
* @param {object} previewData Post preview data.
15-
* @return {object} Object containing post props and revalidate setting.
19+
* @return {object} Object containing post props and revalidate setting.
1620
*/
1721
export default async function getPostTypeStaticProps(
1822
params,
@@ -43,6 +47,20 @@ export default async function getPostTypeStaticProps(
4347
})
4448
}
4549

50+
/* -- Fallback: return error if params missing. -- */
51+
if (!params) {
52+
return '404' !== postType
53+
? {
54+
notFound: true
55+
}
56+
: {
57+
props: {
58+
...sharedProps
59+
},
60+
revalidate
61+
}
62+
}
63+
4664
/* -- Handle dynamic archive display. -- */
4765
if (!Object.keys(params).length) {
4866
const {apolloClient, ...archiveData} = await getPostTypeArchive(postType)
@@ -58,11 +76,60 @@ export default async function getPostTypeStaticProps(
5876
})
5977
}
6078

79+
/* -- Handle taxonomy archives. -- */
80+
if (
81+
Object.keys(archiveQuerySeo).includes(postType) &&
82+
params.slug.length > 1
83+
) {
84+
const taxonomy = params.slug.shift()
85+
const taxonomySlug = params.slug.join('/')
86+
87+
const {apolloClient, ...archiveData} = await getPostTypeTaxonomyArchive(
88+
taxonomy,
89+
taxonomySlug
90+
)
91+
92+
// Merge in query results as Apollo state.
93+
return addApolloState(apolloClient, {
94+
props: {
95+
...archiveData,
96+
...sharedProps,
97+
archive: true
98+
},
99+
revalidate
100+
})
101+
}
102+
61103
/* -- Handle individual posts. -- */
62104

63105
// Handle catch-all routes.
64106
const slug = Array.isArray(params.slug) ? params.slug.join('/') : params.slug
65107

108+
/* -- Handle pages set via Additional Settings. -- */
109+
if (Object.keys(customPageQuerySeo).includes(slug)) {
110+
const {apolloClient, ...pageData} = await getSettingsCustomPage(slug)
111+
112+
// Display 404 error page if error encountered.
113+
if (pageData.error && '404' !== slug) {
114+
return {
115+
notFound: true
116+
}
117+
}
118+
119+
// Remove error prop.
120+
delete pageData?.error
121+
122+
return addApolloState(apolloClient, {
123+
props: {
124+
...pageData,
125+
...sharedProps
126+
},
127+
revalidate
128+
})
129+
}
130+
131+
/* -- Handle dynamic posts. -- */
132+
66133
// Get post identifier (ID or slug).
67134
const postId = Number.isInteger(Number(slug)) ? Number(slug) : slug
68135

@@ -99,6 +166,13 @@ export default async function getPostTypeStaticProps(
99166
props.error = false
100167
}
101168

169+
// Display 404 error page if error encountered.
170+
if (props.error) {
171+
return {
172+
notFound: true
173+
}
174+
}
175+
102176
// Merge in query results as Apollo state.
103177
return addApolloState(apolloClient, {
104178
props,

0 commit comments

Comments
 (0)