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

Commit eec9fb0

Browse files
committed
Extract comments to component
1 parent fbd16a6 commit eec9fb0

File tree

3 files changed

+95
-38
lines changed

3 files changed

+95
-38
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import PropTypes from 'prop-types'
2+
import React from 'react'
3+
import * as Yup from 'yup'
4+
import Form from '@/components/molecules/Form'
5+
import Text from '@/components/atoms/Inputs/Text'
6+
import postComment from '@/api/frontend/wp/comments/postComment'
7+
8+
/**
9+
* Render the Comments component.
10+
*
11+
* @author WebDevStudios
12+
* @param {object} props The component attributes as props.
13+
* @param {Array} props.comments The array of comments to display.
14+
* @param {number} props.postId The database ID of the post.
15+
* @return {Element} The Comments component.
16+
*/
17+
export default function Comments({comments, postId}) {
18+
return (
19+
<>
20+
<h3>Comments</h3>
21+
{
22+
// If there are comments, loop over and display.
23+
!!comments?.length &&
24+
comments.map((comment) => {
25+
const {content, date, author} = comment.node
26+
const {name, url} = author.node
27+
let nameElement = <span>{name}</span>
28+
if (url) {
29+
nameElement = (
30+
<a href={url} rel="nofollow noreferrer" target="_blank">
31+
{name}
32+
</a>
33+
)
34+
}
35+
return (
36+
<>
37+
<h4>
38+
{nameElement}
39+
{` at ${date}`}
40+
</h4>
41+
<div
42+
dangerouslySetInnerHTML={{
43+
__html: content
44+
}}
45+
/>
46+
<hr />
47+
</>
48+
)
49+
})
50+
}
51+
52+
<Form
53+
className="comment-form"
54+
id="comment-form"
55+
title="Add a comment"
56+
validationSchema={Yup.object().shape({
57+
author: Yup.string().required('This field is required.'),
58+
authorEmail: Yup.string().required('This field is required.')
59+
})}
60+
onSubmit={async (values, {setSubmitting}) => {
61+
const {author, authorEmail, authorUrl, content} = values
62+
const response = await postComment(
63+
author,
64+
authorEmail,
65+
authorUrl,
66+
postId,
67+
content
68+
)
69+
response.error
70+
? alert(response.errorMessage)
71+
: alert(JSON.stringify(response))
72+
setSubmitting(false)
73+
}}
74+
>
75+
<Text id="author" label="Author" isRequired type="text" />
76+
<Text id="authorEmail" label="Email" isRequired type="email" />
77+
<Text id="authorUrl" label="Website" type="url" />
78+
<Text id="content" label="Comment" isRequired type="text" />
79+
</Form>
80+
</>
81+
)
82+
}
83+
84+
Comments.propTypes = {
85+
comments: PropTypes.array.isRequired,
86+
postId: PropTypes.number.isRequired
87+
}
88+
89+
Comments.defaultProps = {
90+
comments: [],
91+
postId: 0
92+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export {default} from './Comments'

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

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,9 @@ import getPostTypeStaticProps from '@/api/wordpress/_global/getPostTypeStaticPro
33
import Layout from '@/components/common/Layout'
44
import Link from 'next/link'
55
import getArchivePosts from '@/api/frontend/wp/archive/getArchivePosts'
6-
import postComment from '@/api/frontend/wp/comments/postComment'
76
import getPagePropTypes from '@/functions/getPagePropTypes'
87
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'
8+
import Comments from '@/components/molecules/Comments'
129

1310
// Define route post type.
1411
const postType = 'post'
@@ -68,40 +65,7 @@ export default function BlogPost({post, archive, posts, pagination}) {
6865
<Layout seo={{...post?.seo}} hasJsonLd={true}>
6966
<article className="container py-40">
7067
<Blocks blocks={post?.blocks} />
71-
<div
72-
dangerouslySetInnerHTML={{
73-
__html: JSON.stringify(post?.comments ?? [])
74-
}}
75-
/>
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>
68+
<Comments comments={post?.comments?.edges} postId={post.databaseId} />
10569
</article>
10670
</Layout>
10771
)

0 commit comments

Comments
 (0)