From f5a1f41d3500e2bcd425ee913b603195a6872ff8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Cohen=20Arazi?= Date: Mon, 1 Jun 2026 12:00:57 -0300 Subject: [PATCH] Bug 42730: Migrate place_booking.js $.post to api-client.js MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replace direct $.post() and $.ajax() calls to /api/v1/bookings with the new BookingAPIClient, which uses http-client.js and automatically includes the CSRF token header. Signed-off-by: Tomás Cohen Arazi Signed-off-by: Jonathan Druart Signed-off-by: Lisette Scheer Signed-off-by: Pedro Amorim --- .../intranet-tmpl/prog/js/fetch/api-client.js | 1 + .../prog/js/fetch/booking-api-client.js | 24 +++++++++++++++++++ .../prog/js/modals/place_booking.js | 22 ++++++++--------- .../prog/js/vue/fetch/api-client.js | 2 ++ 4 files changed, 37 insertions(+), 12 deletions(-) create mode 100644 koha-tmpl/intranet-tmpl/prog/js/fetch/booking-api-client.js diff --git a/koha-tmpl/intranet-tmpl/prog/js/fetch/api-client.js b/koha-tmpl/intranet-tmpl/prog/js/fetch/api-client.js index 8bf1e6946d..02d43c3728 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/fetch/api-client.js +++ b/koha-tmpl/intranet-tmpl/prog/js/fetch/api-client.js @@ -187,6 +187,7 @@ export const APIClient = { () => import("./authorised-values-api-client.js") ), acquisition: createClientProxy(() => import("./acquisition-api-client.js")), + booking: createClientProxy(() => import("./booking-api-client.js")), cataloguing: createClientProxy(() => import("./cataloguing-api-client.js")), circulation: createClientProxy(() => import("./circulation-api-client.js")), club: createClientProxy(() => import("./club-api-client.js")), diff --git a/koha-tmpl/intranet-tmpl/prog/js/fetch/booking-api-client.js b/koha-tmpl/intranet-tmpl/prog/js/fetch/booking-api-client.js new file mode 100644 index 0000000000..35b5fc1ef0 --- /dev/null +++ b/koha-tmpl/intranet-tmpl/prog/js/fetch/booking-api-client.js @@ -0,0 +1,24 @@ +export class BookingAPIClient { + constructor(HttpClient) { + this.httpClient = new HttpClient({ + baseURL: "/api/v1", + }); + } + + get bookings() { + return { + create: booking => + this.httpClient.post({ + endpoint: "/bookings", + body: booking, + }), + update: (booking, id) => + this.httpClient.put({ + endpoint: "/bookings/" + id, + body: booking, + }), + }; + } +} + +export default BookingAPIClient; diff --git a/koha-tmpl/intranet-tmpl/prog/js/modals/place_booking.js b/koha-tmpl/intranet-tmpl/prog/js/modals/place_booking.js index cd8bde765c..76ed602d0b 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/modals/place_booking.js +++ b/koha-tmpl/intranet-tmpl/prog/js/modals/place_booking.js @@ -2328,7 +2328,6 @@ function refreshBookingsTable() { $("#placeBookingForm").on("submit", function (e) { e.preventDefault(); - const url = "/api/v1/bookings"; const start_date = $("#booking_start_date").val(); const end_date = $("#booking_end_date").val(); const item_id = $("#booking_item_id").val(); @@ -2354,8 +2353,10 @@ $("#placeBookingForm").on("submit", function (e) { if (!booking_id) { // Create new booking - $.post(url, JSON.stringify(payload)) - .done(function (data) { + const client = APIClient.booking; + client.bookings + .create(payload) + .then(function (data) { bookings.push(data); refreshBookingsTable(); @@ -2369,20 +2370,17 @@ $("#placeBookingForm").on("submit", function (e) { ); showBookingSuccess(__("Booking successfully placed")); }) - .fail(function () { + .catch(function () { showBookingError(__("Failure")); }); } else { // Update existing booking payload.booking_id = booking_id; - $.ajax({ - method: "PUT", - url: url + "/" + booking_id, - contentType: "application/json", - data: JSON.stringify(payload), - }) - .done(function (data) { + const client = APIClient.booking; + client.bookings + .update(payload, booking_id) + .then(function (data) { const target = bookings.find( obj => obj.booking_id === data.booking_id ); @@ -2398,7 +2396,7 @@ $("#placeBookingForm").on("submit", function (e) { showBookingSuccess(__("Booking successfully updated")); }) - .fail(function () { + .catch(function () { showBookingError(__("Failure")); }); } diff --git a/koha-tmpl/intranet-tmpl/prog/js/vue/fetch/api-client.js b/koha-tmpl/intranet-tmpl/prog/js/vue/fetch/api-client.js index bfddd83b1d..e0fa6b260e 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/vue/fetch/api-client.js +++ b/koha-tmpl/intranet-tmpl/prog/js/vue/fetch/api-client.js @@ -1,5 +1,6 @@ import HttpClient from "./http-client"; +import BookingAPIClient from "@fetch/booking-api-client"; import ERMAPIClient from "@fetch/erm-api-client"; import ILLAPIClient from "@fetch/ill-api-client"; import BiblioAPIClient from "@fetch/biblio-api-client"; @@ -16,6 +17,7 @@ import SIP2APIClient from "@fetch/sip2-api-client"; import PreservationAPIClient from "@fetch/preservation-api-client"; export const APIClient = { + booking: new BookingAPIClient(HttpClient), erm: new ERMAPIClient(HttpClient), ill: new ILLAPIClient(HttpClient), biblio: new BiblioAPIClient(HttpClient),