Addint submit and toast behavior.
This commit is contained in:
parent
fc4d3ebfe6
commit
dbf0d6169a
6 changed files with 137 additions and 22 deletions
60
inventory/templates/submit_button.html
Normal file
60
inventory/templates/submit_button.html
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
<button type="submit" class="btn btn-primary" id="submit">Save</button>
|
||||
<script>
|
||||
function formToJson(form) {
|
||||
const fd = new FormData(form);
|
||||
const out = {};
|
||||
|
||||
fd.forEach((value, key) => {
|
||||
if (key in out) {
|
||||
if (!Array.isArray(out[key])) out[key] = [out[key]];
|
||||
out[key].push(value);
|
||||
} else {
|
||||
out[key] = value;
|
||||
}
|
||||
});
|
||||
|
||||
form.querySelectorAll('input[type="checkbox"], input[type="radio"]').forEach(el => {
|
||||
if (!el.name) return;
|
||||
|
||||
if (el.type === 'radio') {
|
||||
if (out[el.name] !== undefined) return;
|
||||
const checked = form.querySelector(`input[type="radio"][name="${CSS.escape(el.name)}"]:checked`);
|
||||
if (checked) out[el.name] = checked.value ?? true;
|
||||
}
|
||||
|
||||
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);
|
||||
out[el.name] = checkedVals;
|
||||
} else {
|
||||
out[el.name] = el.checked;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
document.getElementById("{{ field['attrs']['data-model'] }}_form").addEventListener("submit", async e => {
|
||||
e.preventDefault();
|
||||
const json = formToJson(e.target);
|
||||
|
||||
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)
|
||||
});
|
||||
|
||||
reply = await response.json();
|
||||
if (reply['status'] === 'success') {
|
||||
console.log("WELL DONE!")
|
||||
} else {
|
||||
console.log("YOU HAVE FAILED!")
|
||||
}
|
||||
});
|
||||
</script>
|
||||
Loading…
Add table
Add a link
Reference in a new issue