|
7 | 7 | * Author: Raspgot |
8 | 8 | */ |
9 | 9 |
|
10 | | -const RECAPTCHA_SITE_KEY = 'YOUR_RECAPTCHA_SITE_KEY'; // Replace with your public reCAPTCHA site key |
| 10 | +/** |
| 11 | + * Public reCAPTCHA v3 site key (frontend only) |
| 12 | + * Replace this placeholder with your actual key |
| 13 | + * @constant {string} |
| 14 | + */ |
| 15 | +const RECAPTCHA_SITE_KEY = 'YOUR_RECAPTCHA_SITE_KEY'; |
| 16 | + |
| 17 | +/** |
| 18 | + * @typedef {Object} AjaxResponse |
| 19 | + * @property {boolean} success Indicates if backend processing succeeded |
| 20 | + * @property {string} message Human readable status or error |
| 21 | + * @property {string=} field Optional form field name that failed validation |
| 22 | + */ |
| 23 | + |
| 24 | +/** |
| 25 | + * @typedef {HTMLInputElement|HTMLTextAreaElement|HTMLSelectElement} FormControl |
| 26 | + */ |
11 | 27 |
|
12 | 28 | document.addEventListener('DOMContentLoaded', () => { |
13 | 29 | 'use strict'; |
14 | 30 |
|
| 31 | + /** @type {HTMLFormElement|null} */ |
15 | 32 | const form = document.querySelector('.needs-validation'); |
16 | 33 | if (!form) return; |
17 | 34 |
|
| 35 | + /** @type {HTMLElement|null} */ |
18 | 36 | const spinner = document.getElementById('loading-spinner'); |
| 37 | + /** @type {HTMLButtonElement|null} */ |
19 | 38 | const submitButton = form.querySelector('button[type="submit"]'); |
| 39 | + /** @type {HTMLElement|null} */ |
20 | 40 | const alertContainer = document.getElementById('alert-status'); |
21 | | - |
| 41 | + /** @type {boolean} */ |
22 | 42 | let inFlight = false; |
23 | 43 |
|
24 | 44 | // Live validation |
25 | 45 | form.querySelectorAll('input, select, textarea').forEach((field) => { |
26 | | - field.addEventListener(field.tagName === 'SELECT' ? 'change' : 'input', () => { |
| 46 | + const eventName = field.tagName === 'SELECT' ? 'change' : 'input'; |
| 47 | + field.addEventListener(eventName, () => { |
27 | 48 | if (!field.value.trim()) { |
28 | 49 | field.classList.remove('is-valid', 'is-invalid'); |
29 | 50 | } else if (field.checkValidity()) { |
@@ -92,11 +113,9 @@ document.addEventListener('DOMContentLoaded', () => { |
92 | 113 | body: formData, |
93 | 114 | headers: { Accept: 'application/json' }, |
94 | 115 | }); |
| 116 | + if (!response.ok) throw new Error(`⚠️ Network error: ${response.status}`); |
95 | 117 |
|
96 | | - if (!response.ok) { |
97 | | - throw new Error(`⚠️ Network error: ${response.status}`); |
98 | | - } |
99 | | - |
| 118 | + /** @type {AjaxResponse} */ |
100 | 119 | let result; |
101 | 120 | try { |
102 | 121 | result = await response.json(); |
|
0 commit comments