| 1 |
// packages/block-library/build-module/form/view.mjs |
| 2 |
var formSettings; |
| 3 |
try { |
| 4 |
formSettings = JSON.parse( |
| 5 |
document.getElementById( |
| 6 |
"wp-script-module-data-@wordpress/block-library/form/view" |
| 7 |
)?.textContent |
| 8 |
); |
| 9 |
} catch { |
| 10 |
} |
| 11 |
document.querySelectorAll("form.wp-block-form").forEach(function(form) { |
| 12 |
if (!formSettings || !form.action || !form.action.startsWith("mailto:")) { |
| 13 |
return; |
| 14 |
} |
| 15 |
const redirectNotification = (status) => { |
| 16 |
const urlParams = new URLSearchParams(window.location.search); |
| 17 |
urlParams.append("wp-form-result", status); |
| 18 |
window.location.search = urlParams.toString(); |
| 19 |
}; |
| 20 |
form.addEventListener("submit", async function(event) { |
| 21 |
event.preventDefault(); |
| 22 |
const formData = Object.fromEntries(new FormData(form).entries()); |
| 23 |
formData.formAction = form.action; |
| 24 |
formData._ajax_nonce = formSettings.nonce; |
| 25 |
formData.action = formSettings.action; |
| 26 |
formData._wp_http_referer = window.location.href; |
| 27 |
formData.formAction = form.action; |
| 28 |
try { |
| 29 |
const response = await fetch(formSettings.ajaxUrl, { |
| 30 |
method: "POST", |
| 31 |
headers: { |
| 32 |
"Content-Type": "application/x-www-form-urlencoded" |
| 33 |
}, |
| 34 |
body: new URLSearchParams(formData).toString() |
| 35 |
}); |
| 36 |
if (response.ok) { |
| 37 |
redirectNotification("success"); |
| 38 |
} else { |
| 39 |
redirectNotification("error"); |
| 40 |
} |
| 41 |
} catch { |
| 42 |
redirectNotification("error"); |
| 43 |
} |
| 44 |
}); |
| 45 |
}); |
| 46 |
//# sourceMappingURL=view.js.map |
| 47 |
|