| 1 |
(function () { |
| 2 |
'use strict'; |
| 3 |
|
| 4 |
function initContactForm(wrapper) { |
| 5 |
var form = wrapper.querySelector('.bkbg-cf-form'); |
| 6 |
var btn = wrapper.querySelector('.bkbg-cf-submit'); |
| 7 |
var status = wrapper.querySelector('.bkbg-cf-status'); |
| 8 |
if (!form || !btn || !status) return; |
| 9 |
|
| 10 |
var recipient = wrapper.dataset.recipient || ''; |
| 11 |
var subject = wrapper.dataset.subject || 'New message from your website'; |
| 12 |
var successMsg = wrapper.dataset.success || 'Thank you! Your message has been sent.'; |
| 13 |
var errorMsg = wrapper.dataset.error || 'Something went wrong. Please try again.'; |
| 14 |
|
| 15 |
var endpoint = (window.wpApiSettings && window.wpApiSettings.root) |
| 16 |
? window.wpApiSettings.root.replace(/\/?$/, '/') + 'blockenberg/v1/contact' |
| 17 |
: '/wp-json/blockenberg/v1/contact'; |
| 18 |
|
| 19 |
function showStatus(msg, type) { |
| 20 |
status.textContent = msg; |
| 21 |
status.className = 'bkbg-cf-status bkbg-cf-' + type; |
| 22 |
} |
| 23 |
|
| 24 |
function clearStatus() { |
| 25 |
status.className = 'bkbg-cf-status'; |
| 26 |
status.textContent = ''; |
| 27 |
} |
| 28 |
|
| 29 |
function setLoading(active) { |
| 30 |
btn.disabled = active; |
| 31 |
if (active) { |
| 32 |
btn.classList.add('is-loading'); |
| 33 |
} else { |
| 34 |
btn.classList.remove('is-loading'); |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
form.addEventListener('submit', function (e) { |
| 39 |
e.preventDefault(); |
| 40 |
clearStatus(); |
| 41 |
|
| 42 |
// Native HTML5 validation |
| 43 |
if (!form.checkValidity()) { |
| 44 |
form.reportValidity(); |
| 45 |
return; |
| 46 |
} |
| 47 |
|
| 48 |
var data = { |
| 49 |
name : (form.querySelector('[name="name"]') || {}).value || '', |
| 50 |
email : (form.querySelector('[name="email"]') || {}).value || '', |
| 51 |
phone : (form.querySelector('[name="phone"]') || {}).value || '', |
| 52 |
message : (form.querySelector('[name="message"]') || {}).value || '', |
| 53 |
website : (form.querySelector('[name="website"]') || {}).value || '', // honeypot |
| 54 |
recipient: recipient, |
| 55 |
subject : subject |
| 56 |
}; |
| 57 |
|
| 58 |
// Basic client-side email check |
| 59 |
if (!data.email || !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(data.email)) { |
| 60 |
showStatus('Please enter a valid email address.', 'error'); |
| 61 |
return; |
| 62 |
} |
| 63 |
|
| 64 |
setLoading(true); |
| 65 |
|
| 66 |
fetch(endpoint, { |
| 67 |
method : 'POST', |
| 68 |
headers: { 'Content-Type': 'application/json' }, |
| 69 |
body : JSON.stringify(data) |
| 70 |
}) |
| 71 |
.then(function (res) { |
| 72 |
return res.json().then(function (body) { |
| 73 |
return { ok: res.ok, body: body }; |
| 74 |
}); |
| 75 |
}) |
| 76 |
.then(function (result) { |
| 77 |
setLoading(false); |
| 78 |
if (result.ok && result.body && result.body.ok) { |
| 79 |
showStatus(successMsg, 'success'); |
| 80 |
form.reset(); |
| 81 |
} else { |
| 82 |
var msg = (result.body && result.body.message) ? result.body.message : errorMsg; |
| 83 |
showStatus(msg, 'error'); |
| 84 |
} |
| 85 |
}) |
| 86 |
.catch(function () { |
| 87 |
setLoading(false); |
| 88 |
showStatus(errorMsg, 'error'); |
| 89 |
}); |
| 90 |
}); |
| 91 |
} |
| 92 |
|
| 93 |
function init() { |
| 94 |
document.querySelectorAll('.bkbg-cf-wrapper').forEach(initContactForm); |
| 95 |
} |
| 96 |
|
| 97 |
if (document.readyState === 'loading') { |
| 98 |
document.addEventListener('DOMContentLoaded', init); |
| 99 |
} else { |
| 100 |
init(); |
| 101 |
} |
| 102 |
})(); |
| 103 |
|