import { useState } from "react"; import './style.css' function isValidHttpUrl(string) { let url; try { url = new URL(string); } catch (_) { return false; } return url.protocol === "http:" || url.protocol === "https:"; } export default function CommentForm({ url, site, path }) { const [error, setError] = useState(null); const handleSubmit = e => { e.preventDefault(); let form = { name: e.target.elements.name.value, website: e.target.elements.user_website.value, message: e.target.elements.message.value, token: e.target.elements.token.value }; if (form.name.length === 0) { setError("הזינו שם."); return; } if (form.message.length === 0) { setError("תגובה ריקה."); return; } if (form.token.length === 0) { setError("תשובה לשאלת סינון חסרה."); return; } if (form.website.length > 0 && !isValidHttpUrl(form.website)) { setError("אתר לא תקין. אולי שכחתם לציין את הפרוטוקול?"); return; } fetch(url + '/api/' + site + '/' + path, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(form) }) .then(response => { if (response.ok) { window.location.reload(); } else { response.json().then((err) => { console.log(err); setError(err); }); } }) .catch(error => { console.log(error); }); }; return (

כתיבת תגובה