From beb54b0acb7721068bc36da6906bd615eb01fb8c Mon Sep 17 00:00:00 2001 From: Andrew Lee Date: Sat, 29 Jun 2024 08:38:44 -0400 Subject: Merged Guestbook and Blog Comments into HOC --- src/components/BlogCommentsForm.jsx | 150 ++++++++---------------------------- 1 file changed, 31 insertions(+), 119 deletions(-) (limited to 'src/components/BlogCommentsForm.jsx') diff --git a/src/components/BlogCommentsForm.jsx b/src/components/BlogCommentsForm.jsx index b48236e..d0c585c 100644 --- a/src/components/BlogCommentsForm.jsx +++ b/src/components/BlogCommentsForm.jsx @@ -1,124 +1,36 @@ -import { Component } from 'preact'; +import { h } from 'preact'; +import withFormHandling from './FormHandling'; import '../styles/Form.css'; -import { marked } from "marked"; -import DOMPurify from 'dompurify'; -class BlogCommentsForm extends Component { - state = { - author: '', - comment: '', - isMessageSent: false, - commentId: '' - }; - - componentDidMount() { - // Ensure the Turnstile script is loaded with explicit rendering - const script = document.createElement('script'); - script.src = 'https://challenges.cloudflare.com/turnstile/v0/api.js?render=explicit&onload=onloadTurnstileCallback'; - script.defer = true; - document.body.appendChild(script); - - // Callback function to handle Turnstile token - window.onloadTurnstileCallback = () => { - window.turnstile.render('#turnstile-container', { - sitekey: '0x4AAAAAAAdb4uvxFFzNEDxB', - callback: (token) => { - // Here you can handle the token, e.g., by storing it in a hidden form field - this.setState({turnstileToken: token}); - }, - }); - }; - - // Cleanup function to remove the script when the component unmounts - return () => { - document.body.removeChild(script); - }; - } - - handleChange = (e) => { - this.setState({ [e.target.name]: e.target.value }); - } - - handleSubmit = async (e) => { - e.preventDefault(); - - if (this.state.isMessageSent) { - this.setState({ - errorMessage: 'You have already sent a comment.', - }); - return; - } - - try { - const messageHtml = marked(DOMPurify.sanitize(this.state.comment)); - const { isMessageSent, errorMessage, ...messageData } = this.state; // Exclude isMessageSent from the data - - const response = await fetch(`${import.meta.env.PUBLIC_API_URL}/comments/${this.props.slug}`, { - method: 'POST', - headers: { - 'Content-Type': 'application/json' - }, - body: JSON.stringify({ ...messageData, comment: messageHtml }), - }); - - if (!response.ok) { - const errorBody = await response.json(); - const errorMessage = `HTTP Error ${response.status}: ${response.statusText}
${errorBody.message}`; - - this.setState({ - errorMessage: `There was an error submitting your comment.
Details:
${errorMessage}`, - isMessageSent: false, - }); - return; - } - - const responseBody = await response.json(); - - this.setState({ - author: '', - comment: '', - isMessageSent: true, - errorMessage: '', - commentId: responseBody.id, - }); - - if (this.props.onMessageSent) { - this.props.onMessageSent(); - } - } catch (error) { - this.setState({ - errorMessage: `There was an error submitting your message.
Details:
${error.message}
Check the console for more details.`, - isMessageSent: false, - }); +const BlogCommentsForm = ({ state, handleChange, handleSubmit }) => ( +
+
+

Submit Comment

+ + + + +
+ +
+ {state.errorMessage &&

} + {state.isMessageSent && !state.errorMessage && +

+

Sent successfully!

+

(Optional)

+

Save this message ID: {state.commentId}

+

Then send it to me, to verify that you sent this comment.

+

Email me or tag/message me on these platforms.

+
} - } +
+); - render() { - return ( -
-
-

Submit Comment

- - - - -
- -
- {this.state.errorMessage &&

} - {this.state.isMessageSent && !this.state.errorMessage && -

-

Sent successfully!

-

(Optional)

-

Save this message ID: {this.state.commentId}

-

Then send it to me, to verify that you sent this comment.

-

Email me or tag/message me on these platforms.

-
- } -
- ); - } -} +const getBlogCommentsApiUrl = (props) => `${import.meta.env.PUBLIC_API_URL}/comments/${props.slug}`; -export default BlogCommentsForm; +export default withFormHandling(BlogCommentsForm, getBlogCommentsApiUrl, { + author: '', + comment: '', + commentId: '' +}); -- cgit v1.2.3