From dfe3fcde904c2e60001456c099223d5d4e6b0783 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Wed, 21 Feb 2024 16:56:54 +0100 Subject: [PATCH] Bug 36084: svc - localization Signed-off-by: Jonathan Druart --- .../prog/en/modules/admin/localization.tt | 97 +++++++++---------- .../intranet-tmpl/prog/js/fetch/api-client.js | 2 + .../prog/js/fetch/http-client.js | 40 +++++++- .../prog/js/fetch/localization-api-client.js | 51 ++++++++++ 4 files changed, 140 insertions(+), 50 deletions(-) create mode 100644 koha-tmpl/intranet-tmpl/prog/js/fetch/localization-api-client.js diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/localization.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/localization.tt index 95ef15c133..56990a7f6e 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/localization.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/localization.tt @@ -136,50 +136,49 @@ } function send_update_request( data, cell ) { - $.ajax({ - data: data, - type: 'PUT', - url: '/cgi-bin/koha/svc/localization', - success: function (data) { - if ( data.error ) { + const client = APIClient.localization; + client.localizations.update(data).then( + success => { + if ( success.error ) { $(cell).css('background-color', '#FF0000'); - show_message({ type: 'error_on_update', data: data }); - } else if ( data.is_changed == 1 ) { + show_message({ type: 'error_on_update', data: success }); + } else if ( success.is_changed == 1 ) { $(cell).css('background-color', '#00FF00'); - show_message({ type: 'success_on_update', data: data }); + show_message({ type: 'success_on_update', data: success }); } if ( $(cell).hasClass('lang') ) { - $(cell).text(data.lang) + $(cell).text(success.lang) } else if ( $(cell).hasClass('translation') ) { - $(cell).text(data.translation) + $(cell).text(success.translation) } }, - error: function (data) { + error => { $(cell).css('background-color', '#FF9090'); if ( $(cell).hasClass('lang') ) { - $(cell).text(data.lang) + $(cell).text(error.lang) } else if ( $(cell).hasClass('translation') ) { - $(cell).text(data.translation) + $(cell).text(error.translation) } - show_message({ type: 'error_on_update', data: data }); - }, - }); + show_message({ type: 'error_on_update', data: error }); + console.warn("Something wrong happened: %s".format(error)); + } + ); } function send_delete_request( id, cell ) { - $.ajax({ - type: 'DELETE', - url: '/cgi-bin/koha/svc/localization/?id='+id, - success: function (data) { + const client = APIClient.localization; + client.localizations.delete(id).then( + success => { $("#localization").DataTable().row( '#row_id_' + id ).remove().draw(); - show_message({ type: 'success_on_delete', data: data }); + show_message({ type: 'success_on_delete', data: {id} }); }, - error: function (data) { + error => { $(cell).css('background-color', '#FF9090'); - show_message({ type: 'error_on_delete', data: data }); - }, - }); + show_message({ type: 'error_on_delete', data: error }); + console.warn("Something wrong happened: %s".format(error)); + } + ); } $(document).ready(function() { @@ -224,8 +223,8 @@ var tr = $(this).parent().parent(); var id = $(tr).data('id'); var lang = $(this).find('option:selected').val(); - var data = "id=" + encodeURIComponent(id) + "&lang=" + encodeURIComponent(lang); - send_update_request( data, td ); + var translation = $(this).text(); + send_update_request( {id, lang, translation}, td ); }); $(my_select).on('blur', function(){ $(td).html(lang); @@ -236,9 +235,9 @@ $("td.translation").on('blur', function(){ var tr = $(this).parent(); var id = $(tr).data('id'); + var lang = $(tr).find('td.lang').text(); var translation = $(this).text(); - var data = "id=" + encodeURIComponent(id) + "&translation=" + encodeURIComponent(translation); - send_update_request( data, this ); + send_update_request( {id, lang, translation}, this ); }); $("body").on("click", "a.delete", function(e){ @@ -253,28 +252,28 @@ $("#add_translation").on('submit', function(e){ e.preventDefault(); - var entity = $(this).find('input[name="entity"]').val(); - var code = $(this).find('input[name="code"]').val(); - var lang = $(this).find('select[name="lang"] option:selected').val(); - var translation = $(this).find('input[name="translation"]').val(); - var data = "entity=" + encodeURIComponent(entity) + "&code=" + encodeURIComponent(code) + "&lang=" + encodeURIComponent(lang) + "&translation=" + encodeURIComponent(translation); - $.ajax({ - data: data, - type: 'POST', - url: '/cgi-bin/koha/svc/localization', - success: function (data) { - if ( data.error ) { - show_message({ type: 'error_on_insert', data: data }); + let localization = { + entity: $(this).find('input[name="entity"]').val(), + code: $(this).find('input[name="code"]').val(), + lang: $(this).find('select[name="lang"] option:selected').val(), + translation: $(this).find('input[name="translation"]').val(), + }; + const client = APIClient.localization; + client.localizations.create(localization).then( + success => { + if ( success.error ) { + show_message({ type: 'error_on_insert', data: success }); } else { - var new_row = table.row.add( [ data.id, data.entity, data.code, data.lang, data.translation, " Delete" ] ).draw().node(); - $( new_row ).attr("id", "row_id_" + data.id ).data("id", data.id ); - show_message({ type: 'success_on_insert', data: data }); + var new_row = table.row.add( [ success.id, success.entity, success.code, success.lang, success.translation, " Delete" ] ).draw().node(); + $( new_row ).attr("id", "row_id_" + success.id ).data("id", success.id ); + show_message({ type: 'success_on_insert', data: success }); } }, - error: function (data) { - show_message({ type: 'error_on_insert', data: data }); - }, - }); + error => { + show_message({ type: 'error_on_insert', data: error }); + console.warn("Something wrong happened: %s".format(error)); + } + ); }); }); 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 03be692d8f..223da55aa8 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/fetch/api-client.js +++ b/koha-tmpl/intranet-tmpl/prog/js/fetch/api-client.js @@ -3,6 +3,7 @@ import AVAPIClient from "./authorised-value-api-client.js"; import CirculationAPIClient from "./circulation-api-client.js"; import ClubAPIClient from "./club-api-client.js"; import CoverImageAPIClient from "./cover-image-api-client.js"; +import LocalizationAPIClient from "./localization-api-client.js"; import SysprefAPIClient from "./system-preferences-api-client.js"; export const APIClient = { @@ -11,5 +12,6 @@ export const APIClient = { circulation: new CirculationAPIClient(), club: new ClubAPIClient(), cover_image: new CoverImageAPIClient(), + localization: new LocalizationAPIClient(), syspref: new SysprefAPIClient(), }; 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 c542858f11..f5340df946 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/fetch/http-client.js +++ b/koha-tmpl/intranet-tmpl/prog/js/fetch/http-client.js @@ -77,7 +77,7 @@ class HttpClient { ? typeof params.body === "string" ? params.body : JSON.stringify(params.body) - : params.data || undefined; + : undefined; let csrf_token = { csrf_token: this.csrf_token }; let headers = { ...csrf_token, ...params.headers }; return this._fetchJSON( @@ -92,6 +92,44 @@ class HttpClient { true ); } + + put(params = {}) { + const body = params.body + ? typeof params.body === "string" + ? params.body + : JSON.stringify(params.body) + : undefined; + let csrf_token = { csrf_token: this.csrf_token }; + let headers = { ...csrf_token, ...params.headers }; + return this._fetchJSON( + params.endpoint, + headers, + { + ...params.options, + body, + method: "PUT", + }, + false, + true + ); + } + + delete(params = {}) { + let csrf_token = { csrf_token: this.csrf_token }; + let headers = { ...csrf_token, ...params.headers }; + return this._fetchJSON( + params.endpoint, + headers, + { + parseResponse: false, + ...params.options, + method: "DELETE", + }, + true, + true + ); + } + } export default HttpClient; diff --git a/koha-tmpl/intranet-tmpl/prog/js/fetch/localization-api-client.js b/koha-tmpl/intranet-tmpl/prog/js/fetch/localization-api-client.js new file mode 100644 index 0000000000..1a22dfbd3a --- /dev/null +++ b/koha-tmpl/intranet-tmpl/prog/js/fetch/localization-api-client.js @@ -0,0 +1,51 @@ +import HttpClient from "./http-client.js"; + +export class LocalizationAPIClient extends HttpClient { + constructor() { + super({ + baseURL: "/cgi-bin/koha/svc/localization", + }); + } + + get localizations() { + return { + create: localization => + this.post({ + endpoint: "", + body: "entity=%s&code=%s&lang=%s&translation=%s".format( + encodeURIComponent(localization.entity), + encodeURIComponent(localization.code), + encodeURIComponent(localization.lang), + encodeURIComponent(localization.translation) + ), + headers: { + "Content-Type": + "application/x-www-form-urlencoded;charset=utf-8", + }, + }), + update: localization => + this.put({ + endpoint: "", + body: "id=%s&lang=%s&translation=%s".format( + encodeURIComponent(localization.id), + encodeURIComponent(localization.lang), + encodeURIComponent(localization.translation) + ), + headers: { + "Content-Type": + "application/x-www-form-urlencoded;charset=utf-8", + }, + }), + delete: id => + this.delete({ + endpoint: "/?id=%s".format(id), + headers: { + "Content-Type": + "application/x-www-form-urlencoded;charset=utf-8", + }, + }), + }; + } +} + +export default LocalizationAPIClient; -- 2.39.5