Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added empty cart button in admin cart #3316

Merged
merged 1 commit into from
Aug 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions backend/app/assets/javascripts/spree/backend/models/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ Spree.Models.Order = Backbone.Model.extend({
};
_.extend(options, opts);
return this.fetch(options)
},

empty: function (opts) {
var options = {
url: Spree.routes.orders_api + "/" + this.id + "/empty",
type: 'PUT',
};
_.extend(options, opts);
return this.fetch(options)
}
});

Expand Down
6 changes: 6 additions & 0 deletions backend/app/assets/javascripts/spree/backend/orders/cart.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ Spree.Order.initCartPage = function(order_number) {
collection: collection
});

new Spree.Views.Cart.EmptyCartButton({
el: $('.js-empty-cart'),
collection: collection,
model: order
});

new Spree.Views.Order.DetailsTotal({
el: $('#order-total'),
model: order
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
Spree.Views.Cart.EmptyCartButton = Backbone.View.extend({
initialize: function() {
this.listenTo(this.collection, 'update', this.render);
this.render();
},

events: {
"click": "onClick"
},

onClick: function(e) {
e.preventDefault()
if (!confirm(Spree.translations.are_you_sure_delete)) {
return;
}

this.model.empty({
success: () => {
Copy link
Contributor

@pelargir pelargir Feb 11, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Due to the ES6 arrow syntax on this one line, Uglifier has to be put in harmony mode to run Solidus 2.10. This should probably be changed back to ES5 syntax for better compatibility. We shouldn't have to run Uglifier in harmony mode just to get this one line to minify properly.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, Matt! We would need to either process this file with Babel or update the syntax. I vote for the latter.

@pelargir want to send a PR?

this.collection.reset()
this.collection.push({})
}
})
},

render: function() {
var isNew = function (item) { return item.isNew() };
this.$el.prop("disabled", !this.collection.length || this.collection.some(isNew));
}
});
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
Spree.Views.Cart.LineItemTable = Backbone.View.extend({
initialize: function() {
this.listenTo(this.collection, 'add', this.add);
this.listenTo(this.collection, 'reset', this.reset);
},

add: function(line_item) {
var view = new Spree.Views.Cart.LineItemRow({model: line_item});
view.render();
this.$el.append(view.el);
},

reset: function() {
this.$el.empty();
}
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//= require 'spree/backend/views/calculators/tiered'
//= require 'spree/backend/views/cart/add_line_item_button'
//= require 'spree/backend/views/cart/empty_cart_button'
//= require 'spree/backend/views/cart/line_item_row'
//= require 'spree/backend/views/cart/line_item_table'
//= require 'spree/backend/views/images/upload_zone'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

<% if can?(:update, @order) && can?(:create, Spree::LineItem) %>
<button class="js-add-line-item btn btn-primary" disabled><%= t('spree.add_line_item') %></button>
<button class="js-empty-cart btn btn-primary" disabled><%= t('spree.empty_cart') %></button>
<% end %>

<%= render "spree/admin/orders/order_details", { order: order } %>
Expand Down
13 changes: 13 additions & 0 deletions backend/spec/features/admin/orders/order_details_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,19 @@
expect(page).to have_field('quantity')
end

it "can remove all items with empty cart" do
expect(page).to have_content("spree t-shirt")

accept_confirm "Are you sure you want to delete this record?" do
click_on 'Empty Cart'
end

expect(page).not_to have_content("spree t-shirt")

# Should have a new item row
expect(page).to have_field('quantity')
end

# Regression test for https://github.com/spree/spree/issues/3862
it "can cancel removing an item from a shipment" do
expect(page).to have_content("spree t-shirt")
Expand Down