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

Commit 75ed7fe

Browse files
committed
Better error handling
1 parent eec9fb0 commit 75ed7fe

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

components/molecules/Comments/Comments.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import PropTypes from 'prop-types'
2-
import React from 'react'
2+
import React, {useState} from 'react'
33
import * as Yup from 'yup'
44
import Form from '@/components/molecules/Form'
55
import Text from '@/components/atoms/Inputs/Text'
@@ -15,6 +15,8 @@ import postComment from '@/api/frontend/wp/comments/postComment'
1515
* @return {Element} The Comments component.
1616
*/
1717
export default function Comments({comments, postId}) {
18+
const [message, setMessage] = useState('')
19+
1820
return (
1921
<>
2022
<h3>Comments</h3>
@@ -66,12 +68,22 @@ export default function Comments({comments, postId}) {
6668
postId,
6769
content
6870
)
69-
response.error
70-
? alert(response.errorMessage)
71-
: alert(JSON.stringify(response))
71+
if (response.error) {
72+
setMessage(response.errorMessage)
73+
setSubmitting(false)
74+
return
75+
}
76+
77+
// alert(JSON.stringify(response))
78+
if (response.success && !response.comment) {
79+
setMessage(
80+
'Your comment was sent and will appear after moderation.'
81+
)
82+
}
7283
setSubmitting(false)
7384
}}
7485
>
86+
{!!message && <div>{message}</div>}
7587
<Text id="author" label="Author" isRequired type="text" />
7688
<Text id="authorEmail" label="Email" isRequired type="email" />
7789
<Text id="authorUrl" label="Website" type="url" />

0 commit comments

Comments
 (0)