2025-07-01 15:37:35 -07:00
|
|
|
document.addEventListener("DOMContentLoaded", () => {
|
|
|
|
/* make all forms semi-smart */
|
2025-07-04 14:51:49 -07:00
|
|
|
const forms = document.querySelectorAll("form[data-smart]");
|
2025-07-01 15:37:35 -07:00
|
|
|
for (const form of forms) {
|
2025-07-11 18:33:32 -07:00
|
|
|
async function on_submit(event) {
|
2025-07-01 15:37:35 -07:00
|
|
|
event.preventDefault();
|
2025-07-11 18:33:32 -07:00
|
|
|
form.disabled = true;
|
|
|
|
|
|
|
|
if (form.on_submit) {
|
|
|
|
const result = await form.on_submit(event);
|
|
|
|
if (result === false) {
|
|
|
|
form.disabled = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2025-07-01 15:37:35 -07:00
|
|
|
|
2025-07-04 15:16:51 -07:00
|
|
|
const url = form.action;
|
|
|
|
const method = form.dataset.method ?? "POST";
|
|
|
|
|
|
|
|
const json = {};
|
|
|
|
|
2025-07-01 15:37:35 -07:00
|
|
|
const form_data = new FormData(form);
|
|
|
|
for (const [key, value] of form_data.entries()) {
|
|
|
|
const elements = key.split(".");
|
2025-07-04 15:16:51 -07:00
|
|
|
let current = json;
|
2025-07-01 15:37:35 -07:00
|
|
|
for (const element of elements.slice(0, elements.length - 1)) {
|
|
|
|
current[element] = current[element] ?? {};
|
|
|
|
current = current[element];
|
|
|
|
}
|
|
|
|
|
|
|
|
current[elements.slice(elements.length - 1).shift()] = value;
|
|
|
|
}
|
|
|
|
|
2025-07-11 18:33:32 -07:00
|
|
|
if (form.on_parsed) {
|
|
|
|
await form.on_parsed(json);
|
|
|
|
}
|
|
|
|
|
2025-07-01 15:37:35 -07:00
|
|
|
try {
|
2025-07-04 14:51:49 -07:00
|
|
|
const options = {
|
|
|
|
method,
|
2025-07-01 15:37:35 -07:00
|
|
|
headers: {
|
|
|
|
Accept: "application/json",
|
|
|
|
},
|
2025-07-04 14:51:49 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
if (["POST", "PUT", "PATCH"].includes(method)) {
|
2025-07-04 15:16:51 -07:00
|
|
|
options.json = json;
|
2025-07-04 14:51:49 -07:00
|
|
|
}
|
|
|
|
|
2025-07-04 15:16:51 -07:00
|
|
|
const response = await api.fetch(url, options);
|
2025-07-01 15:37:35 -07:00
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
const error_body = await response.json();
|
|
|
|
const error = error_body?.error;
|
|
|
|
|
|
|
|
if (form.on_error) {
|
|
|
|
return form.on_error(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
alert(error.message ?? "Unknown error!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (form.on_response) {
|
2025-07-11 18:33:32 -07:00
|
|
|
await form.on_response(response);
|
|
|
|
}
|
|
|
|
|
|
|
|
const response_body = await response.json();
|
|
|
|
if (form.on_reply) {
|
|
|
|
return form.on_reply(response_body);
|
2025-07-01 15:37:35 -07:00
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
console.dir({
|
|
|
|
error,
|
|
|
|
});
|
|
|
|
|
2025-07-11 18:33:32 -07:00
|
|
|
if (form.on_error) {
|
|
|
|
return form.on_error(error);
|
2025-07-01 15:37:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
alert(error);
|
2025-07-11 18:33:32 -07:00
|
|
|
} finally {
|
|
|
|
form.disabled = false;
|
2025-07-01 15:37:35 -07:00
|
|
|
}
|
2025-07-11 18:33:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
form.addEventListener("submit", on_submit);
|
|
|
|
//form.onsubmit = on_submit;
|
2025-07-01 15:37:35 -07:00
|
|
|
}
|
|
|
|
});
|