diff --git a/app/assets/javascripts/admin/customers/controllers/customers_controller.js.coffee b/app/assets/javascripts/admin/customers/controllers/customers_controller.js.coffee index 9fdd44672c..573468f9ab 100644 --- a/app/assets/javascripts/admin/customers/controllers/customers_controller.js.coffee +++ b/app/assets/javascripts/admin/customers/controllers/customers_controller.js.coffee @@ -11,6 +11,9 @@ angular.module("admin.customers").controller "customersCtrl", ($scope, $q, $filt $scope.confirmRefresh = (event) -> event.preventDefault() unless pendingChanges.unsavedCount() == 0 || confirm(t("unsaved_changes_warning")) + $scope.hasUnsavedChanges = -> + pendingChanges.yes() + $scope.$watch "shop_id", -> if $scope.shop_id? CurrentShop.shop = $filter('filter')($scope.shops, {id: parseInt($scope.shop_id)}, true)[0] diff --git a/app/assets/javascripts/admin/index_utils/directives/obj_for_update.js.coffee b/app/assets/javascripts/admin/index_utils/directives/obj_for_update.js.coffee index 3ed4e6c98f..bf1edfdc20 100644 --- a/app/assets/javascripts/admin/index_utils/directives/obj_for_update.js.coffee +++ b/app/assets/javascripts/admin/index_utils/directives/obj_for_update.js.coffee @@ -4,15 +4,16 @@ angular.module("admin.indexUtils").directive "objForUpdate", (switchClass, pendi type: "@objForUpdate" attr: "@attrForUpdate" link: (scope, element, attrs) -> - scope.savedValue = scope.object()[scope.attr] + scope.savedValue = scope.object()[scope.attr] || "" scope.$watch "object().#{scope.attr}", (value) -> - if value == scope.savedValue + strValue = value || "" + if strValue == scope.savedValue pendingChanges.remove(scope.object().id, scope.attr) scope.clear() else scope.pending() - addPendingChange(scope.attr, value ? "") + addPendingChange(scope.attr, strValue) scope.reset = (value) -> scope.savedValue = value diff --git a/app/assets/javascripts/admin/index_utils/services/pending_changes.js.coffee b/app/assets/javascripts/admin/index_utils/services/pending_changes.js.coffee index 69373cc7e9..dafab4a4c3 100644 --- a/app/assets/javascripts/admin/index_utils/services/pending_changes.js.coffee +++ b/app/assets/javascripts/admin/index_utils/services/pending_changes.js.coffee @@ -16,7 +16,10 @@ angular.module("admin.indexUtils").factory "pendingChanges", ($q, resources, Sta remove: (id, attr) => if @pendingChanges.hasOwnProperty("#{id}") delete @pendingChanges["#{id}"]["#{attr}"] - delete @pendingChanges["#{id}"] if @changeCount( @pendingChanges["#{id}"] ) < 1 + + if @changeCount( @pendingChanges["#{id}"] ) < 1 + delete @pendingChanges["#{id}"] + StatusMessage.clear() submitAll: (form=null) => all = [] @@ -47,5 +50,8 @@ angular.module("admin.indexUtils").factory "pendingChanges", ($q, resources, Sta unsavedCount: -> Object.keys(@pendingChanges).length + yes: -> + @unsavedCount() > 0 + changeCount: (objectChanges) -> Object.keys(objectChanges).length diff --git a/app/assets/javascripts/admin/index_utils/services/switch_class.js.coffee b/app/assets/javascripts/admin/index_utils/services/switch_class.js.coffee index 0be1f2ec40..46eb0dc298 100644 --- a/app/assets/javascripts/admin/index_utils/services/switch_class.js.coffee +++ b/app/assets/javascripts/admin/index_utils/services/switch_class.js.coffee @@ -1,4 +1,4 @@ -angular.module("admin.indexUtils").factory "switchClass", ($timeout) -> +angular.module("admin.indexUtils").factory "switchClass", ($timeout, StatusMessage) -> return (element, classToAdd, removeClasses, timeout) -> $timeout.cancel element.timeout if element.timeout element.removeClass className for className in removeClasses @@ -7,4 +7,6 @@ angular.module("admin.indexUtils").factory "switchClass", ($timeout) -> if timeout && intRegex.test(timeout) element.timeout = $timeout(-> element.removeClass classToAdd + StatusMessage.clear() , timeout, true) + element diff --git a/app/controllers/admin/customers_controller.rb b/app/controllers/admin/customers_controller.rb index ff5c43ab63..a7e0038e80 100644 --- a/app/controllers/admin/customers_controller.rb +++ b/app/controllers/admin/customers_controller.rb @@ -51,6 +51,18 @@ module Admin end end + # copy of Admin::ResourceController without flash notice + def update + if @object.update(permitted_resource_params) + respond_with(@object) do |format| + format.html { redirect_to location_after_save } + format.js { render layout: false } + end + else + respond_with(@object) + end + end + # copy of Admin::ResourceController without flash notice def destroy if @object.destroy diff --git a/app/views/admin/customers/index.html.haml b/app/views/admin/customers/index.html.haml index 3ba03ca538..a61fc61cf9 100644 --- a/app/views/admin/customers/index.html.haml +++ b/app/views/admin/customers/index.html.haml @@ -44,8 +44,8 @@ %h1#no_results{ 'ng-show' => '!RequestMonitor.loading && filteredCustomers.length == 0' } =t :no_customers_found - %save-bar{ dirty: "customers_form.$dirty", persist: "false" } - %input.red{ type: "button", value: t(:save_changes), "ng-click": "submitAll(customers_form)" } + %save-bar{ persist: "filteredCustomers.length > 0" } + %input.red{ type: "button", value: t(:save_changes), "ng-click": "submitAll(customers_form)", "ng-disabled": "!hasUnsavedChanges()" } %table.index#customers{ 'ng-show' => '!RequestMonitor.loading && filteredCustomers.length > 0' } %col.email{ width: "20%", 'ng-show' => 'columns.email.visible' } diff --git a/app/webpacker/css/admin/orders.scss b/app/webpacker/css/admin/orders.scss index 2e51e74936..5aec38c538 100644 --- a/app/webpacker/css/admin/orders.scss +++ b/app/webpacker/css/admin/orders.scss @@ -9,6 +9,10 @@ input, div { &.update-pending { border: solid 1px orange; + + &:focus { + border: solid 1px orange; + } } } diff --git a/spec/system/admin/customers_spec.rb b/spec/system/admin/customers_spec.rb index fdcc76b673..2033654f41 100644 --- a/spec/system/admin/customers_spec.rb +++ b/spec/system/admin/customers_spec.rb @@ -214,6 +214,7 @@ RSpec.describe 'Customers' do expect(page).to have_content 'You have unsaved changes' click_button "Save Changes" + expect(page).to have_content 'All changes saved successfully' # changes are saved in the database expect(customer4.reload.code).to eq(nil) @@ -250,6 +251,7 @@ RSpec.describe 'Customers' do expect(page).to have_content 'You have unsaved changes' click_button "Save Changes" + expect(page).to have_content 'All changes saved successfully' expect(customer4.reload.tag_list).to be_empty end @@ -259,6 +261,17 @@ RSpec.describe 'Customers' do it "allows updating of attributes" do select2_select managed_distributor1.name, from: "shop_id" + expect(page).to have_button "Save Changes", disabled: true + + # Editing attributes but undoing changes + within("tr#c_#{customer1.id}") { fill_in "first_name", with: "customer abc" } + expect(page).to have_content 'You have unsaved changes' + within("tr#c_#{customer1.id}") { fill_in "first_name", with: "John" } + expect(page).not_to have_content 'You have unsaved changes' + within("tr#c_#{customer1.id}") { fill_in "code", with: "new-customer-code" } + expect(page).to have_content 'You have unsaved changes' + within("tr#c_#{customer1.id}") { fill_in "code", with: "" } + expect(page).not_to have_content 'You have unsaved changes' within "tr#c_#{customer1.id}" do expect(find_field('first_name').value).to eq 'John'