When confirm modal is dismissed, reset daterange picker to previous values

+ update specs as well
This commit is contained in:
Jean-Baptiste Bellet
2022-11-03 16:58:06 +01:00
parent 228e484f02
commit 39e5613db8
2 changed files with 34 additions and 4 deletions

View File

@@ -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

View File

@@ -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"