From d4ce4d8fa25b8f44b4e7ae0e0716608bb0cfc3ff Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Fri, 23 Feb 2024 09:51:04 +0100 Subject: [PATCH] Bug 36084: svc - recall Signed-off-by: Jonathan Druart --- .../prog/en/includes/recalls.inc | 10 +- .../intranet-tmpl/prog/js/fetch/api-client.js | 4 +- .../prog/js/fetch/recall-api-client.js | 64 ++++++ koha-tmpl/intranet-tmpl/prog/js/recalls.js | 182 +++++++----------- svc/recall | 12 +- 5 files changed, 150 insertions(+), 122 deletions(-) create mode 100644 koha-tmpl/intranet-tmpl/prog/js/fetch/recall-api-client.js diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/recalls.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/recalls.inc index 426b974836..46dcf9ffe5 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/includes/recalls.inc +++ b/koha-tmpl/intranet-tmpl/prog/en/includes/recalls.inc @@ -102,15 +102,15 @@ 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 6eb096de47..87b240ac5c 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/fetch/api-client.js +++ b/koha-tmpl/intranet-tmpl/prog/js/fetch/api-client.js @@ -5,6 +5,7 @@ import ClubAPIClient from "./club-api-client.js"; import CoverImageAPIClient from "./cover-image-api-client.js"; import LocalizationAPIClient from "./localization-api-client.js"; import PatronAPIClient from "./patron-api-client.js"; +import RecallAPIClient from "./recall-api-client.js"; import SysprefAPIClient from "./system-preferences-api-client.js"; import TicketAPIClient from "./ticket-api-client.js"; @@ -16,6 +17,7 @@ export const APIClient = { cover_image: new CoverImageAPIClient(), localization: new LocalizationAPIClient(), patron: new PatronAPIClient(), - ticket: new TicketAPIClient(), + recall: new RecallAPIClient(), syspref: new SysprefAPIClient(), + ticket: new TicketAPIClient(), }; diff --git a/koha-tmpl/intranet-tmpl/prog/js/fetch/recall-api-client.js b/koha-tmpl/intranet-tmpl/prog/js/fetch/recall-api-client.js new file mode 100644 index 0000000000..d7f04eb8ae --- /dev/null +++ b/koha-tmpl/intranet-tmpl/prog/js/fetch/recall-api-client.js @@ -0,0 +1,64 @@ +import HttpClient from "./http-client.js"; + +export class RecallAPIClient extends HttpClient { + constructor() { + super({ + baseURL: "/cgi-bin/koha/svc/recall", + }); + } + + get recalls() { + return { + cancel: recall_id => + this.post({ + endpoint: "", + body: "recall_id=%s&op=%s".format(recall_id, "cud-cancel"), + headers: { + "Content-Type": + "application/x-www-form-urlencoded;charset=utf-8", + }, + }), + expire: recall_id => + this.post({ + endpoint: "", + body: "recall_id=%s&op=%s".format(recall_id, "cud-expire"), + headers: { + "Content-Type": + "application/x-www-form-urlencoded;charset=utf-8", + }, + }), + + revert: recall_id => + this.post({ + endpoint: "", + body: "recall_id=%s&op=%s".format(recall_id, "cud-revert"), + headers: { + "Content-Type": + "application/x-www-form-urlencoded;charset=utf-8", + }, + }), + + overdue: recall_id => + this.post({ + endpoint: "", + body: "recall_id=%s&op=%s".format(recall_id, "cud-overdue"), + headers: { + "Content-Type": + "application/x-www-form-urlencoded;charset=utf-8", + }, + }), + + transit: recall_id => + this.post({ + endpoint: "", + body: "recall_id=%s&op=%s".format(recall_id, "cud-transit"), + headers: { + "Content-Type": + "application/x-www-form-urlencoded;charset=utf-8", + }, + }), + }; + } +} + +export default RecallAPIClient; diff --git a/koha-tmpl/intranet-tmpl/prog/js/recalls.js b/koha-tmpl/intranet-tmpl/prog/js/recalls.js index df7faac0fe..9ab3494f1b 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/recalls.js +++ b/koha-tmpl/intranet-tmpl/prog/js/recalls.js @@ -1,142 +1,104 @@ $(document).ready(function() { + const client = APIClient.recall; + $(".cancel_recall").click(function(e){ if (confirmDelete(__("Are you sure you want to remove this recall?"))){ - var $self = $(this); - var $recall_id = $(this).data('id'); - var $action = $(this).data('action'); - var ajaxData = { - 'recall_id': $recall_id, - 'action' : $action, - }; - - $.ajax({ - url: '/cgi-bin/koha/svc/recall', - type: 'POST', - dataType: 'json', - data: ajaxData, - }) - .done(function(data) { - var message = ""; - if(data.success == 0) { - message = __("The recall may have already been cancelled. Please refresh the page."); - } else { - message = __("Cancelled"); + let td_node = $(this).parents('td'); + let recall_id = $(this).data('id'); + client.recalls.cancel(recall_id).then( + success => { + if(success.success == 0) { + message = __("The recall may have already been cancelled. Please refresh the page."); + } else { + message = __("Cancelled"); + } + td_node.html(message); + }, + error => { + console.warn("Something wrong happened: %s".format(error)); } - $self.parent().parent().parent().parent().html(message); - }); + ); } }); $(".expire_recall").click(function(e){ if (confirmDelete(__("Are you sure you want to expire this recall?"))){ - var $self = $(this); - var $recall_id = $(this).data('id'); - var $action = $(this).data('action'); - var ajaxData = { - 'recall_id': $recall_id, - 'action' : $action, - }; - - $.ajax({ - url: '/cgi-bin/koha/svc/recall', - type: 'POST', - dataType: 'json', - data: ajaxData, - }) - .done(function(data) { - var message = ""; - if(data.success == 0) { - message = __("The recall may have already been expired. Please refresh the page."); - } else { - message = __("Expired"); + let td_node = $(this).parents('td'); + let recall_id = $(this).data('id'); + client.recalls.expire(recall_id).then( + success => { + if(success.success == 0) { + message = __("The recall may have already been expired. Please refresh the page."); + } else { + message = __("Expired"); + } + td_node.html(message); + }, + error => { + console.warn("Something wrong happened: %s".format(error)); } - $self.parent().parent().parent().parent().html(message); - }); + ); } }); $(".revert_recall").click(function(e){ if (confirmDelete(__("Are you sure you want to revert the waiting status of this recall?"))){ - var $self = $(this); - var $recall_id = $(this).data('id'); - var $action = $(this).data('action'); - var ajaxData = { - 'recall_id': $recall_id, - 'action' : $action, - }; - - $.ajax({ - url: '/cgi-bin/koha/svc/recall', - type: 'POST', - dataType: 'json', - data: ajaxData, - }) - .done(function(data) { - var message = ""; - if(data.success == 0) { - message = __("The recall waiting status may have already been reverted. Please refresh the page."); - } else { - message = __("Waiting status reverted"); + let td_node = $(this).parents('td'); + let recall_id = $(this).data('id'); + client.recalls.revert(recall_id).then( + success => { + if(success.success == 0) { + message = __("The recall may have already been reverted. Please refresh the page."); + } else { + message = __("Waiting status reverted"); + } + td_node.html(message); + }, + error => { + console.warn("Something wrong happened: %s".format(error)); } - $self.parent().parent().parent().parent().html(message); - }); + ); } }); $(".overdue_recall").click(function(e){ if (confirmDelete(__("Are you sure you want to mark this recall as overdue?"))){ - var $self = $(this); - var $recall_id = $(this).data('id'); - var $action = $(this).data('action'); - var ajaxData = { - 'recall_id': $recall_id, - 'action' : $action, - }; - - $.ajax({ - url: '/cgi-bin/koha/svc/recall', - type: 'POST', - dataType: 'json', - data: ajaxData, - }) - .done(function(data) { - var message = ""; - if(data.success == 0) { - message = __("The recall may have already been marked as overdue. Please refresh the page."); - } else { - message = __("Marked overdue"); + let td_node = $(this).parents('td'); + let recall_id = $(this).data('id'); + client.recalls.overdue(recall_id).then( + success => { + if(success.success == 0) { + message = __("The recall may have already been marked as overdue. Please refresh the page."); + } else { + message = __("Marked overdue"); + } + td_node.html(message); + }, + error => { + console.warn("Something wrong happened: %s".format(error)); } - $self.parent().parent().parent().parent().html(message); - }); + ); } }); $(".transit_recall").click(function(e){ if (confirmDelete(__("Are you sure you want to remove this recall and return the item to it's home library?"))){ - var $self = $(this); - var $recall_id = $(this).data('id'); - var $action = $(this).data('action'); - var ajaxData = { - 'recall_id': $recall_id, - 'action' : $action, - }; - - $.ajax({ - url: '/cgi-bin/koha/svc/recall', - type: 'POST', - dataType: 'json', - data: ajaxData, - }) - .done(function(data) { - var message = ""; - if(data.success == 0) { - message = __("The recall may have already been removed. Please refresh the page."); - } else { - message = __("Cancelled"); + let td_node = $(this).parents('td'); + let recall_id = $(this).data('id'); + client.recalls.transit(recall_id).then( + success => { + if(success.success == 0) { + message = __("The recall may have already been removed. Please refresh the page."); + } else { + message = __("Cancelled"); + } + td_node.html(message); + }, + error => { + console.warn("Something wrong happened: %s".format(error)); } - $self.parent().parent().parent().parent().html(message); - }); + ); } }); diff --git a/svc/recall b/svc/recall index aad5d67155..75eb2d39bd 100755 --- a/svc/recall +++ b/svc/recall @@ -45,37 +45,37 @@ unless ( $recall ) { exit; } -my $op = $input->param('action'); +my $op = $input->param('op'); -if ( $op eq 'cancel' ) { +if ( $op eq 'cud-cancel' ) { # cancel recall $recall->set_cancelled; if ( $recall->cancelled ){ $json = encode_json({ success => 1 }); } -} elsif ( $op eq 'expire' ) { +} elsif ( $op eq 'cud-expire' ) { # expire recall $recall->set_expired({ interface => 'INTRANET' }); if ( $recall->expired ){ $json = encode_json({ success => 1 }); } -} elsif ( $op eq 'revert' ) { +} elsif ( $op eq 'cud-revert' ) { # revert recall waiting status $recall->revert_waiting; if ( $recall->requested ){ $json = encode_json({ success => 1 }); } -} elsif ( $op eq 'overdue' ) { +} elsif ( $op eq 'cud-overdue' ) { # mark recall as overdue $recall->set_overdue({ interface => 'INTRANET' }); if ( $recall->overdue ){ $json = encode_json({ success => 1 }); } -} elsif ( $op eq 'transit' ) { +} elsif ( $op eq 'cud-transit' ) { # cancel recall and return item to home library if ( $recall->in_transit ) { C4::Items::ModItemTransfer( -- 2.39.5