Bug 36084: svc - localization

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
This commit is contained in:
Jonathan Druart 2024-02-21 16:56:54 +01:00
parent 8ebb11b244
commit dfe3fcde90
No known key found for this signature in database
GPG Key ID: A085E712BEF0E0F0
4 changed files with 140 additions and 50 deletions

View File

@ -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, "<a href=\"#\" class=\"delete\"><i class=\"fa fa-trash-can\"></i> Delete</a>" ] ).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, "<a href=\"#\" class=\"delete\"><i class=\"fa fa-trash-can\"></i> Delete</a>" ] ).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));
}
);
});
});

View File

@ -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(),
};

View File

@ -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;

View File

@ -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;