diff --git a/app/assets/javascripts/admin/line_items/controllers/line_items_controller.js.coffee b/app/assets/javascripts/admin/line_items/controllers/line_items_controller.js.coffee index 2a5b6233fb..eed001210e 100644 --- a/app/assets/javascripts/admin/line_items/controllers/line_items_controller.js.coffee +++ b/app/assets/javascripts/admin/line_items/controllers/line_items_controller.js.coffee @@ -5,6 +5,8 @@ angular.module("admin.lineItems").controller 'LineItemsCtrl', ($scope, $timeout, $scope.confirmDelete = true $scope.startDate = moment().startOf('day').subtract(7, 'days').format('YYYY-MM-DD') $scope.endDate = moment().startOf('day').format('YYYY-MM-DD') + $scope.previousDates = { startDate: $scope.startDate, endDate: $scope.endDate } + $scope.datesChangedOnCancelEvent = false $scope.bulkActions = [ { name: t("admin.orders.bulk_management.actions_delete"), callback: 'deleteLineItems' } ] $scope.selectedUnitsProduct = {} $scope.selectedUnitsVariant = {} @@ -26,9 +28,27 @@ angular.module("admin.lineItems").controller 'LineItemsCtrl', ($scope, $timeout, $scope.refreshData() $scope.$watchCollection "[startDate, endDate]", (newValues, oldValues) -> - if newValues != oldValues - $scope.refreshData() - + if newValues != oldValues && !$scope.datesChangedOnCancelEvent + state = $scope.refreshData() + if (state == "cancel") + $scope.datesChangedOnCancelEvent = true + $scope.cancelDateChange() + + $scope.cancelDateChange = -> + # Reset the date filters to the previous values + $scope.startDate = $scope.previousDates.startDate + $scope.endDate = $scope.previousDates.endDate + # throw a flatpickr:change event to change the date back in the datepicker + event = new CustomEvent('flatpickr:change', { + detail: { + startDate: $scope.previousDates.startDate, + endDate: $scope.previousDates.endDate + } + }) + window.dispatchEvent(event) + $timeout -> + $scope.datesChangedOnCancelEvent = false + $scope.refreshData = -> unless !$scope.orderCycleFilter? || $scope.orderCycleFilter == '' $scope.setOrderCycleDateRange() @@ -38,7 +58,7 @@ angular.module("admin.lineItems").controller 'LineItemsCtrl', ($scope, $timeout, return unless moment($scope.formattedStartDate).isValid() and moment($scope.formattedEndDate).isValid() - return unless $scope.confirmRefresh() + return "cancel" unless $scope.confirmRefresh() $scope.loadOrders() $scope.loadLineItems() @@ -47,6 +67,11 @@ angular.module("admin.lineItems").controller 'LineItemsCtrl', ($scope, $timeout, $scope.loadAssociatedData() $scope.dereferenceLoadedData() + + $timeout -> + # update the previous dates with the current ones since loading was successful + $scope.previousDates.startDate = $scope.startDate + $scope.previousDates.endDate = $scope.endDate $scope.setOrderCycleDateRange = -> start_date = OrderCycles.byID[$scope.orderCycleFilter].orders_open_at diff --git a/spec/system/admin/bulk_order_management_spec.rb b/spec/system/admin/bulk_order_management_spec.rb index e41675f43a..4eead43f93 100644 --- a/spec/system/admin/bulk_order_management_spec.rb +++ b/spec/system/admin/bulk_order_management_spec.rb @@ -616,6 +616,8 @@ describe ' find("input.datepicker").click select_dates_from_daterangepicker(today - 9.days, today) end + # daterange picker should have changed + expect(find("input.datepicker").value).to eq "#{today.prev_day(9).strftime('%F')} to #{today.strftime('%F')}" expect(page).to have_no_selector "#save-bar" within("tr#li_#{li2.id} td.quantity") do expect(page).to have_no_selector "input[name=quantity].ng-dirty" @@ -623,10 +625,13 @@ describe ' end it "shows a dialog and keeps changes when confirm dialog is rejected" do + previousdaterangestring = find("input.datepicker").value dismiss_confirm "Unsaved changes exist and will be lost if you continue." do find("input.datepicker").click select_dates_from_daterangepicker(today - 9.days, today) end + # daterange picker shouldn't have changed + expect(find("input.datepicker").value).to eq previousdaterangestring expect(page).to have_selector "#save-bar" within("tr#li_#{li2.id} td.quantity") do expect(page).to have_selector "input[name=quantity].ng-dirty"