Added button fragment and did the do.
This commit is contained in:
parent
34f1c5a824
commit
9ca9aa86a1
4 changed files with 301 additions and 263 deletions
|
@ -3,20 +3,69 @@
|
|||
|
||||
{% block title %}{{ title }}{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
{% block precontent %}
|
||||
{% set saveLogic %}
|
||||
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
|
||||
};
|
||||
|
||||
{{ breadcrumbs.breadcrumb_header(
|
||||
title=title,
|
||||
breadcrumbs=[
|
||||
{'label': 'Users', 'url': url_for('main.list_users')}
|
||||
],
|
||||
save_button = True
|
||||
) }}
|
||||
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);
|
||||
}
|
||||
{% endset %}
|
||||
{{ toolbars.render_toolbar(
|
||||
id = 'newUser',
|
||||
left = breadcrumbs.breadcrumb_header(
|
||||
title=title,
|
||||
breadcrumbs=[
|
||||
{'label': 'Users', 'url': url_for('main.list_users')}
|
||||
]
|
||||
),
|
||||
right = buttons.render_button(
|
||||
id = 'save',
|
||||
icon = 'floppy',
|
||||
logic = saveLogic
|
||||
)
|
||||
) }}
|
||||
{% if not user.active %}
|
||||
<div class="alert alert-danger">This user is inactive. You will not be able to make any changes to this record.</div>
|
||||
{% endif %}
|
||||
{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<input type="hidden" id="userId" value="{{ user.id }}">
|
||||
<div class="container">
|
||||
<form action="POST">
|
||||
|
@ -90,53 +139,3 @@
|
|||
</div>
|
||||
</div>
|
||||
{% 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 %}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue