Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Improve cart view, user React for quantity select

Paweł Kowalewski47f15b32672d
diff --git a/Gruntfile.js b/Gruntfile.js
index a96a184e06..c78a276945 100644
--- a/Gruntfile.js
+++ b/Gruntfile.js
@@ -1,5 +1,12 @@
module.exports = function(grunt) {
grunt.initConfig({
+ babel: {
+ dist: {
+ files: {
+ 'saleor/static/js/storefront_react.js': 'saleor/static/js_src/storefront_react.js'
+ }
+ }
+ },
browserSync: {
dev: {
bsFiles: {
@@ -122,7 +129,11 @@ module.exports = function(grunt) {
],
"saleor/static/js/storefront.js": [
"saleor/static/components/jquery/dist/jquery.js",
- "saleor/static/components/bootstrap-sass/assets/javascripts/bootstrap.js"
+ "saleor/static/components/react/react.js",
+ "saleor/static/components/bootstrap-sass/assets/javascripts/bootstrap.js",
+ "saleor/static/components/underscore/underscore.js",
+ "saleor/static/js_src/storefront.js",
+ "saleor/static/js/storefront_react.js"
]
}
}
@@ -140,14 +151,14 @@ module.exports = function(grunt) {
},
uglify: {
files: ["saleor/static/js_src/**/*.js"],
- tasks: ["uglify"]
+ tasks: ["babel", "uglify"]
}
}
});
require("load-grunt-tasks")(grunt);
- grunt.registerTask("default", ["copy", "sass", "postcss", "uglify"]);
+ grunt.registerTask("default", ["copy", "sass", "postcss", "babel", "uglify"]);
grunt.registerTask("sync", ["browserSync", "watch"]);
- grunt.registerTask("heroku", ["copy", "sass", "postcss", "uglify"]);
+ grunt.registerTask("heroku", ["copy", "sass", "postcss", "babel", "uglify"]);
};
diff --git a/bower.json b/bower.json
index a56cabb82e..aac95bfbd1 100644
--- a/bower.json
+++ b/bower.json
@@ -17,6 +17,8 @@
"materialize": "~0.97.0",
"modernizr": "~2.8.3",
"select2": "~4.0.0",
- "Sortable": "~1.2.0"
+ "Sortable": "~1.2.0",
+ "react": "~0.13.3",
+ "underscore": "~1.8.3"
}
}
diff --git a/package.json b/package.json
index 6fbb102d64..898653a985 100644
--- a/package.json
+++ b/package.json
@@ -28,6 +28,7 @@
"grunt-contrib-watch": "~0.6.1",
"grunt-postcss": "~0.4.0",
"grunt-sass": "~1.0.0",
- "load-grunt-tasks": "~3.2.0"
+ "load-grunt-tasks": "~3.2.0",
+ "grunt-babel": "~5.0.1"
}
}
diff --git a/saleor/cart/static/js/delete_buttons.js b/saleor/cart/static/js/delete_buttons.js
deleted file mode 100644
index 78b7b035fa..0000000000
--- a/saleor/cart/static/js/delete_buttons.js
+++ /dev/null
@@ -1,12 +0,0 @@
-$.fn.addDeleteButtons = function() {
- this.find('input[type="number"]').each(function(){
- var icon = $('<i>').addClass('glyphicon glyphicon-trash');
- var button = $('<button>').addClass('btn btn-danger delete-button').html(icon);
- var container = $('<div>').addClass('input-group-btn').html(button);
- $(this).after(container);
- $(this).parent().addClass('input-group');
- });
- this.find('.delete-button').click(function(){
- $(this).parents('tr').find('input[type="number"]').val('0');
- });
-};
diff --git a/saleor/cart/templates/cart/index.html b/saleor/cart/templates/cart/index.html
index 8636daa9ba..f48a23bb5e 100644
--- a/saleor/cart/templates/cart/index.html
+++ b/saleor/cart/templates/cart/index.html
@@ -16,43 +16,52 @@
<form role="form" action="." method="post">
{% csrf_token %}
{{ formset.management_form }}
- <table class="table table-striped" id="cart-items">
+ <table class="table table-striped cart" id="cart-items">
<thead>
<tr>
- <th class="col-sm-4">{% trans "Product" %}</th>
- <th class="col-sm-2">{% trans "Quantity" %}</th>
- <th class="col-sm-2">{% trans "Price" %}</th>
- <th class="col-sm-2">{% trans "Subtotal" %}</th>
+ <th class="col-sm-7">{% trans "Product" %}</th>
+ <th class="col-sm-1 text-right">{% trans "Quantity" %}</th>
+ <th class="col-sm-2 text-right">{% trans "Price" %}</th>
+ <th class="col-sm-2 text-right">{% trans "Subtotal" %}</th>
</tr>
</thead>
<tfoot>
<tr>
- <td class="col-sm-4"></td>
- <td class="col-sm-2">
- <button class="btn btn-default" type="submit">
- {% trans "Update quantities" %}
- </button>
- </td>
- <th class="col-sm-2">{% trans "Total" %}:</th>
- <td class="col-sm-2">{% gross cart.get_total %}</td>
+ <td class="col-sm-10 text-right lead" colspan="3">{% trans "Total" %}:
+ <td class="col-sm-2 text-right lead"><b>{% gross cart.get_total %}</b></td>
</tr>
</tfoot>
<tbody>
{% for form in formset %}
- <tr>
+ <tr class="cart-item">
<td>
- <a href="{{ form.cart_line.product.get_absolute_url }}">{{ form.cart_line.product.product }} - {{ form.cart_line.product.display_variant }}</a>
+ <a href="{{ form.cart_line.product.get_absolute_url }}">
+ {% if form.cart_line.product.product.images.first %}
+ <img class="cart-item-image" itemprop="image" src="{{ form.cart_line.product.product.images.first.image.crop.100x100 }}" alt="">
+ {% else %}
+ <img class="cart-item-image" src="{% static 'images/product-image-placeholder.png' %}" alt="">
+ {% endif %}
+ {{ form.cart_line.product.product }} - {{ form.cart_line.product.display_variant }}
+ </a>
+ </td>
+ <td class="cart-item-quantity">
+ {{ form.quantity|as_vertical_form:False }}
+ <button type="submit" class="btn btn-link btn-sm">{% trans "Update" %}</button>
+ </td>
+ <td class="text-right">{% gross form.cart_line.get_price_per_item %}</td>
+ <td class="text-right cart-item-subtotal">
+ {% gross form.cart_line.get_total %}
+ <button type="submit" class="btn btn-link btn-sm cart-item-remove">
+ <span class="text-muted">{% trans "Remove from cart" %}</span>
+ </button>
</td>
- <td>{{ form.quantity|as_vertical_form:False }}</td>
- <td>{% gross form.cart_line.get_price_per_item %}</td>
- <td>{% gross form.cart_line.get_total %}</td>
</tr>
{% endfor %}
</tbody>
</table>
<div class="form-group">
<div>
- <a href="{% url "checkout:index" %}" class="btn btn-primary">Checkout</a>
+ <a href="{% url "checkout:index" %}" class="btn btn-primary btn-lg pull-right">{% trans "Proceed to checkout" %}</a>
</div>
</div>
</form>
@@ -62,11 +71,3 @@
</p>
{% endif %}
{% endblock content %}
-
-{% block footer_scripts %}
- {{ block.super }}
- <script type="text/javascript" src="{% static "js/delete_buttons.js" %}"></script>
- <script>
- $('#cart-items').addDeleteButtons();
- </script>
-{% endblock footer_scripts %}
\ No newline at end of file
diff --git a/saleor/static/js_src/storefront_react.js b/saleor/static/js_src/storefront_react.js
new file mode 100644
index 0000000000..abf29aa3a3
--- /dev/null
+++ b/saleor/static/js_src/storefront_react.js
@@ -0,0 +1,49 @@
+var CartItemAmount = React.createClass({
+ getInitialState: function() {
+ return {
+ value: this.props.value
+ }
+ },
+ change: function(event){
+ if (event.target.value == this.lastOptionValue) {
+ var parent = this.getDOMNode().parentNode;
+ console.log(parent.innerHTML);
+ React.unmountComponentAtNode(parent);
+ parent.appendChild(inputs[this.props.name]);
+ } else {
+ this.setState({value: event.target.value});
+ }
+ },
+ render: function() {
+ this.lastOptionValue = _.clone(this.props.options).pop() + 1;
+ this.lastOptionLabel = this.lastOptionValue + " +";
+ return <select name={this.props.name} onChange={this.change} value={this.state.value} className="form-control">
+ {this.props.options.map(function(option) {
+ return <CartItemAmountOption key={option} value={option} />
+ })}
+ <CartItemAmountLastOption value={this.lastOptionValue} label={this.lastOptionLabel} />
+ </select>;
+ }
+});
+
+class CartItemAmountOption extends React.Component {
+ render() {
+ return <option value={this.props.value}>{this.props.value}</option>;
+ };
+}
+
+class CartItemAmountLastOption extends React.Component {
+ render() {
+ return <option value={this.props.value}>{this.props.label}</option>;
+ };
+}
+
+var inputs = [];
+$(".cart-item-quantity").each(function() {
+ var input = $(this).find("input");
+ var value = input.val();
+ var name = input.attr("name");
+ inputs[name] = input[0];
+
+ React.render(<CartItemAmount options={_.range(1, 10)} value={value} name={name} />, this);
+});
diff --git a/saleor/static/scss/_app.scss b/saleor/static/scss/_app.scss
index de16b886ba..7021540619 100644
--- a/saleor/static/scss/_app.scss
+++ b/saleor/static/scss/_app.scss
@@ -52,9 +52,35 @@ $icon-font-path: "../fonts/";
}
.breadcrumb {
- > li+li {
- &:before {
- content: '/';
+ > li+li {
+ &:before {
+ content: '/';
+ }
+ }
+}
+
+.cart {
+ > tbody {
+ > .cart-item {
+ > td {
+ vertical-align: text-bottom;
+ }
+ }
+ }
+
+ &-item {
+ &-image {
+ width: 100px;
+ }
+
+ &-subtotal {
+ position: relative;
+ }
+
+ &-remove {
+ position: absolute;
+ bottom: 0;
+ right: 0;
+ }
}
- }
}