From c2cfdf4ff6b3dc9a54f7180473e1e3d8337ee8b3 Mon Sep 17 00:00:00 2001
From: Antonin <zepcome@gmail.com>
Date: Mon, 8 Aug 2016 11:50:39 +0200
Subject: [PATCH] Manage button state in the new feature

---
 js/app/date_poll.js | 20 ++++++++++++--------
 1 file changed, 12 insertions(+), 8 deletions(-)

diff --git a/js/app/date_poll.js b/js/app/date_poll.js
index 43601dad..db90cbde 100644
--- a/js/app/date_poll.js
+++ b/js/app/date_poll.js
@@ -75,8 +75,8 @@ $(document).ready(function () {
 
     function getLastDayNumber(last_day) {
         if (last_day == null)
-            last_day = $selected_days.find('fieldset').filter(':last')
-        return parseInt(/^d([0-9]+)-h[0-9]+$/.exec(last_day.find('.hours').filter(':first').attr('id'))[1])
+            last_day = $selected_days.find('fieldset').filter(':last');
+        return parseInt(/^d([0-9]+)-h[0-9]+$/.exec($(last_day).find('.hours').filter(':first').attr('id'))[1])
     }
 
     function newDateFields(dateStr) {
@@ -102,6 +102,14 @@ $(document).ready(function () {
         $removeaday_and_copyhours.removeClass('disabled');
     }
 
+    function manageRemoveadayAndCopyhoursButtons() {
+        var nb_days = $selected_days.find('fieldset').length;
+        $('#day' + (getLastDayNumber() - 1)).focus();
+        if (nb_days == 1) {
+            $removeaday_and_copyhours.addClass('disabled');
+        }
+    }
+
     var useFirstEmptyDateField = function (dateStr) {
         var used = false;
         $selected_days.find('fieldset legend input').each(function () {
@@ -225,12 +233,7 @@ $(document).ready(function () {
     $('#remove-a-day').on('click', function () {
         $selected_days.find('fieldset:last').remove();
 
-        var nb_days = $selected_days.find('fieldset').length;
-
-        $('#day' + (getLastDayNumber() - 1)).focus();
-        if (nb_days == 1) {
-            $removeaday_and_copyhours.addClass('disabled');
-        }
+        manageRemoveadayAndCopyhoursButtons();
         submitDaysAvalaible();
     });
 
@@ -240,6 +243,7 @@ $(document).ready(function () {
         if ($('#days_container').find('fieldset').length > 1) {
             $(this).parents('fieldset').remove();
         }
+        manageRemoveadayAndCopyhoursButtons();
         submitDaysAvalaible();
     });
 
-- 
GitLab