From 492a8439d5d4b1ef749af79b23ee059fdc6838d9 Mon Sep 17 00:00:00 2001
From: Todd Dembrey <todd.dembrey@torchbox.com>
Date: Tue, 19 Feb 2019 09:13:45 +0000
Subject: [PATCH] GH-858: Enable and disable the button depending on valid
 actions

---
 .../src/javascript/apply/batch-actions.js     | 43 +++++++++++++------
 1 file changed, 29 insertions(+), 14 deletions(-)

diff --git a/opentech/static_src/src/javascript/apply/batch-actions.js b/opentech/static_src/src/javascript/apply/batch-actions.js
index 1e9a75582..05130ee6d 100644
--- a/opentech/static_src/src/javascript/apply/batch-actions.js
+++ b/opentech/static_src/src/javascript/apply/batch-actions.js
@@ -7,6 +7,7 @@
     const $allCheckboxInput = $('.js-batch-select-all');
     const $batchButtons = $('.js-batch-button');
     const $batchProgress = $('.js-batch-progress');
+    const $actionOptions = $('#id_action option');
     const $batchTitlesList = $('.js-batch-titles');
     const $batchTitleCount = $('.js-batch-title-count');
     const $hiddenIDlist = $('#id_submission_ids');
@@ -33,6 +34,7 @@
 
         toggleBatchActions();
         updateCount();
+        updateProgressButton();
     });
 
     $checkbox.change(function () {
@@ -46,6 +48,8 @@
         if (!$(this).is(':checked') && $allCheckboxInput.is(':checked')) {
             resetCheckAllInput();
         }
+
+        updateProgressButton();
     });
 
     // append selected project titles to batch update reviewer modal
@@ -56,20 +60,7 @@
     });
 
     $batchProgress.click(function () {
-        const $actionOptions = $('#id_action option');
-        var actions = $actionOptions.map(function() { return this.value; }).get();
-        $checkbox.filter(':checked').each(function () {
-            let newActions = $(this).parents('tr').find('.js-actions').data('actions');
-            actions = actions.filter(action => newActions.includes(action));
-        });
-        $actionOptions.each(function() {
-            if (!actions.includes(this.value)) {
-                $(this).attr("disabled", "disabled");
-            } else {
-                $(this).removeAttr("disabled");
-            }
-        });
-        $actionOptions.filter(':enabled:first').prop('selected', true);;
+        updateProgressButton();
     });
 
     // show/hide the list of actions
@@ -111,6 +102,30 @@
         $hiddenIDlist.val(selectedIDs.join(','));
     }
 
+    function updateProgressButton() {
+        var actions = $actionOptions.map(function () { return this.value; }).get();
+        $checkbox.filter(':checked').each(function () {
+            let newActions = $(this).parents('tr').find('.js-actions').data('actions');
+            actions = actions.filter(action => newActions.includes(action));
+        });
+        $actionOptions.each(function () {
+            if (!actions.includes(this.value)) {
+                $(this).attr('disabled', 'disabled');
+            }
+            else {
+                $(this).removeAttr('disabled');
+            }
+        });
+        $actionOptions.filter(':enabled:first').prop('selected', true);
+        if (actions.length === 0) {
+            $batchProgress.attr('disabled', 'disabled');
+        }
+        else {
+            $batchProgress.removeAttr('disabled');
+        }
+    }
+
+
     function toggleBatchActions() {
         if ($('.js-batch-select:checked').length) {
             $body.addClass(activeClass);
-- 
GitLab