import { Component } from 'preact';
import '../styles/Form.css';
import { marked } from "marked";
import DOMPurify from 'dompurify';
class GuestbookForm extends Component {
state = {
name: '',
website: '',
message: '',
isMessageSent: false,
messageId: ''
};
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 message.',
});
return;
}
try {
const messageHtml = marked(DOMPurify.sanitize(this.state.message));
const { isMessageSent, errorMessage, ...messageData } = this.state; // Exclude isMessageSent from the data
const response = await fetch(`${import.meta.env.PUBLIC_API_URL}/guestbook`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ ...messageData, message: 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 message.
Details:
${errorMessage}`,
isMessageSent: false,
});
return;
}
const responseBody = await response.json();
this.setState({
name: '',
website: '',
message: '',
isMessageSent: true,
errorMessage: '',
messageId: 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,
});
}
}
render() {
return (