Skip to content

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

Add AJAX to image reorder

Paweł Kowalewski661880ef8904
diff --git a/saleor/dashboard/templates/dashboard/product/product_form.html b/saleor/dashboard/templates/dashboard/product/product_form.html
index 556ae47fe3..20eaa4f80a 100644
--- a/saleor/dashboard/templates/dashboard/product/product_form.html
+++ b/saleor/dashboard/templates/dashboard/product/product_form.html
@@ -116,7 +116,7 @@
<div class="row" id="images">
<div class="col l12">
- <ul class="row product-gallery" id="product-gallery">
+ <ul class="row product-gallery" id="product-gallery" data-post-url="{{ images_reorder_url }}">
{% for image in images %}
<li class="col s6 m4 product-gallery-item" data-id="{{ image.pk }}">
<img class="responsive-img" src="{{ image.image.crop.300x300 }}" alt="">
diff --git a/saleor/static/js_src/dashboard.js b/saleor/static/js_src/dashboard.js
index 52da249d79..d5fada7158 100644
--- a/saleor/static/js_src/dashboard.js
+++ b/saleor/static/js_src/dashboard.js
@@ -6,5 +6,26 @@ $('ul.tabs').find('.tab').on('click', function(e) {
});
var el = document.getElementById('product-gallery');
var sortable = Sortable.create(el, {
- onUpdate: function(e) {}
+ onUpdate: function() {
+ $.ajax({
+ dataType: 'json',
+ data: {
+ 'data': (function () {
+ var postData = [];
+ $(el).find('.product-gallery-item').each(function (i) {
+ postData.push({
+ pk: $(this).data('id'),
+ order: i
+ });
+ });
+ return JSON.stringify(postData);
+ })()
+ },
+ headers: {
+ 'X-CSRFToken': $('[name=csrfmiddlewaretoken]').val()
+ },
+ method: 'post',
+ url: $(el).data('post-url')
+ });
+ }
});