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

Commit 4d7dd27

Browse files
committed
Add tax archive retrieval fn
1 parent 490607a commit 4d7dd27

File tree

1 file changed

+131
-0
lines changed

1 file changed

+131
-0
lines changed
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
import {initializeWpApollo} from '../connector'
2+
import {postTypes} from './postTypes'
3+
import formatDefaultSeoData from '@/functions/formatDefaultSeoData'
4+
import getMenus from '../menus/getMenus'
5+
import queryPostsByTag from '../tags/queryPostsByTag'
6+
7+
/**
8+
* Retrieve post taxnomy archive.
9+
*
10+
* @author WebDevStudios
11+
* @param {string} taxonomy WP taxonomy type slug.
12+
* @param {string} taxonomyId WP taxonomy term slug.
13+
* @param {string} postType WP post type.
14+
* @param {string} orderBy Order by: field.
15+
* @param {string} order Order by: direction.
16+
* @param {string} cursor Start/end cursor for pagination.
17+
* @param {boolean} getNext Whether to retrieve next set of posts (true) or previous set (false).
18+
* @param {number} perPage Number of posts per page.
19+
* @return {object} Object containing Apollo client instance and post archive data or error object.
20+
*/
21+
export default async function getPostTypeTaxonomyArchive(
22+
taxonomy,
23+
taxonomyId,
24+
postType = 'post',
25+
orderBy = 'DATE',
26+
order = 'DESC',
27+
cursor = null,
28+
getNext = true,
29+
perPage = 10
30+
) {
31+
// Define single post query based on taxonomy.
32+
const postTypeQuery = {
33+
tag: queryPostsByTag
34+
}
35+
36+
// Retrieve taxonomy query.
37+
const query = postTypeQuery?.[taxonomy] ?? null
38+
39+
// Get/create Apollo instance.
40+
const apolloClient = initializeWpApollo()
41+
42+
// Set up return object.
43+
const response = {
44+
apolloClient,
45+
posts: null,
46+
pagination: null,
47+
error: false,
48+
errorMessage: null
49+
}
50+
51+
// If no query is set for given taxonomy, return error message.
52+
if (!query) {
53+
return {
54+
apolloClient,
55+
error: true,
56+
errorMessage: `Taxonomy \`${taxonomy}\` archives are not supported.`
57+
}
58+
}
59+
60+
// Determine query variables.
61+
const variables = {
62+
id: taxonomyId,
63+
first: getNext ? perPage : null, // Only used for retrieving next set.
64+
last: getNext ? null : perPage, // Only used for retrieving previous set.
65+
after: getNext ? cursor : null, // Only used for retrieving next set.
66+
before: getNext ? null : cursor, // Only used for retrieving previous set.
67+
orderBy,
68+
order
69+
}
70+
71+
// Execute query.
72+
await apolloClient
73+
.query({query, variables})
74+
.then((archive) => {
75+
const {homepageSettings, siteSeo, menus, ...archiveData} = archive.data
76+
77+
// Retrieve menus.
78+
response.menus = getMenus(menus)
79+
80+
// Retrieve default SEO data.
81+
response.defaultSeo = formatDefaultSeoData({homepageSettings, siteSeo})
82+
83+
const data = archiveData?.[taxonomy] ?? null
84+
85+
// Get post type plural name.
86+
const pluralName = postTypes?.[postType]?.pluralName
87+
88+
// Retrieve archive SEO.
89+
const archiveSeo = data?.seo
90+
91+
// Retrieve posts by post type.
92+
const {pageInfo, edges: posts} = data?.[pluralName] ?? {}
93+
94+
// Set error props if data not found.
95+
if (!posts || !pageInfo) {
96+
response.error = true
97+
response.errorMessage = `An error occurred while trying to retrieve data for ${taxonomy} taxonomy archive.`
98+
99+
return null
100+
}
101+
102+
// Flatten posts array to include inner node post data.
103+
response.posts = posts.map((post) => post.node)
104+
105+
// Structure archive SEO.
106+
response.post = {
107+
seo: {
108+
title:
109+
archiveSeo?.title ??
110+
`${taxonomyId} - ${response.defaultSeo?.openGraph?.siteName ?? ''}`,
111+
metaDesc: archiveSeo?.metaDesc ?? '',
112+
canonical:
113+
archiveSeo?.canonical ??
114+
`${response.defaultSeo?.openGraph?.url ?? ''}/${
115+
postTypes?.[postType]?.route
116+
}/${taxonomy}/${taxonomyId}`,
117+
metaRobotsNofollow: archiveSeo?.metaRobotsNofollow ?? 'follow',
118+
metaRobotsNoindex: archiveSeo?.metaRobotsNoindex ?? 'index'
119+
}
120+
}
121+
122+
// Extract pagination data.
123+
response.pagination = pageInfo
124+
})
125+
.catch((error) => {
126+
response.error = true
127+
response.errorMessage = error.message
128+
})
129+
130+
return response
131+
}

0 commit comments

Comments
 (0)