From 058c476bc7e0a02fe27555ff30b9f98176b7fea4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Cohen=20Arazi?= Date: Mon, 1 Jun 2026 13:58:51 -0300 Subject: [PATCH] Bug 42732: Migrate catalogue/detail.tt addToBundle $.post to api-client.js MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replace direct $.post() to /api/v1/items/{id}/bundled_items with ItemAPIClient.bundled_items.add(), which uses http-client.js and automatically includes the CSRF token header. The caller passes return_response: true because it needs access to the raw response status code and body for fine-grained error handling (different user-facing messages for 409/already_bundled, 409/checked_out, 409/reserved, 404/not_found, 400/failed_nesting). http-client.js default behavior throws a plain Error on non-2xx responses, losing the status code and error_code fields the UI needs. Signed-off-by: Tomás Cohen Arazi Signed-off-by: Jonathan Druart Signed-off-by: Lisette Scheer Signed-off-by: Pedro Amorim --- .../prog/en/modules/catalogue/detail.tt | 104 +++++++++--------- .../intranet-tmpl/prog/js/fetch/api-client.js | 1 + .../prog/js/fetch/item-api-client.js | 11 ++ 3 files changed, 63 insertions(+), 53 deletions(-) diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/detail.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/detail.tt index 45c2edc7aa..6746f8023d 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/detail.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/detail.tt @@ -2018,65 +2018,64 @@ bundle_form_active = item_id; }); - function addToBundle(url, data) { + function addToBundle(item_id, data) { /* Send the data using post with external_id */ - var posting = $.post({ - url: url, - data: JSON.stringify(data), - contentType: "application/json; charset=utf-8", - dataType: "json", - }); + const client = APIClient.item; + const posting = client.bundled_items.add(item_id, data, { return_response: true }); const barcode = data.external_id; const marc_link = data.marc_link; /* Report the results */ - posting.done(function (data) { - $("#addResult").replaceWith('
' + _("Success: Added '%s'").format(barcode) + "
"); - $("#external_id").val("").focus(); - bundle_changed = 1; - }); - posting.fail(function (data) { - if (data.status === 409) { - var response = data.responseJSON; - if (response.error_code === "already_bundled") { - $("#addResult").replaceWith('
' + _("Warning: Item '%s' already attached").format(barcode) + "
"); - } else if (response.error_code === "bundle_checkout_out") { - $("#addResult").replaceWith('
' + _("Failure: Bundle is currently checked out") + "
"); - } else if (response.error_code === "checked_out") { - const button = $('