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

Commit 5c4df82

Browse files
committed
Add basic comment form
1 parent b662451 commit 5c4df82

File tree

1 file changed

+57
-15
lines changed

1 file changed

+57
-15
lines changed

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

Lines changed: 57 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 {Field, ErrorMessage} from 'formik'
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,60 @@ 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+
await postComment(
88+
author,
89+
authorEmail,
90+
authorUrl,
91+
post.databaseId,
92+
content
93+
)
94+
setSubmitting(false)
95+
}}
96+
>
97+
<label htmlFor="author">Author</label>
98+
<Field
99+
id="author"
100+
name="author"
101+
placeholder="Test Testerson"
102+
type="text"
103+
/>
104+
<ErrorMessage name="author" />
105+
<label htmlFor="authorEmail">Email</label>
106+
<Field
107+
id="authorEmail"
108+
name="authorEmail"
109+
placeholder="[email protected]"
110+
type="email"
111+
/>
112+
<ErrorMessage name="authorEmail" />
113+
<label htmlFor="authorUrl">Website</label>
114+
<Field
115+
id="authorUrl"
116+
name="authorUrl"
117+
placeholder="https://nextjswp.local"
118+
type="url"
119+
/>
120+
<ErrorMessage name="authorUrl" />
121+
<label htmlFor="content">Comment</label>
122+
<Field
123+
id="content"
124+
name="content"
125+
placeholder="This is a test comment"
126+
type="textarea"
127+
/>
128+
<ErrorMessage name="content" />
129+
</Form>
88130
</article>
89131
</Layout>
90132
)

0 commit comments

Comments
 (0)