Skip to content

Commit 4d261a4

Browse files
author
gauthier witkowski
committed
Add helper functions
1 parent 4ae22cb commit 4d261a4

File tree

1 file changed

+26
-7
lines changed

1 file changed

+26
-7
lines changed

AjaxForm.js

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,44 @@
77
* Author: Raspgot
88
*/
99

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+
*/
1127

1228
document.addEventListener('DOMContentLoaded', () => {
1329
'use strict';
1430

31+
/** @type {HTMLFormElement|null} */
1532
const form = document.querySelector('.needs-validation');
1633
if (!form) return;
1734

35+
/** @type {HTMLElement|null} */
1836
const spinner = document.getElementById('loading-spinner');
37+
/** @type {HTMLButtonElement|null} */
1938
const submitButton = form.querySelector('button[type="submit"]');
39+
/** @type {HTMLElement|null} */
2040
const alertContainer = document.getElementById('alert-status');
21-
41+
/** @type {boolean} */
2242
let inFlight = false;
2343

2444
// Live validation
2545
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, () => {
2748
if (!field.value.trim()) {
2849
field.classList.remove('is-valid', 'is-invalid');
2950
} else if (field.checkValidity()) {
@@ -92,11 +113,9 @@ document.addEventListener('DOMContentLoaded', () => {
92113
body: formData,
93114
headers: { Accept: 'application/json' },
94115
});
116+
if (!response.ok) throw new Error(`⚠️ Network error: ${response.status}`);
95117

96-
if (!response.ok) {
97-
throw new Error(`⚠️ Network error: ${response.status}`);
98-
}
99-
118+
/** @type {AjaxResponse} */
100119
let result;
101120
try {
102121
result = await response.json();

0 commit comments

Comments
 (0)