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

Commit 7673059

Browse files
author
Mike England
authored
Merge pull request #46 from WebDevStudios/feature/45-form-query
Feature/45 form query
2 parents a428135 + 4c1f1d2 commit 7673059

File tree

6 files changed

+176
-2
lines changed

6 files changed

+176
-2
lines changed

api/wordpress/_global/getPostTypeById.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import queryPostById from '../posts/queryPostById'
22
import {initializeApollo} from '../connector'
33
import queryPageById from '../pages/queryPageById'
44
import {isHierarchicalPostType} from './postTypes'
5+
import formatBlockData from '@/functions/formatBlockData'
56

67
/**
78
* Retrieve single post by specified identifier.
@@ -43,6 +44,20 @@ export default async function getPostTypeById(postType, id, idType = 'SLUG') {
4344
const post = await apolloClient
4445
.query({query, variables: {id, idType}})
4546
.then((post) => post?.data?.[postType] ?? null)
47+
.then(async (post) => {
48+
// Handle blocks.
49+
if (!post || !post?.blocksJSON) {
50+
return post
51+
}
52+
53+
const newPost = {...post}
54+
55+
newPost.blocks = await formatBlockData(
56+
JSON.parse(newPost.blocksJSON) ?? []
57+
)
58+
59+
return newPost
60+
})
4661
.catch((error) => {
4762
return {
4863
isError: true,
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import {initializeApollo} from '../connector'
2+
import queryFormById from './queryFormById'
3+
4+
/**
5+
* Retrieve single form by ID.
6+
*
7+
* @param {string} id Form ID.
8+
* @return {Object} Post data or error object.
9+
*/
10+
export default async function getFormById(id) {
11+
// Determine form global ID.
12+
const formId = Buffer.from(`GravityFormsForm:${id}`).toString('base64')
13+
14+
// Get/create Apollo instance.
15+
const apolloClient = initializeApollo()
16+
17+
// Execute query.
18+
const form = await apolloClient
19+
.query({query: queryFormById, variables: {id: formId}})
20+
.then((form) => form?.data?.gravityFormsForm ?? null)
21+
.catch((error) => {
22+
return {
23+
isError: true,
24+
message: error.message
25+
}
26+
})
27+
28+
if (!form) {
29+
return {
30+
isError: true,
31+
message: `An error occurred while trying to retrieve data for form "${id}."`
32+
}
33+
}
34+
35+
return form
36+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
const {gql} = require('@apollo/client')
2+
3+
/**
4+
* Partial: retrieve basic data on all form fields.
5+
*
6+
* @return {string} Form fields query partial.
7+
*/
8+
function getFormFieldsPartial() {
9+
const fields = [
10+
'AddressField',
11+
'CaptchaField',
12+
'ChainedSelectField',
13+
'CheckboxField',
14+
'DateField',
15+
'EmailField',
16+
'FileUploadField',
17+
'HiddenField',
18+
'HtmlField',
19+
'ListField',
20+
'MultiSelectField',
21+
'NameField',
22+
'NumberField',
23+
'PageField',
24+
'PasswordField',
25+
'PhoneField',
26+
'PostCategoryField',
27+
'PostContentField',
28+
'PostCustomField',
29+
'PostExcerptField',
30+
'PostImageField',
31+
'PostTagsField',
32+
'PostTitleField',
33+
'RadioField',
34+
'SectionField',
35+
'SignatureField',
36+
'SelectField',
37+
'TextAreaField',
38+
'TextField',
39+
'TimeField',
40+
'WebsiteField'
41+
]
42+
43+
return (
44+
fields
45+
// Build individual query partials by field type.
46+
.map(
47+
(field) => `
48+
... on ${field} {
49+
type
50+
label
51+
cssClass
52+
}
53+
`
54+
)
55+
// Connect query partial pieces.
56+
.join('')
57+
)
58+
}
59+
60+
// Fragment: retrieve single form fields.
61+
const singleFormFragment = gql`
62+
fragment SingleFormFields on GravityFormsForm {
63+
formId
64+
title
65+
description
66+
cssClass
67+
fields(first: 100) {
68+
edges {
69+
node {
70+
${getFormFieldsPartial()}
71+
}
72+
}
73+
}
74+
}
75+
`
76+
77+
// Query: retrieve form by ID.
78+
const queryFormById = gql`
79+
query GET_FORM_BY_ID($id: ID!) {
80+
gravityFormsForm(id: $id) {
81+
...SingleFormFields
82+
}
83+
}
84+
${singleFormFragment}
85+
`
86+
87+
export default queryFormById

functions/formatBlockData.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import getFormById from '@/api/wordpress/gravityForms/getFormById'
2+
3+
/**
4+
* Format and retrieve expanded block data.
5+
*
6+
* @param {Array} blocks Basic block data.
7+
* @return {Array} Formatted block data.
8+
*/
9+
export default async function formatBlockData(blocks) {
10+
if (!blocks || !blocks.length) {
11+
return []
12+
}
13+
14+
return await Promise.all(
15+
blocks.map(async (block) => {
16+
const {name, attributes} = block
17+
18+
switch (name) {
19+
case 'gravityforms/form':
20+
// Retrieve form data.
21+
attributes.formData = await getFormById(attributes.formId)
22+
break
23+
}
24+
25+
return {name, attributes}
26+
})
27+
)
28+
}

pages/[...slug].js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@ export default function Page({post}) {
2828
<section>
2929
<article>
3030
<h1 dangerouslySetInnerHTML={{__html: post?.title}} />
31-
<div dangerouslySetInnerHTML={{__html: post?.blocksJSON}} />
31+
<div
32+
dangerouslySetInnerHTML={{
33+
__html: JSON.stringify(post?.blocks ?? [])
34+
}}
35+
/>
3236
</article>
3337
</section>
3438
</div>

pages/posts/[[...slug]].js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@ export default function BlogPost({post}) {
2828
<section>
2929
<article>
3030
<h1 dangerouslySetInnerHTML={{__html: post?.title}} />
31-
<div dangerouslySetInnerHTML={{__html: post?.blocksJSON}} />
31+
<div
32+
dangerouslySetInnerHTML={{
33+
__html: JSON.stringify(post?.blocks ?? [])
34+
}}
35+
/>
3236
</article>
3337
</section>
3438
</div>

0 commit comments

Comments
 (0)