", methods=["PUT"])
+def update_user(id):
+ try:
+ data = request.get_json(force=True)
+ user = db.session.query(User).get(id)
+
+ if not user:
+ return jsonify({"success": False, "error": f"User with ID {id} not found."}), 404
+
+ user.staff = bool(data.get("staff", user.staff))
+ user.active = bool(data.get("active", user.active))
+ user.last_name = data.get("last_name", user.last_name)
+ user.first_name = data.get("first_name", user.first_name)
+ user.location_id = data.get("location_id", user.location_id)
+ user.supervisor_id = data.get("supervisor_id", user.supervisor_id)
+
+ db.session.commit()
+
+ return jsonify({"success": True, "id": user.id}), 200
+
+ except Exception as e:
+ db.session.rollback()
+ return jsonify({"success": False, "error": str(e)}), 400
\ No newline at end of file
diff --git a/templates/inventory.html b/templates/inventory.html
index fbcc601..e09b90e 100644
--- a/templates/inventory.html
+++ b/templates/inventory.html
@@ -157,7 +157,7 @@
const id = document.querySelector("#inventoryId").value;
const isEdit = id && id !== "None";
- const endpoint = isEdit ? `/api/inventory/${id}` : "/api/inventory"
+ const endpoint = isEdit ? `/api/inventory/${id}` : "/api/inventory";
const method = isEdit ? "PUT" : "POST";
const response = await fetch(endpoint, {
diff --git a/templates/user.html b/templates/user.html
index cbdba37..5c87e25 100644
--- a/templates/user.html
+++ b/templates/user.html
@@ -7,26 +7,28 @@
{{ breadcrumbs.breadcrumb_header(
-title=title,
-breadcrumbs=[
-{'label': 'Users', 'url': url_for('main.list_users')}
-]
+ title=title,
+ breadcrumbs=[
+ {'label': 'Users', 'url': url_for('main.list_users')}
+ ],
+ save_button = True
) }}
{% if not user.active %}
This user is inactive. You will not be able to make any changes to this record.
{% endif %}
+
@@ -87,4 +89,54 @@ breadcrumbs=[
{% endif %}
-{% endblock %}
\ No newline at end of file
+{% endblock %}
+
+{% block script %}
+ const saveButton = document.getElementById("saveButton");
+ const deleteButton = document.getElementById("deleteButton");
+
+ if (saveButton) {
+ saveButton.addEventListener("click", async (e) => {
+ e.preventDefault();
+
+ const payload = {
+ staff: document.querySelector("input[name='staffCheck']").checked,
+ active: document.querySelector("input[name='activeCheck']").checked,
+ last_name: document.querySelector("input[name='lastName']").value,
+ first_name: document.querySelector("input[name='firstName']").value,
+ supervisor_id: parseInt(document.querySelector("select[name='supervisor']").value) || null,
+ location_id: parseInt(document.querySelector("select[name='location']").value) || null
+ };
+
+ try {
+ const id = document.querySelector("#userId").value;
+ const isEdit = id && id !== "None";
+
+ const endpoint = isEdit ? `/api/user/${id}` : "/api/user";
+ 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 ? "User updated!" : "User created!",
+ type: "success"
+ }));
+
+ window.location.href = `/user/${result.id}`;
+ } else {
+ renderToast({ message: `Error: ${result.error}`, type: "danger" });
+ }
+ } catch (err) {
+ console.error(err);
+ }
+ });
+ }
+{% endblock %}