Skip to content

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

Refactor product image gallery

dominik-zeglen81596333b89e
diff --git a/saleor/static/dashboard/js/components/image-gallery.js b/saleor/static/dashboard/js/components/image-gallery.js
index 6a0e51bbf7..0eeadb8dd2 100644
--- a/saleor/static/dashboard/js/components/image-gallery.js
+++ b/saleor/static/dashboard/js/components/image-gallery.js
@@ -1,57 +1,59 @@
import Dropzone from 'dropzone';
import Sortable from 'sortablejs';
-export default $(document).ready((e) => {
- $('#product-image-form').dropzone({
- paramName: 'image_0',
- maxFilesize: 20,
- previewsContainer: '.product-gallery',
- thumbnailWidth: 400,
- thumbnailHeight: 400,
- previewTemplate: $('#template').html(),
- method: 'POST',
- clickable: '.dropzone-message',
- init: function () {
- this.on('success', function (e, response) {
- $(e.previewElement).find('.product-gallery-item-desc').html(response.image);
- $(e.previewElement).attr('data-id', response.id);
- let editLinkHref = $(e.previewElement).find('.card-action-edit').attr('href');
- editLinkHref = editLinkHref.split('/');
- editLinkHref[editLinkHref.length - 2] = response.id;
- $(e.previewElement).find('.card-action-edit').attr('href', editLinkHref.join('/'));
- $(e.previewElement).find('.card-action-edit').show();
- let deleteLinkHref = $(e.previewElement).find('.card-action-delete').attr('data-href');
- deleteLinkHref = deleteLinkHref.split('/');
- deleteLinkHref[deleteLinkHref.length - 3] = response.id;
- $(e.previewElement).find('.card-action-delete').attr('data-href', deleteLinkHref.join('/'));
- $(e.previewElement).find('.card-action-delete').show();
- $('.no-images').addClass('hide');
- });
- }
- });
- let el = document.getElementById('product-gallery');
- if (el) {
- Sortable.create(el, {
- handle: '.sortable__drag-area',
- onUpdate: function () {
- let orderedImages = (function() {
- let postData = [];
- $(el).find('.product-gallery-item[data-id]').each(function() {
- postData.push($(this).data('id'));
- });
- return postData;
- })();
+const createLink = (link, index, replacement) => {
+ const outputLink = link.attr('data-href-template').split('/');
+ outputLink[outputLink.length + index] = replacement;
+ return outputLink.join('/')
+};
+
+$('#product-image-form').dropzone({
+ paramName: 'image_0',
+ maxFilesize: 20,
+ previewsContainer: '.product-gallery',
+ thumbnailWidth: 400,
+ thumbnailHeight: 400,
+ previewTemplate: $('#template').html(),
+ method: 'POST',
+ clickable: '.dropzone-message',
+ init: function () {
+ this.on('success', (e, response) => {
+ const $previewElement = $(e.previewElement);
+ $previewElement.find('.product-gallery-item-desc').html(response.image);
+ $previewElement.attr('data-id', response.id);
+
+ const $editLinkElement = $previewElement.find('.card-action-edit');
+ const editLinkHref = createLink($editLinkElement, -2, response.id);
+ $editLinkElement.attr('href', editLinkHref);
- $.ajax({
- method: 'POST',
- url: $(el).data('post-url'),
- data: {ordered_images: orderedImages},
- traditional: true,
- headers: {
- 'X-CSRFToken': $('[name=csrfmiddlewaretoken]').val()
- }
- });
- }
+ const $deleteLinkElement = $previewElement.find('.card-action-delete');
+ const deleteLinkHref = createLink($deleteLinkElement, -3, response.id);
+ $deleteLinkElement.attr('data-href', deleteLinkHref);
+
+ $('.no-images').addClass('hide');
});
}
});
+const productGallery = document.getElementById('product-gallery');
+if (productGallery) {
+ Sortable.create(productGallery, {
+ handle: '.sortable__drag-area',
+ onUpdate: () => {
+ const orderedImages = $(productGallery)
+ .find('.product-gallery-item[data-id]')
+ .map((index, item) => item.dataset.id)
+ .toArray();
+
+ // TODO: Get rid of ajax() in favour of fetch()
+ $.ajax({
+ method: 'POST',
+ url: $(productGallery).data('post-url'),
+ data: {ordered_images: orderedImages},
+ traditional: true,
+ headers: {
+ 'X-CSRFToken': $.cookie('csrftoken')
+ }
+ });
+ }
+ });
+}
diff --git a/templates/dashboard/product/product_image/list.html b/templates/dashboard/product/product_image/list.html
index fe02c74d70..2d498aff12 100644
--- a/templates/dashboard/product/product_image/list.html
+++ b/templates/dashboard/product/product_image/list.html
@@ -124,11 +124,11 @@
</div>
<div class="sortable__drag-area"></div>
<div class="card-action">
- <a class="btn-flat waves-effect card-action-edit" href="{% url 'dashboard:product-image-update' product_pk=product.pk img_pk=0 %}">
+ <a class="btn-flat waves-effect card-action-edit" data-href-template="{% url 'dashboard:product-image-update' product_pk=product.pk img_pk=0 %}">
{% trans "Edit" context "Dashboard edit action" %}
</a>
<a href="#base-modal" class="btn-flat waves-effect card-action-delete modal-trigger-custom"
- data-href="{% url 'dashboard:product-image-delete' product_pk=product.pk img_pk=0 %}">
+ data-href-template="{% url 'dashboard:product-image-delete' product_pk=product.pk img_pk=0 %}">
{% trans "Remove" context "Dashboard remove action" %}
</a>
</div>