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

Commit 5d17780

Browse files
author
Greg Rickaby
authored
Merge pull request #132 from WebDevStudios/feature/60-basic-comment-form
Feature/60 basic comment form
2 parents 5b38670 + cdad426 commit 5d17780

File tree

4 files changed

+41
-25
lines changed

4 files changed

+41
-25
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 = {

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
)

0 commit comments

Comments
 (0)