From 6a507d1f373af1c2cb55262aa2d706b59702e9ef Mon Sep 17 00:00:00 2001 From: Daniele Palombo Date: Fri, 12 Jun 2020 13:20:51 +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 | 1 - .../backend/components/selectable_table.js | 6 ++++ .../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 | 1 + ...selectable_table_sum_return_item_amount.js | 32 +++++++++++++++++ .../_return_item_selection.html.erb | 5 ++- .../return_authorizations/_form.html.erb | 13 ++----- .../orders/return_authorizations_spec.rb | 2 +- 10 files changed, 53 insertions(+), 50 deletions(-) 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/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 a653a86ae18..5da1258695e 100644 --- a/backend/app/assets/javascripts/spree/backend.js +++ b/backend/app/assets/javascripts/spree/backend.js @@ -49,7 +49,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.js b/backend/app/assets/javascripts/spree/backend/components/selectable_table.js index dca2a01c603..789925b0479 100644 --- a/backend/app/assets/javascripts/spree/backend/components/selectable_table.js +++ b/backend/app/assets/javascripts/spree/backend/components/selectable_table.js @@ -6,5 +6,11 @@ Spree.ready(function() { }) new Spree.Views.Tables.SelectableTable({el: this, model: selectableTableModel}); + + if($(this).hasClass('return-items-table')) { + var tfoot = document.createElement('tfoot') + new Spree.Views.Tables.SelectableTable.SumReturnItemAmount({ el: tfoot, model: selectableTableModel}); + $(this).append(tfoot); + } }) }); 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 4b4f5f6ab43..dec213af48c 100644 --- a/backend/app/assets/javascripts/spree/backend/views/index.js +++ b/backend/app/assets/javascripts/spree/backend/views/index.js @@ -26,3 +26,4 @@ //= require 'spree/backend/views/tables/editable_table_row' //= 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/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!"