Merge pull request #13679 from deivid-rodriguez/customer-edition

Improve `/admin/customers` form handling
This commit is contained in:
Filipe
2025-11-21 10:51:55 +00:00
committed by GitHub
8 changed files with 48 additions and 7 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -9,6 +9,10 @@ input,
div {
&.update-pending {
border: solid 1px orange;
&:focus {
border: solid 1px orange;
}
}
}

View File

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