From 61f1f88c5c871cf86138f81ed6f2e3db63b78d04 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Tue, 20 Feb 2024 16:43:51 +0100 Subject: [PATCH] Bug 36084: svc - clubs Signed-off-by: Jonathan Druart --- .../prog/en/modules/clubs/clubs.tt | 34 +++++----- .../prog/en/modules/clubs/patron-clubs-tab.tt | 17 ++--- .../prog/en/modules/clubs/patron-enroll.tt | 18 ++--- .../intranet-tmpl/prog/js/fetch/api-client.js | 2 + .../prog/js/fetch/club-api-client.js | 66 +++++++++++++++++++ svc/club/delete | 3 +- svc/club/enroll | 5 +- svc/club/template/delete | 3 +- 8 files changed, 113 insertions(+), 35 deletions(-) create mode 100644 koha-tmpl/intranet-tmpl/prog/js/fetch/club-api-client.js diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/clubs/clubs.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/clubs/clubs.tt index ad03620b63..d93f478d8c 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/clubs/clubs.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/clubs/clubs.tt @@ -204,37 +204,39 @@ function ConfirmDeleteTemplate( id, name ) { if ( confirm( _("Are you sure you want to delete the club template %s? This will delete all clubs using this template and cancel patron enrollments" ).format(name) ) ) { - $.ajax({ - type: "POST", - url: '/cgi-bin/koha/svc/club/template/delete', - data: { id: id }, - success: function( data ) { - if ( data.success ) { + const client = APIClient.club; + client.templates.delete(id).then( + success => { + if ( success.success ) { location.reload(); } else { alert(_("Unable to delete template!")); } }, - dataType: 'json' - }); + error => { + console.warn("Something wrong happened: %s".format(error)); + alert(_("Unable to delete template!")); + } + ); } } function ConfirmDeleteClub( id, name ) { if ( confirm( _("Are you sure you want to delete the club %s? This will cancel all patron enrollments in this club." ).format(name) ) ) { - $.ajax({ - type: "POST", - url: '/cgi-bin/koha/svc/club/delete', - data: { id: id }, - success: function( data ) { - if ( data.success ) { + const client = APIClient.club; + client.clubs.delete(id).then( + success => { + if ( success.success ) { location.reload(); } else { alert(_("Unable to delete club!")); } }, - dataType: 'json' - }); + error => { + console.warn("Something wrong happened: %s".format(error)); + alert(_("Unable to delete club!")); + } + ); } } diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/clubs/patron-clubs-tab.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/clubs/patron-clubs-tab.tt index 86acd025ef..855683ea26 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/clubs/patron-clubs-tab.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/clubs/patron-clubs-tab.tt @@ -86,12 +86,10 @@ function loadEnrollmentForm( id, enrollent_id = 0 ) { function cancelEnrollment( id ) { $("body").css("cursor", "progress"); - $.ajax({ - type: "POST", - url: '/cgi-bin/koha/svc/club/cancel_enrollment', - data: { id: id }, - success: function( data ) { - if ( data.success ) { + const client = APIClient.club; + client.enrollments.cancel(id).then( + success => { + if ( success.success ) { $('#clubs_panel').load('/cgi-bin/koha/clubs/patron-clubs-tab.pl?borrowernumber=[% borrowernumber | html %]', function() { $("body").css("cursor", "default"); }); @@ -99,8 +97,11 @@ function cancelEnrollment( id ) { alert(_("Unable to cancel enrollment!")); } }, - dataType: 'json' - }); + error => { + console.warn("Something wrong happened: %s".format(error)); + alert(_("Unable to cancel enrollment!")); + } + ); return false; } diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/clubs/patron-enroll.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/clubs/patron-enroll.tt index 354194dc99..8db62bdf25 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/clubs/patron-enroll.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/clubs/patron-enroll.tt @@ -67,12 +67,11 @@ if([% enrollent_id | html %]){ } function addEnrollment() { $("body").css("cursor", "progress"); - $.ajax({ - type: "POST", - url: '/cgi-bin/koha/svc/club/enroll', - data: $( "#patron-enrollment-form" ).serialize(), - success: function( data ) { - if ( data.success ) { + const client = APIClient.club; + let data = $( "#patron-enrollment-form" ).serialize(); + client.enrollments.enroll(data).then( + success => { + if ( success.success ) { $('#clubs_panel').load('/cgi-bin/koha/clubs/patron-clubs-tab.pl?borrowernumber=[% borrowernumber | html %]&id=[% club.id | html %]', function() { $("body").css("cursor", "default"); }); @@ -80,8 +79,11 @@ function addEnrollment() { alert(_("Unable to create enrollment!")); } }, - dataType: 'json' - }); + error => { + console.warn("Something wrong happened: %s".format(error)); + alert(_("Unable to create enrollment!")); + } + ); return false; } 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 d44025cec1..40abda2dc6 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/fetch/api-client.js +++ b/koha-tmpl/intranet-tmpl/prog/js/fetch/api-client.js @@ -1,11 +1,13 @@ import ArticleRequestAPIClient from "./article-request-api-client.js"; import AVAPIClient from "./authorised-value-api-client.js"; import CirculationAPIClient from "./circulation-api-client.js"; +import ClubAPIClient from "./club-api-client.js"; import SysprefAPIClient from "./system-preferences-api-client.js"; export const APIClient = { article_request: new ArticleRequestAPIClient(), authorised_value: new AVAPIClient(), circulation: new CirculationAPIClient(), + club: new ClubAPIClient(), syspref: new SysprefAPIClient(), }; diff --git a/koha-tmpl/intranet-tmpl/prog/js/fetch/club-api-client.js b/koha-tmpl/intranet-tmpl/prog/js/fetch/club-api-client.js new file mode 100644 index 0000000000..102a01c8ec --- /dev/null +++ b/koha-tmpl/intranet-tmpl/prog/js/fetch/club-api-client.js @@ -0,0 +1,66 @@ +import HttpClient from "./http-client.js"; + +export class ClubAPIClient extends HttpClient { + constructor() { + super({ + baseURL: "/cgi-bin/koha/svc/club/", + }); + } + + get templates() { + return { + delete: template_id => + this.post({ + endpoint: "template/delete", + body: "id=%s&op=%s".format(template_id, "cud-delete"), + headers: { + "Content-Type": + "application/x-www-form-urlencoded;charset=utf-8", + }, + }), + }; + } + + get clubs() { + return { + delete: club_id => + this.post({ + endpoint: "delete", + body: "id=%s&op=%s".format(club_id, "cud-delete"), + headers: { + "Content-Type": + "application/x-www-form-urlencoded;charset=utf-8", + }, + }), + }; + } + + get enrollments() { + return { + cancel: enrollment_id => + this.post({ + endpoint: "cancel_enrollment", + body: "id=%s&op=%s".format(enrollment_id, "cud-delete"), + headers: { + "Content-Type": + "application/x-www-form-urlencoded;charset=utf-8", + }, + }), + + enroll: data => + this.post({ + endpoint: "enroll", + body: "%s&op=%s".format( + data, // Could do better, but too much work for now! + "cud-enroll" + ), + headers: { + "Content-Type": + "application/x-www-form-urlencoded;charset=utf-8", + }, + }), + }; + } +} + +export default ClubAPIClient; diff --git a/svc/club/delete b/svc/club/delete index 6d7bcf5c08..b320190679 100755 --- a/svc/club/delete +++ b/svc/club/delete @@ -35,10 +35,11 @@ if ( $auth_status ne "ok" ) { my $success = 0; +my $op = $cgi->param('op') || q{}; my $id = $cgi->param('id'); my $club = Koha::Clubs->find($id); -if ($club) { +if ($club && $op eq 'cud-delete') { $success = $club->delete(); } diff --git a/svc/club/enroll b/svc/club/enroll index 0d7762b8b9..40d9391ad5 100755 --- a/svc/club/enroll +++ b/svc/club/enroll @@ -36,6 +36,9 @@ if ( $auth_status ne "ok" ) { exit 0; } +my $op = $cgi->param('op') || q{}; +exit unless $op eq 'cud-enroll'; # FIXME could be nicer here. + my $id = $cgi->param('id'); my $borrowernumber = $cgi->param('borrowernumber'); my $enrollent_id = scalar $cgi->param('enrollent_id'); @@ -94,4 +97,4 @@ if($enrollent_id){ } else{ print to_json( { success => $enrollment ? 1 : 0 } ); -} \ No newline at end of file +} diff --git a/svc/club/template/delete b/svc/club/template/delete index 8166ca2905..095bfacee4 100755 --- a/svc/club/template/delete +++ b/svc/club/template/delete @@ -36,10 +36,11 @@ if ( $auth_status ne "ok" ) { my $success = 0; +my $op = $cgi->param('op') || q{}; my $id = $cgi->param('id'); my $club_template = Koha::Club::Templates->find($id); -if ($club_template) { +if ($club_template && $op eq 'cud-delete') { $success = $club_template->delete(); } -- 2.39.5