|
| 1 | +(function () { |
| 2 | + const endpoint = |
| 3 | + 'https://defaultb5687cc343ce40ed91fd6a67198489.81.environment.api.powerplatform.com:443/powerautomate/automations/direct/workflows/67f7bf7e639044d8a78de32f1691dfa1/triggers/manual/paths/invoke?api-version=1&sp=%2Ftriggers%2Fmanual%2Frun&sv=1.0&sig=U8kmHBuwfedGCM7VufLr_6FtbDcgLhgwSTbeE26hdQs'; |
| 4 | + |
| 5 | + const forms = document.querySelectorAll('[data-contact-form]'); |
| 6 | + |
| 7 | + if (!forms.length) { |
| 8 | + return; |
| 9 | + } |
| 10 | + |
| 11 | + const setStatus = (form, message, state) => { |
| 12 | + const status = form.querySelector('[data-contact-status]'); |
| 13 | + |
| 14 | + if (!status) { |
| 15 | + return; |
| 16 | + } |
| 17 | + |
| 18 | + status.textContent = message; |
| 19 | + status.dataset.state = state; |
| 20 | + }; |
| 21 | + |
| 22 | + const getValue = (formData, key) => String(formData.get(key) || '').trim(); |
| 23 | + |
| 24 | + forms.forEach((form) => { |
| 25 | + form.addEventListener('submit', async (event) => { |
| 26 | + event.preventDefault(); |
| 27 | + |
| 28 | + if (!form.reportValidity()) { |
| 29 | + return; |
| 30 | + } |
| 31 | + |
| 32 | + const formData = new FormData(form); |
| 33 | + const honeypot = getValue(formData, 'company'); |
| 34 | + |
| 35 | + if (honeypot) { |
| 36 | + form.reset(); |
| 37 | + setStatus(form, 'Thanks, your message has been sent.', 'success'); |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + const submitButton = form.querySelector('button[type="submit"]'); |
| 42 | + const originalButtonText = submitButton ? submitButton.textContent : ''; |
| 43 | + const payload = { |
| 44 | + firstName: getValue(formData, 'firstName'), |
| 45 | + lastName: getValue(formData, 'lastName'), |
| 46 | + email: getValue(formData, 'email'), |
| 47 | + message: getValue(formData, 'message'), |
| 48 | + sourcePage: window.location.href, |
| 49 | + formSource: form.dataset.formSource || 'Website', |
| 50 | + siteName: 'collabor8 Architecture + Design (BC)', |
| 51 | + submittedAt: new Date().toISOString(), |
| 52 | + }; |
| 53 | + |
| 54 | + setStatus(form, 'Sending...', 'pending'); |
| 55 | + |
| 56 | + if (submitButton) { |
| 57 | + submitButton.disabled = true; |
| 58 | + submitButton.textContent = 'Sending'; |
| 59 | + } |
| 60 | + |
| 61 | + try { |
| 62 | + const response = await fetch(endpoint, { |
| 63 | + method: 'POST', |
| 64 | + headers: { |
| 65 | + 'Content-Type': 'application/json', |
| 66 | + }, |
| 67 | + body: JSON.stringify(payload), |
| 68 | + }); |
| 69 | + |
| 70 | + if (!response.ok) { |
| 71 | + throw new Error(`Power Automate returned ${response.status}`); |
| 72 | + } |
| 73 | + |
| 74 | + form.reset(); |
| 75 | + setStatus(form, 'Thanks, your message has been sent.', 'success'); |
| 76 | + } catch (error) { |
| 77 | + console.error('Contact form submission failed:', error); |
| 78 | + setStatus( |
| 79 | + form, |
| 80 | + 'Sorry, something went wrong. Please email info@c8bc.ca directly.', |
| 81 | + 'error', |
| 82 | + ); |
| 83 | + } finally { |
| 84 | + if (submitButton) { |
| 85 | + submitButton.disabled = false; |
| 86 | + submitButton.textContent = originalButtonText || 'Submit'; |
| 87 | + } |
| 88 | + } |
| 89 | + }); |
| 90 | + }); |
| 91 | +})(); |
0 commit comments