feature: first pass on replies
refactor: move more smarts in the smartforms around
This commit is contained in:
parent
e1bb07a138
commit
ab63d4ba8d
6 changed files with 343 additions and 181 deletions
|
|
@ -1,10 +1,11 @@
|
|||
document.addEventListener("DOMContentLoaded", () => {
|
||||
/* make all forms semi-smart */
|
||||
const forms = document.querySelectorAll("form[data-smart]");
|
||||
function smarten_forms() {
|
||||
const forms = document.body.querySelectorAll("form[data-smart]:not([data-smartened])");
|
||||
for (const form of forms) {
|
||||
async function on_submit(event) {
|
||||
debugger;
|
||||
event.preventDefault();
|
||||
form.disabled = true;
|
||||
form.__submitted_at = new Date();
|
||||
|
||||
if (form.on_submit) {
|
||||
const result = await form.on_submit(event);
|
||||
|
|
@ -25,8 +26,63 @@ document.addEventListener("DOMContentLoaded", () => {
|
|||
continue;
|
||||
}
|
||||
|
||||
if (typeof value === "string" && value.length === 0) {
|
||||
const input = form.querySelector(`[name="${key}"]`);
|
||||
const input = form.querySelector(`[name="${key}"]`);
|
||||
|
||||
if (input.type === "file") {
|
||||
if (input.dataset["smartformsSaveToHome"]) {
|
||||
form.uploaded = [];
|
||||
form.errors = [];
|
||||
|
||||
const user = document.body.dataset.user
|
||||
? JSON.parse(document.body.dataset.user)
|
||||
: undefined;
|
||||
if (!user) {
|
||||
throw new Error("You must be logged in to upload files here.");
|
||||
}
|
||||
|
||||
for await (const file of input.files) {
|
||||
const body = new FormData();
|
||||
body.append("file", file, encodeURIComponent(file.name));
|
||||
|
||||
const file_path =
|
||||
"/files/users/" + user.id + "/" + encodeURIComponent(file.name);
|
||||
|
||||
const file_upload_response = await api.fetch(file_path, {
|
||||
method: "PUT",
|
||||
body,
|
||||
});
|
||||
|
||||
if (!file_upload_response.ok) {
|
||||
const error = await file_upload_response.json();
|
||||
form.errors.push(error?.error?.message ?? "Unknown error.");
|
||||
continue;
|
||||
}
|
||||
|
||||
const file_url =
|
||||
window.location.protocol + "//" + window.location.host + file_path;
|
||||
form.uploaded.push(file_url);
|
||||
}
|
||||
|
||||
if (form.errors.length) {
|
||||
const errors = form.errors.join("\n\n");
|
||||
alert(errors);
|
||||
return false;
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
const generator = input.getAttribute("generator");
|
||||
const generated_value =
|
||||
typeof generator === "string" && generator.length
|
||||
? eval(generator)(input, form)
|
||||
: undefined;
|
||||
|
||||
const resolved_value =
|
||||
typeof value === "string" && value.length ? value : generated_value;
|
||||
|
||||
if (typeof resolved_value === "undefined") {
|
||||
const should_submit_empty = input && input.dataset["smartformsSubmitEmpty"];
|
||||
if (!should_submit_empty) {
|
||||
continue;
|
||||
|
|
@ -40,11 +96,20 @@ document.addEventListener("DOMContentLoaded", () => {
|
|||
current = current[element];
|
||||
}
|
||||
|
||||
current[elements.slice(elements.length - 1).shift()] = value;
|
||||
current[elements.slice(elements.length - 1).shift()] = resolved_value;
|
||||
}
|
||||
|
||||
if (form.on_parsed) {
|
||||
await form.on_parsed(json);
|
||||
if (form.uploaded?.length > 0) {
|
||||
json.data = json.data ?? {};
|
||||
json.data.media = [...form.uploaded];
|
||||
}
|
||||
|
||||
const on_parsed =
|
||||
form.on_parsed ??
|
||||
(form.getAttribute("on_parsed") ? eval(form.getAttribute("on_parsed")) : undefined);
|
||||
|
||||
if (on_parsed) {
|
||||
await on_parsed(json);
|
||||
}
|
||||
|
||||
try {
|
||||
|
|
@ -69,7 +134,7 @@ document.addEventListener("DOMContentLoaded", () => {
|
|||
return form.on_error(error);
|
||||
}
|
||||
|
||||
alert(error.message ?? "Unknown error!");
|
||||
alert(error.message ?? "Unknown error:\n\n" + error);
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -78,8 +143,20 @@ document.addEventListener("DOMContentLoaded", () => {
|
|||
}
|
||||
|
||||
const response_body = await response.json();
|
||||
if (form.on_reply) {
|
||||
return form.on_reply(response_body);
|
||||
|
||||
const on_reply =
|
||||
form.on_reply ??
|
||||
(form.getAttribute("on_reply")
|
||||
? eval(form.getAttribute("on_reply"))
|
||||
: undefined);
|
||||
if (on_reply) {
|
||||
await on_reply(response_body);
|
||||
}
|
||||
|
||||
const inputs_for_reset = form.querySelectorAll("input[reset-on-submit]");
|
||||
for (const input of inputs_for_reset) {
|
||||
const reset_value = input.getAttribute("reset-on-submit");
|
||||
input.value = reset_value ?? "";
|
||||
}
|
||||
} catch (error) {
|
||||
console.dir({
|
||||
|
|
@ -97,6 +174,19 @@ document.addEventListener("DOMContentLoaded", () => {
|
|||
}
|
||||
|
||||
form.addEventListener("submit", on_submit);
|
||||
//form.onsubmit = on_submit;
|
||||
form.dataset.smartened = true;
|
||||
}
|
||||
}
|
||||
|
||||
const smarten_observer = new MutationObserver(smarten_forms);
|
||||
smarten_observer.observe(document, {
|
||||
childList: true,
|
||||
subtree: true,
|
||||
});
|
||||
|
||||
/*
|
||||
|
||||
document.addEventListener("DOMContentLoaded", smarten_forms);
|
||||
document.addEventListener("DOMSubtreeModified", smarten_forms);
|
||||
|
||||
*/
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue