Add inventory item creation and update endpoints; enhance inventory template with form handling

This commit is contained in:
Yaro Kasear 2025-07-07 09:40:27 -05:00
parent a1d3f58081
commit dc394dd992
4 changed files with 159 additions and 2 deletions

View file

@ -27,7 +27,7 @@
</div>
{% if submit_button %}
<div class="col text-end">
<button type="submit" class="btn btn-primary">Save</button>
<button type="submit" class="btn btn-primary" id="saveButton">Save</button>
</div>
{% endif %}
</nav>

View file

@ -11,6 +11,7 @@ breadcrumbs=[
title=title,
submit_button=True) }}
<input type="hidden" id="inventoryId" value="{{ item.id }}">
<div class="container">
<div class="row">
<div class="col-6">
@ -52,7 +53,7 @@ submit_button=True) }}
</div>
<div class="col-4">
<label for="model" class="form-label">Model</label>
<input type="text" class="form-control" name="model" placeholder="-" value="{{ item.model }}">
<input type="text" class="form-control" name="model" placeholder="-" value="{{ item.model if item.model else '' }}">
</div>
<div class="col-4">
<label for="type" class="form-label">Category</label>
@ -124,4 +125,68 @@ submit_button=True) }}
{% endif %}
</div>
</div>
{% endblock %}
{% block script %}
document.addEventListener("DOMContentLoaded", () => {
const submitButton = document.getElementById("saveButton");
const toastData = localStorage.getItem("toastMessage");
if (toastData) {
const { message, type } = JSON.parse(toastData);
renderToast({ message, type });
localStorage.removeItem("toastMessage");
}
if (submitButton) {
submitButton.addEventListener("click", async (e) => {
e.preventDefault();
const payload = {
timestamp: document.querySelector("input[name='timestamp']").value,
condition: document.querySelector("select[name='condition']").value,
needed: "", // ← either add a field for this or drop it if obsolete
type_id: parseInt(document.querySelector("select[name='type']").value),
inventory_name: document.querySelector("input[name='inventory_name']").value || null,
serial: document.querySelector("input[name='serial']").value || null,
model: document.querySelector("input[name='model']").value || null,
notes: document.querySelector("textarea[name='notes']").value || null,
owner_id: parseInt(document.querySelector("select#userList").value) || null,
brand_id: parseInt(document.querySelector("select[name='brand']").value) || null,
location_id: parseInt(document.querySelector("select#room").value) || null,
barcode: document.querySelector("input[name='barcode']").value || null,
shared: document.querySelector("input[name='shared']").checked
};
try {
const id = document.querySelector("#inventoryId").value;
const isEdit = id && id !== "None";
const endpoint = isEdit ? `/api/inventory/${id}` : "/api/inventory"
const method = isEdit ? "PUT" : "POST";
const response = await fetch(endpoint, {
method,
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(payload)
});
const result = await response.json();
if(result.success) {
localStorage.setItem("toastMessage", JSON.stringify({
message: isEdit ? "Inventory item updated!" : "Inventory item created!",
type: "success"
}));
window.location.href = `/inventory_item/${result.id}`;
} else {
renderToast({message: `Error: ${result.error}`, type: "danger"});
}
} catch (err) {
console.error(err);
}
});
}
});
{% endblock %}