From 2b646fd21e06abad038909d12e65179c8071050a Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Mon, 16 Jun 2025 09:39:38 +0200 Subject: [PATCH] Bug 40172: Replace jQuery with vanilla JS in js/fetch/http-client.js So that we can reuse it from somewhere else. The current need is to be able to reuse it from Cypress tests. Test plan: Try to hit several places where APIClient is used: * Syspref edition * Translation of item type description (be aware of bug 40161) * Delete of cover images * checkin, renew from the checkout list * etc. Note that this does not affect Vue components. --- .../prog/js/fetch/http-client.js | 40 +++++++++++++++---- 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/koha-tmpl/intranet-tmpl/prog/js/fetch/http-client.js b/koha-tmpl/intranet-tmpl/prog/js/fetch/http-client.js index 50dc09f6ca7..93194a164b5 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/fetch/http-client.js +++ b/koha-tmpl/intranet-tmpl/prog/js/fetch/http-client.js @@ -1,16 +1,31 @@ +function _ifDocumentAvailable(callback) { + if (typeof document !== "undefined" && document.getElementById) { + callback(); + } +} + class Dialog { constructor(options = {}) {} + _appendMessage(type, message) { + _ifDocumentAvailable(() => { + const messagesContainer = document.getElementById("messages"); + if (!messagesContainer) { + return; + } + + const htmlString = + `
%s
`.format(message); + messagesContainer.insertAdjacentHTML("beforeend", htmlString); + }); + } + setMessage(message) { - $("#messages").append( - '
%s
'.format(message) - ); + this._appendMessage("info", message); } setError(error) { - $("#messages").append( - '
%s
'.format(error) - ); + this._appendMessage("warning", error); } } @@ -22,7 +37,18 @@ class HttpClient { "Content-Type": "application/json;charset=utf-8", "X-Requested-With": "XMLHttpRequest", }; - this.csrf_token = $('meta[name="csrf-token"]').attr("content"); + this.csrf_token = this._getCsrfToken(options); + } + + _getCsrfToken(options) { + let token = null; + _ifDocumentAvailable(() => { + const metaTag = document.querySelector('meta[name="csrf-token"]'); + if (metaTag) { + token = metaTag.getAttribute("content"); + } + }); + return token !== null ? token : options.csrfToken || null; } async _fetchJSON( -- 2.34.1