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

Commit 47d57f7

Browse files
committed
Merge branch 'staging' of https://github.com/WebDevStudios/nextjs-wordpress-starter into feature/component-handling
2 parents c19f32d + 227ed3f commit 47d57f7

File tree

6 files changed

+51
-35
lines changed

6 files changed

+51
-35
lines changed

api/frontend/wp/comments/postComment.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export default async function postComment(
4545
})
4646
.then(
4747
(response) =>
48-
response?.data?.createComment ?? {
48+
response?.data?.postComment ?? {
4949
error: true,
5050
errorMessage: `An error occurred while trying to post the comment.`
5151
}

components/molecules/Form/Form.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,22 @@ import cn from 'classnames'
1313
* @param {object} props.formDefaults Formik default data.
1414
* @param {string|number} props.id Form id.
1515
* @param {object} props.validationSchema Yup validation schema object.
16+
* @param {Function} props.onSubmit Function to execute when form is submitted
1617
* @return {Element} The Form component.
1718
*/
1819
export default function Form({
1920
children,
2021
className,
2122
formDefaults,
2223
id,
23-
validationSchema
24+
validationSchema,
25+
onSubmit
2426
}) {
2527
return (
2628
<Formik
2729
initialValues={formDefaults}
2830
validationSchema={validationSchema}
29-
onSubmit={(values, {setSubmitting}) => {
30-
setTimeout(() => {
31-
alert(JSON.stringify(values, null, 2))
32-
setSubmitting(false)
33-
}, 400)
34-
}}
31+
onSubmit={onSubmit}
3532
>
3633
<FormikForm id={id} className={cn(styles.form, className)}>
3734
{children}
@@ -46,7 +43,8 @@ Form.propTypes = {
4643
className: PropTypes.string,
4744
formDefaults: PropTypes.object,
4845
id: PropTypes.string,
49-
validationSchema: PropTypes.object
46+
validationSchema: PropTypes.object,
47+
onSubmit: PropTypes.func
5048
}
5149

5250
Form.defaultProps = {

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,11 @@
6464
"eslint-plugin-prettier": "^3.3.1",
6565
"eslint-plugin-react": "^7.22.0",
6666
"eslint-plugin-react-hooks": "^4.2.0",
67-
"graphql": "^15.4.0",
67+
"graphql": "^15.5.0",
6868
"husky": "^4.3.8",
6969
"lint-staged": "^10.5.3",
7070
"next-seo": "^4.17.0",
71-
"next-sitemap": "^1.4.5",
71+
"next-sitemap": "^1.4.13",
7272
"postcss-flexbugs-fixes": "^4.2.1",
7373
"postcss-preset-env": "^6.7.0",
7474
"prettier": "^2.2.1",

pages/api/wp/postComment.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ export default async function postComment(req, res) {
3333
res
3434
.status(error?.status || 500)
3535
.end(
36-
error?.message || 'An error occurred while attempted to load more posts'
36+
error?.message ||
37+
'An error occurred while attempted to post the comment'
3738
)
3839
}
3940
}

pages/blog/[[...slug]].js

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import getArchivePosts from '@/api/frontend/wp/archive/getArchivePosts'
66
import postComment from '@/api/frontend/wp/comments/postComment'
77
import getPagePropTypes from '@/functions/getPagePropTypes'
88
import Blocks from '@/components/molecules/Blocks'
9+
import Form from '@/components/molecules/Form'
10+
import Text from '@/components/atoms/Inputs/Text'
11+
import * as Yup from 'yup'
912

1013
// Define route post type.
1114
const postType = 'post'
@@ -30,20 +33,6 @@ export default function BlogPost({post, archive, posts, pagination}) {
3033
await getArchivePosts(postType, pagination?.endCursor)
3134
}
3235

33-
/**
34-
* Post a comment back to the blog
35-
*/
36-
async function postTestComment() {
37-
// TODO: get form data and post the comment
38-
await postComment(
39-
'Test Testerson',
40-
41-
'https://nextjswp.test',
42-
post.databaseId,
43-
'This is a test comment.'
44-
)
45-
}
46-
4736
// Check for post archive.
4837
// TODO create generic archive component and move this check to `_app.js`.
4938
if (archive) {
@@ -84,7 +73,35 @@ export default function BlogPost({post, archive, posts, pagination}) {
8473
__html: JSON.stringify(post?.comments ?? [])
8574
}}
8675
/>
87-
<button onClick={postTestComment}>Post test comment</button>
76+
77+
<Form
78+
className="sample-form"
79+
id="form-1"
80+
title="Add a comment"
81+
validationSchema={Yup.object().shape({
82+
author: Yup.string().required('This field is required.'),
83+
authorEmail: Yup.string().required('This field is required.')
84+
})}
85+
onSubmit={async (values, {setSubmitting}) => {
86+
const {author, authorEmail, authorUrl, content} = values
87+
const response = await postComment(
88+
author,
89+
authorEmail,
90+
authorUrl,
91+
post.databaseId,
92+
content
93+
)
94+
response.error
95+
? alert(response.errorMessage)
96+
: alert(JSON.stringify(response))
97+
setSubmitting(false)
98+
}}
99+
>
100+
<Text id="author" label="Author" isRequired type="text" />
101+
<Text id="authorEmail" label="Email" isRequired type="email" />
102+
<Text id="authorUrl" label="Website" type="url" />
103+
<Text id="content" label="Comment" isRequired type="text" />
104+
</Form>
88105
</article>
89106
</Layout>
90107
)

yarn.lock

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7047,10 +7047,10 @@ graphql-tag@^2.11.0:
70477047
resolved "https://registry.yarnpkg.com/graphql-tag/-/graphql-tag-2.11.0.tgz#1deb53a01c46a7eb401d6cb59dec86fa1cccbffd"
70487048
integrity sha512-VmsD5pJqWJnQZMUeRwrDhfgoyqcfwEkvtpANqcoUG8/tOLkwNgU9mzub/Mc78OJMhHjx7gfAMTxzdG43VGg3bA==
70497049

7050-
graphql@^15.4.0:
7051-
version "15.4.0"
7052-
resolved "https://registry.yarnpkg.com/graphql/-/graphql-15.4.0.tgz#e459dea1150da5a106486ba7276518b5295a4347"
7053-
integrity sha512-EB3zgGchcabbsU9cFe1j+yxdzKQKAbGUWRb13DsrsMN1yyfmmIq+2+L5MqVWcDCE4V89R5AyUOi7sMOGxdsYtA==
7050+
graphql@^15.5.0:
7051+
version "15.5.0"
7052+
resolved "https://registry.yarnpkg.com/graphql/-/graphql-15.5.0.tgz#39d19494dbe69d1ea719915b578bf920344a69d5"
7053+
integrity sha512-OmaM7y0kaK31NKG31q4YbD2beNYa6jBBKtMFT6gLYJljHLJr42IqJ8KX08u3Li/0ifzTU5HjmoOOrwa5BRLeDA==
70547054

70557055
gud@^1.0.0:
70567056
version "1.0.0"
@@ -9263,10 +9263,10 @@ next-seo@^4.17.0:
92639263
resolved "https://registry.yarnpkg.com/next-seo/-/next-seo-4.17.0.tgz#fb3dbe0ed7414aa9d83872ef5d8510980a6dae7e"
92649264
integrity sha512-/hnb3oq7bhi8s7bup7+Lm6VzzgRucj+JrWtp+dJiYs/04H0N/NhmE9MpepixQ16fFj6G/39JLYxhzsO/QZG/Kw==
92659265

9266-
next-sitemap@^1.4.5:
9267-
version "1.4.5"
9268-
resolved "https://registry.yarnpkg.com/next-sitemap/-/next-sitemap-1.4.5.tgz#009f99f279cab63ca64803830e467fae2aae2e27"
9269-
integrity sha512-gi7xeBiJrzcSJekT0xRh26KEEU8c+v4yenzy3BDehvm2rXzl2tOFbUoSESuQkv8iHm2Gz5WC8Og297t/VRGmkw==
9266+
next-sitemap@^1.4.13:
9267+
version "1.4.13"
9268+
resolved "https://registry.yarnpkg.com/next-sitemap/-/next-sitemap-1.4.13.tgz#2c7638f1a48a43db8aa138e53cf9c857e540052f"
9269+
integrity sha512-jaf6VsYHU5zIJUYiciN6xSrGqrMoT3xwrmLvCk+//B9jxg+aXqTdl3jKH9QYcHSf5aHo/skAmqMbiXZqaEfwjg==
92709270
dependencies:
92719271
"@corex/deepmerge" "^2.5.3"
92729272
matcher "^3.0.0"

0 commit comments

Comments
 (0)