@@ -5,52 +5,80 @@ import Form from '@/components/molecules/Form'
5
5
import Text from '@/components/atoms/Inputs/Text'
6
6
import postComment from '@/api/frontend/wp/comments/postComment'
7
7
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
+
8
54
/**
9
55
* Render the Comments component.
10
56
*
11
57
* @author WebDevStudios
12
58
* @param {object } props The component attributes as props.
13
59
* @param {Array } props.comments The array of comments to display.
14
60
* @param {number } props.postId The database ID of the post.
15
- * @return {Element } The Comments component.
61
+ * @return {Element } The Comments component.
16
62
*/
17
63
export default function Comments ( { comments, postId} ) {
18
64
const [ message , setMessage ] = useState ( '' )
65
+ const [ postedComment , setPostedComment ] = useState ( false )
19
66
20
67
return (
21
68
< >
22
69
< h3 > Comments</ h3 >
23
70
{
24
71
// If there are comments, loop over and display.
25
72
! ! 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
+ ) )
52
76
}
53
77
78
+ { ! ! postedComment && (
79
+ < SingleComment comment = { postedComment } key = "posted-comment" />
80
+ ) }
81
+
54
82
< Form
55
83
className = "comment-form"
56
84
id = "comment-form"
@@ -80,6 +108,10 @@ export default function Comments({comments, postId}) {
80
108
'Your comment was sent and will appear after moderation.'
81
109
)
82
110
}
111
+
112
+ if ( response . comment ) {
113
+ setPostedComment ( response . comment )
114
+ }
83
115
setSubmitting ( false )
84
116
} }
85
117
>
0 commit comments