From 526ceeb4fa2203236fd7d01021f21c3bd8e5ab42 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Tue, 20 Feb 2024 15:39:21 +0100 Subject: [PATCH] Bug 36084: svc - checkout_notes Signed-off-by: Jonathan Druart --- .../prog/en/modules/circ/checkout-notes.tt | 78 ++++++++++--------- .../prog/js/fetch/circulation-api-client.js | 23 ++++++ svc/checkout_notes | 16 +--- 3 files changed, 70 insertions(+), 47 deletions(-) diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/circ/checkout-notes.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/circ/checkout-notes.tt index 86f486f2ae..e34b339a51 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/circ/checkout-notes.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/circ/checkout-notes.tt @@ -179,44 +179,52 @@ } }); + function show_error(){ + // FIXME Can be improved by displaying meaningful error. + $("#error").text(_("Unable to change status of note.")).show(); + } + $("#notestable").on("click", "button.seen, button.notseen", function(event){ event.preventDefault(); // prevent form submission - var $op = $(this).attr("name"); - var $issue_id = $(this).data('issue_id'); - var ajaxData = { - 'op': $op, - 'issue_id': $issue_id, - }; - - $.ajax({ - url: '/cgi-bin/koha/svc/checkout_notes/', - type: 'POST', - dataType: 'json', - data: ajaxData, - }) - - .done(function(data){ - if (data.status == 'success'){ - if ( $op == 'notseen' ){ - $("#status_" + $issue_id).text(_("Not seen")); - $(event.target).parent().siblings(".seen1").removeClass("seen1").addClass("seen0"); - $(event.target).siblings(".seen").prop("disabled", false); - $(event.target).prop("disabled", true); - } else { - $("#status_" + $issue_id).text(_("Seen")); - $(event.target).parent().siblings(".seen0").removeClass("seen0").addClass("seen1"); - $(event.target).siblings(".notseen").prop("disabled", false); - $(event.target).prop("disabled", true); + var op = $(this).attr("name"); + var issue_id = $(this).data('issue_id'); + + const client = APIClient.circulation; + if ( op == 'seen' ) { + client.checkouts.mark_as_seen(issue_id).then( + success => { + if (success.seen){ + $("#status_" + issue_id).text(_("Seen")); + $(event.target).parent().siblings(".seen0").removeClass("seen0").addClass("seen1"); + $(event.target).siblings(".notseen").prop("disabled", false); + $(event.target).prop("disabled", true); + } else { + show_error(); + } + }, + error => { + console.warn("Something wrong happened: %s".format(error)); + show_error(); + } + ); + } else { + client.checkouts.mark_as_not_seen(issue_id).then( + success => { + if (!success.seen){ + $("#status_" + issue_id).text(_("Not seen")); + $(event.target).parent().siblings(".seen1").removeClass("seen1").addClass("seen0"); + $(event.target).siblings(".seen").prop("disabled", false); + $(event.target).prop("disabled", true); + } else { + show_error(); + } + }, + error => { + console.warn("Something wrong happened: %s".format(error)); + show_error(); } - } else { - $("#error").text(_("Unable to change status of note.")); - $("#error").show(); - } - }) - .error(function(data){ - $("#error").text(_("Unable to change status of note.")); - $("#error").show(); - }); + ); + } }); }); diff --git a/koha-tmpl/intranet-tmpl/prog/js/fetch/circulation-api-client.js b/koha-tmpl/intranet-tmpl/prog/js/fetch/circulation-api-client.js index 4d1a6dbcd5..dd3247b209 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/fetch/circulation-api-client.js +++ b/koha-tmpl/intranet-tmpl/prog/js/fetch/circulation-api-client.js @@ -26,6 +26,29 @@ export class CirculationAPIClient extends HttpClient { }), }; } + + get checkouts() { + return { + mark_as_seen: checkout_id => + this.post({ + endpoint: "checkout_notes", + body: "issue_id=%s&op=%s".format(checkout_id, "cud-seen"), + headers: { + "Content-Type": + "application/x-www-form-urlencoded;charset=utf-8", + }, + }), + mark_as_not_seen: checkout_id => + this.post({ + endpoint: "checkout_notes", + body: "issue_id=%s&op=%s".format(checkout_id, "cud-notseen"), + headers: { + "Content-Type": + "application/x-www-form-urlencoded;charset=utf-8", + }, + }) + } + } } export default CirculationAPIClient; diff --git a/svc/checkout_notes b/svc/checkout_notes index 6b656d7db4..be0f80d2c8 100755 --- a/svc/checkout_notes +++ b/svc/checkout_notes @@ -21,7 +21,6 @@ use Modern::Perl; use JSON qw( to_json ); use CGI; -use C4::Service; use C4::Auth qw ( check_cookie_auth ); use C4::Output qw( is_ajax output_with_http_headers ); use Koha::Checkouts; @@ -41,23 +40,16 @@ my ( $auth_status ) = check_cookie_auth( $query->cookie('CGISESSID'), { circulat if ( $auth_status ne "ok" ) { exit 0; } +my $op = $query->param('op'); if ($is_ajax) { my $issue_id = $query->param('issue_id'); my $issue = Koha::Checkouts->find($issue_id); - my $op = $query->param('op'); - my $status = 'success'; - if ($op eq 'seen'){ + if ($op eq 'cud-seen'){ $issue->set({ noteseen => 1 })->store; - if ( $issue->noteseen != 1 ) { - $status = 'failure'; - } - } elsif ($op eq 'notseen'){ + } elsif ($op eq 'cud-notseen'){ $issue->set({ noteseen => 0 })->store; - if ( $issue->noteseen != 0 ) { - $status = 'failure'; - } } - my $json = to_json ( { status => $status } ); + my $json = to_json ( { seen => $issue->noteseen } ); output_with_http_headers $query, undef, $json, 'js'; exit; } -- 2.39.5