From 25ea67c2ca94b43a6d38c734961606f5d377cd9f Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Thu, 22 Feb 2024 16:50:15 +0100 Subject: [PATCH] Bug 36084: svc - problem_reports Signed-off-by: Jonathan Druart --- .../prog/en/modules/tools/problem-reports.tt | 77 +++++++++++-------- .../intranet-tmpl/prog/js/fetch/api-client.js | 2 + .../prog/js/fetch/ticket-api-client.js | 43 +++++++++++ svc/problem_reports | 21 ++--- 4 files changed, 93 insertions(+), 50 deletions(-) create mode 100644 koha-tmpl/intranet-tmpl/prog/js/fetch/ticket-api-client.js diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/problem-reports.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/problem-reports.tt index 3604d39d1f..e79ef06d51 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/problem-reports.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/problem-reports.tt @@ -203,52 +203,61 @@ } }); + + function show_error(){ + // FIXME Can be improved by displaying meaningful error. + $("#error").text(_("Unable to change status of problem report.")).show(); + } + $("#problemreportstable").on("click", "button.viewed, button.closed, button.new", function(event){ event.preventDefault(); // prevent form submission - var $action = $(this).attr("name"); - var $report_id = $(this).data('report_id'); - var ajaxData = { - 'action': $action, - 'report_id': $report_id, - }; + var op = $(this).attr("name"); + var report_id = $(this).data('report_id'); - $.ajax({ - url: '/cgi-bin/koha/svc/problem_reports/', - type: 'POST', - dataType: 'json', - data: ajaxData, - }) - - .done(function(data){ - if (data.status == 'success'){ - if ( $action == 'viewed' ){ - $("#status_" + $report_id).text(_("Viewed")); + const client = APIClient.ticket; + if ( op == 'viewed' ) { + client.tickets.mark_as_viewed(report_id).then( + success => { + $("#status_" + report_id).text(_("Viewed")); $(event.target).parent().siblings("[name='status']").removeClass().addClass("statusViewed"); $(event.target).siblings(".closed").prop("disabled", false); $(event.target).siblings(".new").prop("disabled", false); $(event.target).prop("disabled", true); - } else if ( $action == 'new' ){ - $("#status_" + $report_id).text(_("New")); - $(event.target).parent().siblings("[name='status']").removeClass().addClass("statusNew"); - $(event.target).siblings(".closed").prop("disabled", false); - $(event.target).siblings(".viewed").prop("disabled", false); - $(event.target).prop("disabled", true); - } else { - $("#status_" + $report_id).text(_("Closed")); + }, + error => { + console.warn("Something wrong happened: %s".format(error)); + show_error(); + } + ); + } else if ( op == "closed" ) { + client.tickets.mark_as_closed(report_id).then( + success => { + $("#status_" + report_id).text(_("Closed")); $(event.target).parent().siblings("[name='status']").removeClass().addClass("statusClosed"); $(event.target).siblings(".viewed").prop("disabled", false); $(event.target).siblings(".new").prop("disabled", false); $(event.target).prop("disabled", true); + }, + error => { + console.warn("Something wrong happened: %s".format(error)); + show_error(); } - } else { - $("#error").text(_("Unable to change status of problem report.")); - $("#error").show(); - } - }) - .error(function(data){ - $("#error").text(_("Unable to change status of problem report.")); - $("#error").show(); - }); + ); + } else { + client.tickets.mark_as_new(report_id).then( + success => { + $("#status_" + report_id).text(_("New")); + $(event.target).parent().siblings("[name='status']").removeClass().addClass("statusNew"); + $(event.target).siblings(".closed").prop("disabled", false); + $(event.target).siblings(".viewed").prop("disabled", false); + $(event.target).prop("disabled", true); + }, + error => { + console.warn("Something wrong happened: %s".format(error)); + show_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 6d616768a5..6eb096de47 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/fetch/api-client.js +++ b/koha-tmpl/intranet-tmpl/prog/js/fetch/api-client.js @@ -6,6 +6,7 @@ import CoverImageAPIClient from "./cover-image-api-client.js"; import LocalizationAPIClient from "./localization-api-client.js"; import PatronAPIClient from "./patron-api-client.js"; import SysprefAPIClient from "./system-preferences-api-client.js"; +import TicketAPIClient from "./ticket-api-client.js"; export const APIClient = { article_request: new ArticleRequestAPIClient(), @@ -15,5 +16,6 @@ export const APIClient = { cover_image: new CoverImageAPIClient(), localization: new LocalizationAPIClient(), patron: new PatronAPIClient(), + ticket: new TicketAPIClient(), syspref: new SysprefAPIClient(), }; diff --git a/koha-tmpl/intranet-tmpl/prog/js/fetch/ticket-api-client.js b/koha-tmpl/intranet-tmpl/prog/js/fetch/ticket-api-client.js new file mode 100644 index 0000000000..6c6a5c6099 --- /dev/null +++ b/koha-tmpl/intranet-tmpl/prog/js/fetch/ticket-api-client.js @@ -0,0 +1,43 @@ +import HttpClient from "./http-client.js"; + +export class TicketAPIClient extends HttpClient { + constructor() { + super({ + baseURL: "/cgi-bin/koha/svc/", + }); + } + + get tickets () { + return { + mark_as_viewed: ticket_id => + this.post({ + endpoint: "problem_reports", + body: "report_id=%s&op=%s".format(ticket_id, "cud-viewed"), + headers: { + "Content-Type": + "application/x-www-form-urlencoded;charset=utf-8", + }, + }), + mark_as_closed: ticket_id => + this.post({ + endpoint: "problem_reports", + body: "report_id=%s&op=%s".format(ticket_id, "cud-closed"), + headers: { + "Content-Type": + "application/x-www-form-urlencoded;charset=utf-8", + }, + }), + mark_as_new: ticket_id => + this.post({ + endpoint: "problem_reports", + body: "report_id=%s&op=%s".format(ticket_id, "cud-new"), + headers: { + "Content-Type": + "application/x-www-form-urlencoded;charset=utf-8", + }, + }), + } + } +} + +export default TicketAPIClient; diff --git a/svc/problem_reports b/svc/problem_reports index 012596bae4..f84bcf38e0 100755 --- a/svc/problem_reports +++ b/svc/problem_reports @@ -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::ProblemReports; @@ -41,28 +40,18 @@ my ( $auth_status ) = check_cookie_auth( $query->cookie('CGISESSID'), { problem_ if ( $auth_status ne "ok" ) { exit 0; } +my $op = $query->param('op') || q{}; if ($is_ajax) { my $report_id = $query->param('report_id'); my $report = Koha::ProblemReports->find($report_id); - my $action = $query->param('action'); - my $status = 'success'; - if ( $action eq 'viewed' ) { + if ( $op eq 'cud-viewed' ) { $report->set({ status => 'Viewed' })->store; - if ( $report->status ne 'Viewed' ) { - $status = 'failure'; - } - } elsif ( $action eq 'closed' ) { + } elsif ( $op eq 'cud-closed' ) { $report->set({ status => 'Closed' })->store; - if ( $report->status ne 'Closed' ) { - $status = 'failure'; - } - } elsif ( $action eq 'new' ) { + } elsif ( $op eq 'cud-new' ) { $report->set({ status => 'New' })->store; - if ( $report->status ne 'New' ) { - $status = 'failure'; - } } - my $json = to_json ( { status => $status } ); + my $json = to_json ( { status => $report->status } ); output_with_http_headers $query, undef, $json, 'js'; exit; } -- 2.39.5