Refactor inventory and layout templates: streamline script handling and improve toast message display

This commit is contained in:
Yaro Kasear 2025-07-07 10:23:07 -05:00
parent ebd2060fd8
commit 3492358645
3 changed files with 94 additions and 91 deletions

View file

@ -130,17 +130,9 @@
{% endblock %}
{% block script %}
document.addEventListener("DOMContentLoaded", () => {
const saveButton = document.getElementById("saveButton");
const deleteButton = document.getElementById("deleteButton");
const toastData = localStorage.getItem("toastMessage");
if (toastData) {
const { message, type } = JSON.parse(toastData);
renderToast({ message, type });
localStorage.removeItem("toastMessage");
}
if (saveButton) {
saveButton.addEventListener("click", async (e) => {
e.preventDefault();
@ -177,7 +169,7 @@
});
const result = await response.json();
if(result.success) {
if (result.success) {
localStorage.setItem("toastMessage", JSON.stringify({
message: isEdit ? "Inventory item updated!" : "Inventory item created!",
type: "success"
@ -185,7 +177,7 @@
window.location.href = `/inventory_item/${result.id}`;
} else {
renderToast({message: `Error: ${result.error}`, type: "danger"});
renderToast({ message: `Error: ${result.error}`, type: "danger" });
}
} catch (err) {
console.error(err);
@ -202,7 +194,7 @@
return;
}
if(!confirm("Are you sure you want to delete this inventory item? This action cannot be undone.")) {
if (!confirm("Are you sure you want to delete this inventory item? This action cannot be undone.")) {
return;
}
@ -228,5 +220,4 @@
}
});
}
});
{% endblock %}

View file

@ -78,7 +78,16 @@
searchInput.addEventListener('input', () => {
searchButton.disabled = searchInput.value.trim() === '';
});
document.addEventListener("DOMContentLoaded", () => {
const toastData = localStorage.getItem("toastMessage");
if (toastData) {
const { message, type } = JSON.parse(toastData);
renderToast({ message, type });
localStorage.removeItem("toastMessage");
}
{% block script %} {% endblock %}
});
</script>
</body>

View file

@ -12,3 +12,6 @@
{{ tables.render_table(headers=header, rows=rows, id='table', entry_route=entry_route) }}
{% endblock %}
{% block script %}
{% endblock %}