Koha/koha-tmpl/intranet-tmpl/prog/js/cancel_booking_modal.js
Paul Derscheid 36c5a1c011
Bug 38175: (follow-up) Refactor staff interface additions to vanilla JS; adjust modules, controllers for coding guidelines and consistency
In the spirit of the current movement for more modern JS, the refactor tries to limit jQuery usage.

Test plan:
    1) Create a booking on a bookable item
    2) Cancel it and see that it's simply deleted
    3) Apply the patch and run “restart_all”.
    4) Repeat the same cancel operation and see that it's still there, albeit with a different appearance.
    5) Try the filters in the table

Signed-off-by: Paul Derscheid <paul.derscheid@lmscloud.de>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Signed-off-by: Katrin Fischer <katrin.fischer@bsz-bw.de>
2024-10-24 18:27:23 +02:00

80 lines
2.1 KiB
JavaScript

(() => {
document
.getElementById("cancelBookingModal")
?.addEventListener("show.bs.modal", handleShowBsModal);
document
.getElementById("cancelBookingForm")
?.addEventListener("submit", handleSubmit);
async function handleSubmit(e) {
e.preventDefault();
const bookingIdInput = document.getElementById("cancel_booking_id");
if (!bookingIdInput) {
return;
}
const bookingId = bookingIdInput.value;
if (!bookingId) {
return;
}
let [error, response] = await catchError(
fetch(`/api/v1/bookings/${bookingId}`, {
method: "PATCH",
body: JSON.stringify({ status: "cancelled" }),
headers: {
"Content-Type": "application/json",
},
})
);
if (error || !response.ok) {
const alertContainer = document.getElementById(
"cancel_booking_result"
);
alertContainer.outerHTML = `
<div id="booking_result" class="alert alert-danger">
${__("Failure")}
</div>
`;
return;
}
cancel_success = true;
bookings_table?.api().ajax.reload();
timeline?.itemsData.remove(Number(booking_id));
$("#cancelBookingModal").modal("hide");
const bookingsCount = document.querySelector(".bookings_count");
if (!bookingsCount) {
return;
}
bookingsCount.innerHTML = parseInt(bookingsCount.innerHTML, 10) - 1;
}
function handleShowBsModal(e) {
const button = e.relatedTarget;
if (!button) {
return;
}
const booking = button.dataset.booking;
if (!booking) {
return;
}
const bookingIdInput = document.getElementById("cancel_booking_id");
if (!bookingIdInput) {
return;
}
bookingIdInput.value = booking;
}
function catchError(promise) {
return promise.then(data => [undefined, data]).catch(error => [error]);
}
})();