From b146a4567a5010a433903ecbdf1d22368b87393e Mon Sep 17 00:00:00 2001
From: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Date: Wed, 16 Nov 2022 07:40:59 +0100
Subject: [PATCH] Bug 31891: (bug 27421 follow-up) Remove one additional click

From comment 0
"""
Moving marc batch import actions to background jobs has introduced some confusing
clicks to the process. Before this change, after staging a batch one was taken
to a "MARC staging results" page with details of how the staging went and, most
importantly, a link straight to the batch you'd just staged
(at manage-marc-import.pl?import_batch_id).

Now, however, one must click the status bar to get to details of the job
and then click "View batch" from there. Not only is that an extra click
and page load, but neither of those links are particularly large or
obvious.
"""

Test plan:
- Stage a record for import, notice that the "View batch" link appears
when the job is finished
- Add order to basket from a new file, select a file and import
Notice that the "Add staged files to basket" link is displayed when the
job is finished

Note for QA: We should have a js_callback in the background_job include
file instead.
---
 .../prog/en/modules/tools/stage-marc-import.tt   | 16 +++++++++++++---
 koha-tmpl/intranet-tmpl/prog/js/job_progess.js   |  5 +++--
 2 files changed, 16 insertions(+), 5 deletions(-)

diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/stage-marc-import.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/stage-marc-import.tt
index 6c6be6c43f1..dfb1d05fe7c 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/stage-marc-import.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/stage-marc-import.tt
@@ -79,6 +79,7 @@
               <p>The job has been enqueued! It will be processed as soon as possible.</p>
                [% INCLUDE "job_progress.inc" job_id=job_id %]
               <p><a class="job_details" href="/cgi-bin/koha/admin/background_jobs.pl?op=view&id=[% job_id | uri %]" title="View detail of the enqueued job">View detail of the enqueued job</a>
+              <div id="job_callback"></div>
             </div>
         [% ELSE %]
 <h1>Stage MARC records for import</h1>
@@ -105,8 +106,7 @@
 
     <div id="fileuploadpanel">
         <div id="fileuploadstatus" class="progress_panel">Upload progress:
-            <progress id="fileuploadprogress" max="100" value="0">
-            </progress>
+            <progress id="fileuploadprogress" max="100" value="0"></progress>
             <span class="fileuploadpercent">0</span>%
         </div>
         <div id="fileuploadfailed"></div>
@@ -428,7 +428,17 @@
                 })
             });
             [% IF job_enqueued %]
-                updateProgress([% job_id | html %]);
+                updateProgress([% job_id | html %], function(){
+                    $.getJSON('/api/v1/jobs/[% job_id | html %]', function(job){
+                        let import_batch_id = job.data.report.import_batch_id;
+                        $('<p><a href="/cgi-bin/koha/tools/manage-marc-import.pl?import_batch_id=%s">%s</a></p>'.format(import_batch_id, _("View batch"))).appendTo("#job_callback");
+                        let basket_id = job.data.report.basket_id;
+                        let vendor_id = job.data.report.vendor_id;
+                        if ( basket_id && vendor_id ) {
+                            $('<p><a id="addtobasket" class="btn btn-default" href="/cgi-bin/koha/acqui/addorderiso2709.pl?import_batch_id=%s&basketno=%s&booksellerid=%s">%s</a></p>'.format(import_batch_id, basket_id, vendor_id, _("Add staged files to basket"))).appendTo("#job_callback");
+                        }
+                    });
+                });
             [% END %]
         });
 
diff --git a/koha-tmpl/intranet-tmpl/prog/js/job_progess.js b/koha-tmpl/intranet-tmpl/prog/js/job_progess.js
index 5b915075962..8da44420a17 100644
--- a/koha-tmpl/intranet-tmpl/prog/js/job_progess.js
+++ b/koha-tmpl/intranet-tmpl/prog/js/job_progess.js
@@ -1,4 +1,4 @@
-function updateProgress(job_id) {
+function updateProgress(job_id, callback) {
     $.getJSON('/api/v1/jobs/' + job_id, function(job){
         let recheck = true;
 
@@ -19,6 +19,7 @@ function updateProgress(job_id) {
             $('#progress-bar-' + job_id).addClass("progress-bar-success");
             $('#progress-bar-' + job_id).attr('aria-valuenow', 100).css("width", "100%");
             recheck = false;
+            callback();
         } else if ( job.status == "failed" ) {
             $('#job-percent-' + job_id).text(0);
             $('#job-status-' + job_id).text(JOB_PROGRESS_FAILED);
@@ -28,7 +29,7 @@ function updateProgress(job_id) {
         }
 
         if ( recheck ) {
-            setTimeout(function(){updateProgress(job_id)}, 1 * 1000);
+            setTimeout(function(){updateProgress(job_id, callback)}, 1 * 1000);
         }
     });
 }
-- 
2.25.1