Bug 42730: Migrate place_booking.js $.post to api-client.js
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 <tomascohen@theke.io> Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org> Signed-off-by: Lisette Scheer <lisette@bywatersolutions.com> Signed-off-by: Pedro Amorim <pedro.amorim@openfifth.co.uk>
This commit is contained in:
parent
f5e8cdb399
commit
f5a1f41d35
|
|
@ -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")),
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
@ -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"));
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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),
|
||||
|
|
|
|||
Loading…
Reference in New Issue