From 3b14c0159158f402279b25145bbf1df1327ba104 Mon Sep 17 00:00:00 2001 From: Brain Date: Wed, 24 Aug 2022 15:27:20 +0200 Subject: [PATCH] Only load or do things if necessary --- assets/js/contact_form.js | 32 ++++++++++++++++++++------------ assets/js/contact_form_toggle.js | 4 +++- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/assets/js/contact_form.js b/assets/js/contact_form.js index fd2db68..43681f6 100644 --- a/assets/js/contact_form.js +++ b/assets/js/contact_form.js @@ -1,14 +1,14 @@ const contactFormAjaxUrl = '/php/contact_form.php'; -const contact_form = document.getElementsByClassName('content__contact_form')[0]; -const subject = document.getElementsByClassName('contact_form__subject')[0]; -const message = document.getElementsByClassName('contact_form__message')[0]; -const name = document.getElementsByClassName('contact_form__name')[0]; -const email = document.getElementsByClassName('contact_form__email')[0]; -const captcha = document.getElementsByClassName('contact_form__captcha')[0]; -const now = (new Date().getTime()/1000).toFixed(); -const feedback = document.getElementsByClassName('contact_form__feedback')[0]; -window.addEventListener('DOMContentLoaded', function(event) { +window.addEventListener('DOMContentLoaded', function() { + const contact_form = document.getElementsByClassName('content__contact_form')[0]; + if (contact_form) { + contact_form.addEventListener('submit', wtf_submitContactForm, false); + wtf_startContactFormSession(); + } +}); + +function wtf_startContactFormSession() { let formData = new FormData(); formData.append('action', 'start_session'); fetch(contactFormAjaxUrl, { @@ -29,10 +29,17 @@ window.addEventListener('DOMContentLoaded', function(event) { .catch(error => { console.error('Could not start the session:', error); }); -}); +} -contact_form.addEventListener('submit', function(event) { +function wtf_submitContactForm(event) { event.preventDefault(); + + const subject = document.getElementsByClassName('contact_form__subject')[0]; + const message = document.getElementsByClassName('contact_form__message')[0]; + const name = document.getElementsByClassName('contact_form__name')[0]; + const email = document.getElementsByClassName('contact_form__email')[0]; + const captcha = document.getElementsByClassName('contact_form__captcha')[0]; + let formData = new FormData(); formData.append('action', 'handle_form'); formData.append('subject', subject.value); @@ -55,6 +62,7 @@ contact_form.addEventListener('submit', function(event) { }) .then(response => response.json()) .then(json => { + const feedback = document.getElementsByClassName('contact_form__feedback')[0]; console.log(json); if (json.errors) { feedback.classList.remove('--success'); @@ -84,4 +92,4 @@ contact_form.addEventListener('submit', function(event) { } }) .catch(error => console.log(error)); -}, false); +} diff --git a/assets/js/contact_form_toggle.js b/assets/js/contact_form_toggle.js index b2dce0c..00ca58c 100644 --- a/assets/js/contact_form_toggle.js +++ b/assets/js/contact_form_toggle.js @@ -1,5 +1,7 @@ /* Unhide contact form if JS is enabled */ window.addEventListener('DOMContentLoaded', (event) => { const contact_form_wrapper = document.getElementsByClassName('content__contact_form_wrapper')[0]; - contact_form_wrapper.style.setProperty('display', 'block'); + if (contact_form_wrapper) { + contact_form_wrapper.style.setProperty('display', 'block'); + } });