From ea23be219da4490af89f0d980ba00a373bbbc333 Mon Sep 17 00:00:00 2001 From: Daniele Palombo Date: Sat, 11 Jul 2020 08:55:54 +0200 Subject: [PATCH] Use selectable.js code on return items. This commit updates the JS and HTML code related to return items, to use the selectable.js code. You can see the code working on: /admin/orders/ORDER_NUMBER/customer_returns/new /admin/orders/ORDER_NUMBER/return_authorizations/new return_item_selection.js file is removed in favor of HBS template. --- .../app/assets/javascripts/spree/backend.js | 2 +- .../selectable_table/return_items.js | 5 +++ .../backend/returns/return_item_selection.js | 35 ------------------- .../spree/backend/templates/index.js | 1 + .../tables/return_item_sum_amount.hbs | 7 ++++ .../javascripts/spree/backend/views/index.js | 2 ++ .../backend/views/tables/return_items.js | 9 +++++ .../sum_return_item_amount.js | 32 +++++++++++++++++ .../_return_item_selection.html.erb | 5 ++- .../return_authorizations/_form.html.erb | 13 ++----- .../orders/return_authorizations_spec.rb | 2 +- 11 files changed, 63 insertions(+), 50 deletions(-) create mode 100644 backend/app/assets/javascripts/spree/backend/components/selectable_table/return_items.js delete mode 100644 backend/app/assets/javascripts/spree/backend/returns/return_item_selection.js create mode 100644 backend/app/assets/javascripts/spree/backend/templates/tables/return_item_sum_amount.hbs create mode 100644 backend/app/assets/javascripts/spree/backend/views/tables/return_items.js create mode 100644 backend/app/assets/javascripts/spree/backend/views/tables/selectable_table/sum_return_item_amount.js diff --git a/backend/app/assets/javascripts/spree/backend.js b/backend/app/assets/javascripts/spree/backend.js index 4c7d43ff979..2b3a52922b0 100644 --- a/backend/app/assets/javascripts/spree/backend.js +++ b/backend/app/assets/javascripts/spree/backend.js @@ -31,6 +31,7 @@ //= require spree/backend/components/editable_table //= require spree/backend/components/selectable_table //= require spree/backend/components/selectable_table/summary +//= require spree/backend/components/selectable_table/return_items //= require spree/backend/components/sortable_table //= require spree/backend/components/admin_nav //= require spree/backend/datepicker @@ -50,7 +51,6 @@ //= require spree/backend/progress //= require spree/backend/promotions //= require spree/backend/promotions/activation -//= require spree/backend/returns/return_item_selection //= require spree/backend/routes //= require spree/backend/shipments //= require spree/backend/spree-select2 diff --git a/backend/app/assets/javascripts/spree/backend/components/selectable_table/return_items.js b/backend/app/assets/javascripts/spree/backend/components/selectable_table/return_items.js new file mode 100644 index 00000000000..424c945df20 --- /dev/null +++ b/backend/app/assets/javascripts/spree/backend/components/selectable_table/return_items.js @@ -0,0 +1,5 @@ +Backbone.on('selectableTable:init', function(selectableTable){ + if(selectableTable.$el.hasClass('return-items-table')) { + new Spree.Views.Tables.ReturnItems({el: selectableTable.el, model: selectableTable.model}); + } +}) diff --git a/backend/app/assets/javascripts/spree/backend/returns/return_item_selection.js b/backend/app/assets/javascripts/spree/backend/returns/return_item_selection.js deleted file mode 100644 index 6e4d7ee8a6b..00000000000 --- a/backend/app/assets/javascripts/spree/backend/returns/return_item_selection.js +++ /dev/null @@ -1,35 +0,0 @@ -Spree.ready(function() { - function checkAddItemBox() { - $(this).closest('tr').find('input.add-item').attr('checked', 'checked'); - updateSuggestedAmount(); - } - - function updateSuggestedAmount() { - var totalPretaxRefund = 0; - var checkedItems = formFields.find('input.add-item:checked'); - $.each(checkedItems, function(i, checkbox) { - totalPretaxRefund += parseFloat($(checkbox).parents('tr').find('.refund-amount-input').val()); - }); - - var displayTotal = isNaN(totalPretaxRefund) ? '' : totalPretaxRefund.toFixed(2); - formFields.find('span#total_pre_tax_refund').html(displayTotal); - } - - var formFields = $("[data-hook='admin_customer_return_form_fields'], \ - [data-hook='admin_return_authorization_form_fields']"); - - if(formFields.length > 0) { - updateSuggestedAmount(); - - formFields.find('input#select-all').on('change', function(ev) { - var checkBoxes = $(ev.currentTarget).parents('table:first').find('input.add-item'); - checkBoxes.prop('checked', this.checked); - updateSuggestedAmount(); - }); - - formFields.find('input.add-item').on('change', updateSuggestedAmount); - formFields.find('.refund-amount-input').on('keyup', updateSuggestedAmount); - - formFields.find('input, select').not('.add-item').on('change', checkAddItemBox); - } -}); diff --git a/backend/app/assets/javascripts/spree/backend/templates/index.js b/backend/app/assets/javascripts/spree/backend/templates/index.js index b5a77abfcc6..39d4a178d98 100644 --- a/backend/app/assets/javascripts/spree/backend/templates/index.js +++ b/backend/app/assets/javascripts/spree/backend/templates/index.js @@ -1,5 +1,6 @@ //= require spree/backend/templates/_image //= require spree/backend/templates/tables/selectable_label +//= require spree/backend/templates/tables/return_item_sum_amount //= require spree/backend/templates/orders/customer_details/autocomplete //= require spree/backend/templates/orders/details_adjustment_row //= require spree/backend/templates/orders/line_item diff --git a/backend/app/assets/javascripts/spree/backend/templates/tables/return_item_sum_amount.hbs b/backend/app/assets/javascripts/spree/backend/templates/tables/return_item_sum_amount.hbs new file mode 100644 index 00000000000..7680008cfc2 --- /dev/null +++ b/backend/app/assets/javascripts/spree/backend/templates/tables/return_item_sum_amount.hbs @@ -0,0 +1,7 @@ + + + + + {{total_pre_tax_refund}} {{total_selected_item_amount}} + + diff --git a/backend/app/assets/javascripts/spree/backend/views/index.js b/backend/app/assets/javascripts/spree/backend/views/index.js index 9523a66ec07..670ce59032b 100644 --- a/backend/app/assets/javascripts/spree/backend/views/index.js +++ b/backend/app/assets/javascripts/spree/backend/views/index.js @@ -24,5 +24,7 @@ //= require 'spree/backend/views/promotions/option_values_rule' //= require 'spree/backend/views/tables/editable_table' //= require 'spree/backend/views/tables/editable_table_row' +//= require 'spree/backend/views/tables/return_items' //= require 'spree/backend/views/tables/selectable_table' //= require 'spree/backend/views/tables/selectable_table/summary' +//= require 'spree/backend/views/tables/selectable_table/sum_return_item_amount' diff --git a/backend/app/assets/javascripts/spree/backend/views/tables/return_items.js b/backend/app/assets/javascripts/spree/backend/views/tables/return_items.js new file mode 100644 index 00000000000..611cb21d3f5 --- /dev/null +++ b/backend/app/assets/javascripts/spree/backend/views/tables/return_items.js @@ -0,0 +1,9 @@ +Spree.Views.Tables.ReturnItems = Backbone.View.extend({ + initialize: function() { + if(this.$el.hasClass('return-items-table')) { + var tfoot = document.createElement('tfoot') + new Spree.Views.Tables.SelectableTable.SumReturnItemAmount({el: tfoot, model: this.model}); + this.$el.append(tfoot); + } + }, +}) diff --git a/backend/app/assets/javascripts/spree/backend/views/tables/selectable_table/sum_return_item_amount.js b/backend/app/assets/javascripts/spree/backend/views/tables/selectable_table/sum_return_item_amount.js new file mode 100644 index 00000000000..39d632da355 --- /dev/null +++ b/backend/app/assets/javascripts/spree/backend/views/tables/selectable_table/sum_return_item_amount.js @@ -0,0 +1,32 @@ +Spree.Views.Tables.SelectableTable.SumReturnItemAmount = Backbone.View.extend({ + initialize: function(options) { + this.listenTo(this.model, 'change', this.render); + + this.render(); + }, + + render: function() { + var html = HandlebarsTemplates['tables/return_item_sum_amount']({ + total_pre_tax_refund: Spree.t("total_pre_tax_refund"), + total_selected_item_amount: this.totalSelectedReturnItemAmount() + }); + + this.$el.html(html); + }, + + totalSelectedReturnItemAmount: function () { + var totalAmount = 0.00; + var selectedItems = []; + + if(this.model.get('allSelected')) { + selectedItems = $('.selectable'); + } else { + selectedItems = $(this.model.attributes.selectedItems); + } + selectedItems.each(function(_, selectedItem){ + totalAmount += $(selectedItem).data('price'); + }) + + return totalAmount; + }, +}); diff --git a/backend/app/views/spree/admin/customer_returns/_return_item_selection.html.erb b/backend/app/views/spree/admin/customer_returns/_return_item_selection.html.erb index 903db1a204d..07005fe5972 100644 --- a/backend/app/views/spree/admin/customer_returns/_return_item_selection.html.erb +++ b/backend/app/views/spree/admin/customer_returns/_return_item_selection.html.erb @@ -1,8 +1,7 @@ - +
@@ -25,7 +24,7 @@ <%= item_fields.hidden_field :return_authorization_id %> <%= item_fields.hidden_field :amount %> - <%= item_fields.check_box :returned, {checked: false, class: 'add-item', "data-price" => return_item.amount}, '1', '0' %> + <%= item_fields.check_box :returned, {checked: false, class: 'selectable add-item', "data-price" => return_item.amount}, '1', '0' %>
- <%= check_box_tag 'select-all' %> <%= Spree::Product.model_name.human %> <%= Spree::Variant.human_attribute_name(:sku) %>
<%= return_item.inventory_unit.variant.name %>
diff --git a/backend/app/views/spree/admin/return_authorizations/_form.html.erb b/backend/app/views/spree/admin/return_authorizations/_form.html.erb index ebd040f2528..dca7ab2ba7a 100644 --- a/backend/app/views/spree/admin/return_authorizations/_form.html.erb +++ b/backend/app/views/spree/admin/return_authorizations/_form.html.erb @@ -1,13 +1,10 @@ <% allow_return_item_changes = !@return_authorization.customer_returned_items? %>
- +
- @@ -27,7 +24,7 @@
- <% if allow_return_item_changes %> - <%= check_box_tag 'select-all' %> - <% end %> + <%= Spree::Product.model_name.human %> <%= Spree::ReturnAuthorization.human_attribute_name(:state) %> <% if editable %> <%= item_fields.hidden_field :inventory_unit_id %> - <%= item_fields.check_box :_destroy, {checked: return_item.persisted?, class: 'add-item', "data-price" => return_item.amount}, '0', '1' %> + <%= item_fields.check_box :_destroy, {checked: return_item.persisted?, class: 'selectable add-item', "data-price" => return_item.amount}, '0', '1' %> <% end %> @@ -75,10 +72,6 @@
- <%= f.field_container :amount do %> - <%= t('spree.total_pre_tax_refund') %>: 0.00 - <% end %> - <%= f.field_container :stock_location do %> <%= f.label :stock_location_id, Spree::StockLocation.model_name.human %> <%= f.select :stock_location_id, @stock_locations.to_a.collect{|l|[l.name, l.id]}, {include_blank: true}, {class: 'custom-select fullwidth', "data-placeholder" => t('spree.select_a_stock_location')} %> diff --git a/backend/spec/features/admin/orders/return_authorizations_spec.rb b/backend/spec/features/admin/orders/return_authorizations_spec.rb index 4441529522a..0a53182e74c 100644 --- a/backend/spec/features/admin/orders/return_authorizations_spec.rb +++ b/backend/spec/features/admin/orders/return_authorizations_spec.rb @@ -38,7 +38,7 @@ def create_return_authorization visit spree.new_admin_order_return_authorization_path(order) end - it "creates a return authorization" do + it "creates a return authorization", :js do create_return_authorization expect(page).to have_content "Return Authorization has been successfully created!"