Hooray! Updates to updates!
This commit is contained in:
parent
85b0e576c7
commit
bcf14cf251
3 changed files with 106 additions and 33 deletions
|
|
@ -7,6 +7,7 @@
|
|||
const fd = new FormData(form);
|
||||
const out = {};
|
||||
|
||||
// base values
|
||||
fd.forEach((value, key) => {
|
||||
if (key in out) {
|
||||
if (!Array.isArray(out[key])) out[key] = [out[key]];
|
||||
|
|
@ -16,21 +17,21 @@
|
|||
}
|
||||
});
|
||||
|
||||
// normalize radios and checkboxes
|
||||
form.querySelectorAll('input[type="checkbox"], input[type="radio"]').forEach(el => {
|
||||
if (!el.name) return;
|
||||
|
||||
if (el.type === 'radio') {
|
||||
if (out[el.name] !== undefined) return;
|
||||
if (out[el.name] !== undefined) return; // already set for this group
|
||||
const checked = form.querySelector(`input[type="radio"][name="${CSS.escape(el.name)}"]:checked`);
|
||||
if (checked) out[el.name] = checked.value ?? true;
|
||||
return;
|
||||
}
|
||||
|
||||
if (el.type === 'checkbox') {
|
||||
const group = form.querySelectorAll(`input[type="checkbox"][name="${CSS.escape(el.name)}"]`);
|
||||
if (group.length > 1) {
|
||||
const checkedVals = Array.from(group)
|
||||
.filter(i => i.checked)
|
||||
.map(i => i.value ?? true);
|
||||
const checkedVals = Array.from(group).filter(i => i.checked).map(i => i.value ?? true);
|
||||
out[el.name] = checkedVals;
|
||||
} else {
|
||||
out[el.name] = el.checked;
|
||||
|
|
@ -41,24 +42,61 @@
|
|||
return out;
|
||||
}
|
||||
|
||||
function collectExistingUpdateIds() {
|
||||
return Array.from(document.querySelectorAll('script[type="application/json"][id^="md-"]'))
|
||||
.map(el => Number(el.id.slice(3)))
|
||||
.filter(Number.isFinite);
|
||||
}
|
||||
|
||||
function collectEditedUpdates() {
|
||||
const updates = [];
|
||||
for (const id of collectExistingUpdateIds()) {
|
||||
updates.push({ id, content: getMarkdown(id) }); // ensure getMarkdown exists
|
||||
}
|
||||
for (const md of (window.newDrafts || [])) {
|
||||
if ((md ?? '').trim()) updates.push({ content: md });
|
||||
}
|
||||
return updates;
|
||||
}
|
||||
|
||||
function collectDeletedIds() {
|
||||
return (window.deletedIds || []).filter(Number.isFinite);
|
||||
}
|
||||
|
||||
document.getElementById("{{ field['attrs']['data-model'] }}_form").addEventListener("submit", async e => {
|
||||
e.preventDefault();
|
||||
|
||||
const json = formToJson(e.target);
|
||||
json['id'] = {{ field['template_ctx']['values']['id'] }};
|
||||
json.id = {{ field['template_ctx']['values']['id'] }};
|
||||
|
||||
response = await fetch("{{ url_for('entry.update_entry', id=field['template_ctx']['values']['id'], model=field['attrs']['data-model']) }}", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify(json)
|
||||
});
|
||||
// map friendly names to real FK columns if needed
|
||||
if (json.contact && !json.contact_id) json.contact_id = Number(json.contact) || null;
|
||||
if (json.work_item && !json.work_item_id) json.work_item_id = Number(json.work_item) || null;
|
||||
|
||||
reply = await response.json();
|
||||
if (reply['status'] === 'success') {
|
||||
toastMessage("This entry has been successfully saved!", "success");
|
||||
} else {
|
||||
toastMessage(`Unable to save entry: ${reply['error']}`, "danger");
|
||||
// child mutations
|
||||
json.updates = collectEditedUpdates();
|
||||
json.delete_update_ids = collectDeletedIds();
|
||||
|
||||
try {
|
||||
const response = await fetch(
|
||||
"{{ url_for('entry.update_entry', id=field['template_ctx']['values']['id'], model=field['attrs']['data-model']) }}",
|
||||
{
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(json),
|
||||
}
|
||||
);
|
||||
|
||||
const reply = await response.json();
|
||||
if (reply.status === 'success') {
|
||||
toastMessage('This entry has been successfully saved!', 'success');
|
||||
window.newDrafts = [];
|
||||
window.deletedIds = [];
|
||||
} else {
|
||||
toastMessage(`Unable to save entry: ${reply.error}`, 'danger');
|
||||
}
|
||||
} catch (err) {
|
||||
toastMessage(`Network error: ${String(err)}`, 'danger');
|
||||
}
|
||||
});
|
||||
});
|
||||
</script>
|
||||
Loading…
Add table
Add a link
Reference in a new issue