Skip to content

Commit

Permalink
Use selectable.js code on return items.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
DanielePalombo committed Jun 19, 2020
1 parent c12cdce commit 6a507d1
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 50 deletions.
1 change: 0 additions & 1 deletion backend/app/assets/javascripts/spree/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
})
});

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<tr class="selectable-header">
<th class="selectable-table-summary" colspan="2">
</th>
<td colspan="7" class="text-right">
{{total_pre_tax_refund}} <span id="total_pre_tax_refund">{{total_selected_item_amount}}</span>
</td>
</tr>
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Original file line number Diff line number Diff line change
@@ -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;
},
});
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<table class="index return-items-table">
<table class="index return-items-table selectable-table">
<thead>
<tr>
<th>
<%= check_box_tag 'select-all' %>
</th>
<th><%= Spree::Product.model_name.human %></th>
<th><%= Spree::Variant.human_attribute_name(:sku) %></th>
Expand All @@ -25,7 +24,7 @@
<%= item_fields.hidden_field :return_authorization_id %>
<%= item_fields.hidden_field :amount %>
</div>
<%= 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' %>
</td>
<td>
<div class="variant-name"><%= return_item.inventory_unit.variant.name %></div>
Expand Down
13 changes: 3 additions & 10 deletions backend/app/views/spree/admin/return_authorizations/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
<% allow_return_item_changes = !@return_authorization.customer_returned_items? %>

<div data-hook="admin_return_authorization_form_fields">
<table class="index return-items-table">
<table class="index return-items-table selectable-table">
<thead>
<tr data-hook="rma_header">
<th class="return-item-checkbox">
<% if allow_return_item_changes %>
<%= check_box_tag 'select-all' %>
<% end %>
<th>
</th>
<th class="return-item-product"><%= Spree::Product.model_name.human %></th>
<th class="return-item-state"><%= Spree::ReturnAuthorization.human_attribute_name(:state) %></th>
Expand All @@ -27,7 +24,7 @@
<td class="return-item-checkbox inventory-unit-checkbox">
<% 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 %>
</td>
<td class="return-item-product">
Expand Down Expand Up @@ -75,10 +72,6 @@
</tbody>
</table>

<%= f.field_container :amount do %>
<%= t('spree.total_pre_tax_refund') %>: <span id="total_pre_tax_refund">0.00</span>
<% 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')} %>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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!"
Expand Down

0 comments on commit 6a507d1

Please sign in to comment.