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

Commit 558b917

Browse files
committed
Show successfully posted comment
1 parent 75ed7fe commit 558b917

File tree

1 file changed

+59
-27
lines changed

1 file changed

+59
-27
lines changed

components/molecules/Comments/Comments.js

Lines changed: 59 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,52 +5,80 @@ import Form from '@/components/molecules/Form'
55
import Text from '@/components/atoms/Inputs/Text'
66
import postComment from '@/api/frontend/wp/comments/postComment'
77

8+
/**
9+
* Render an individual comment component.
10+
*
11+
* @author WebDevStudios
12+
* @param {object} props The component attributes as props.
13+
* @param {Array} props.comment The comment to display.
14+
* @return {Element} The Comment component.
15+
*/
16+
function SingleComment({comment}) {
17+
if (!comment) {
18+
return ''
19+
}
20+
const {content, date, author} = comment
21+
const {name, url} = author.node
22+
let nameElement = <span>{name}</span>
23+
if (url) {
24+
nameElement = (
25+
<a href={url} rel="nofollow noreferrer" target="_blank">
26+
{name}
27+
</a>
28+
)
29+
}
30+
return (
31+
<>
32+
<h4>
33+
{nameElement}
34+
{` at ${date}`}
35+
</h4>
36+
<div
37+
dangerouslySetInnerHTML={{
38+
__html: content
39+
}}
40+
/>
41+
<hr />
42+
</>
43+
)
44+
}
45+
46+
SingleComment.propTypes = {
47+
comment: PropTypes.object.isRequired
48+
}
49+
50+
SingleComment.defaultProps = {
51+
comment: {}
52+
}
53+
854
/**
955
* Render the Comments component.
1056
*
1157
* @author WebDevStudios
1258
* @param {object} props The component attributes as props.
1359
* @param {Array} props.comments The array of comments to display.
1460
* @param {number} props.postId The database ID of the post.
15-
* @return {Element} The Comments component.
61+
* @return {Element} The Comments component.
1662
*/
1763
export default function Comments({comments, postId}) {
1864
const [message, setMessage] = useState('')
65+
const [postedComment, setPostedComment] = useState(false)
1966

2067
return (
2168
<>
2269
<h3>Comments</h3>
2370
{
2471
// If there are comments, loop over and display.
2572
!!comments?.length &&
26-
comments.map((comment) => {
27-
const {content, date, author} = comment.node
28-
const {name, url} = author.node
29-
let nameElement = <span>{name}</span>
30-
if (url) {
31-
nameElement = (
32-
<a href={url} rel="nofollow noreferrer" target="_blank">
33-
{name}
34-
</a>
35-
)
36-
}
37-
return (
38-
<>
39-
<h4>
40-
{nameElement}
41-
{` at ${date}`}
42-
</h4>
43-
<div
44-
dangerouslySetInnerHTML={{
45-
__html: content
46-
}}
47-
/>
48-
<hr />
49-
</>
50-
)
51-
})
73+
comments.map((comment, index) => (
74+
<SingleComment comment={comment.node} key={index} />
75+
))
5276
}
5377

78+
{!!postedComment && (
79+
<SingleComment comment={postedComment} key="posted-comment" />
80+
)}
81+
5482
<Form
5583
className="comment-form"
5684
id="comment-form"
@@ -80,6 +108,10 @@ export default function Comments({comments, postId}) {
80108
'Your comment was sent and will appear after moderation.'
81109
)
82110
}
111+
112+
if (response.comment) {
113+
setPostedComment(response.comment)
114+
}
83115
setSubmitting(false)
84116
}}
85117
>

0 commit comments

Comments
 (0)