Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Use new Classiffications API in Admin Panel
Damian Legawiec84eb3373867d
diff --git a/backend/app/assets/javascripts/spree/backend.js b/backend/app/assets/javascripts/spree/backend.jsindex ee7140d806..92100108b5 100644--- a/backend/app/assets/javascripts/spree/backend.js+++ b/backend/app/assets/javascripts/spree/backend.js@@ -92,6 +92,7 @@ Spree.routes.stock_items_api = function (stockLocationId) {// API v2Spree.routes.countries_api_v2 = Spree.pathFor('api/v2/platform/countries')+Spree.routes.classifications_api_v2 = Spree.pathFor('api/v2/platform/classifications')Spree.routes.menus_api_v2 = Spree.pathFor('api/v2/platform/menus')Spree.routes.menus_items_api_v2 = Spree.pathFor('api/v2/platform/menu_items')Spree.routes.option_types_api_v2 = Spree.pathFor('api/v2/platform/option_types')diff --git a/backend/app/assets/javascripts/spree/backend/taxons.js b/backend/app/assets/javascripts/spree/backend/taxons.jsindex d973e46388..c3d99051aa 100644--- a/backend/app/assets/javascripts/spree/backend/taxons.js+++ b/backend/app/assets/javascripts/spree/backend/taxons.js@@ -14,17 +14,17 @@ $(function () {swapThreshold: 0.9,forceFallback: true,onEnd: function (evt) {- var itemEl = evt.item.getAttribute('data-product-id')- var newin = evt.newIndex+ var classificationId = evt.item.getAttribute('data-classification-id')+ var newIndex = evt.newIndexreturn $.ajax({- url: Spree.routes.classifications_api,+ url: Spree.routes.classifications_api_v2 + '/' + classificationId.toString() + '/reposition',+ headers: Spree.apiV2Authentication(),method: 'PUT',dataType: 'json',data: {- token: Spree.api_key,- product_id: itemEl,- taxon_id: $('#taxon_id').val(),- position: newin+ classification: {+ position: newIndex+ }}})}@@ -37,22 +37,27 @@ $(function () {minimumInputLength: 3,multiple: false,ajax: {- url: Spree.routes.taxons_api,+ url: Spree.routes.taxons_api_v2,datatype: 'json',+ headers: Spree.apiV2Authentication(),data: function (params, page) {return {per_page: 50,page: page,- without_children: true,- q: {+ filter: {name_cont: params.term- },- token: Spree.api_key+ }}},processResults: function (data, page) {- var more = page < data.pages- var results = formatTaxonList(data.taxons)+ var more = page < data.meta.total_pages++ var results = data.data.map(function (obj) {+ return {+ id: obj.id,+ text: obj.attributes.pretty_name+ }+ })return {results: results,@@ -61,40 +66,61 @@ $(function () {}}}- },- templateResult: formatTaxon,- templateSelection: formatTaxon+ }}).on('select2:select', function (e) {- var el = $('#taxon_products')$.ajax({- url: Spree.routes.taxon_products_api,- data: { id: e.params.data.id, token: Spree.api_key }- }).done(function (data) {- var i, j, len, len1, product, ref, ref1, results, variant- el.empty()- if (data.products.length === 0) {- return $('#taxon_products').html('<div class="alert alert-info">' + Spree.translations.no_results + '</div>')+ url: Spree.routes.classifications_api_v2,+ headers: Spree.apiV2Authentication(),+ data: {+ filter: {+ taxon_id_eq: e.params.data.id+ },+ include: 'product.images',+ per_page: 150,+ sort: 'position'+ }+ }).done(function (json) {+ taxonProducts.empty()++ if (json.meta.total_count === 0) {+ return taxonProducts.html('<p class="text-center w-100 p-4">' + Spree.translations.no_results + '</p>')} else {- ref = data.products- results = []- for (i = 0, len = ref.length; i < len; i++) {- product = ref[i]- if (product.master.images[0] !== void 0 && product.master.images[0].small_url !== void 0) {- product.image = product.master.images[0].small_url- } else {- ref1 = product.variants- for (j = 0, len1 = ref1.length; j < len1; j++) {- variant = ref1[j]- if (variant.images[0] !== void 0 && variant.images[0].small_url !== void 0) {- product.image = variant.images[0].small_url- break+ var results = []++ json.data.forEach(function (classification) {+ var productId = classification.relationships.product.data.id.toString()++ var product = json.included.find(function(included) {+ if (included.type === 'product' && included.id === productId) {+ return included+ }+ })++ if (product && classification) {+ var imageUrl = null++ if (product.relationships.images.data.length > 0) {+ var imageId = product.relationships.images.data[0].id++ var image = json.included.find(function(included) {+ if (included.type === 'image' && included.id === imageId) {+ return included+ }+ })++ if (image && image.attributes && image.attributes.styles) {+ imageUrl = image.attributes.styles[2].url}}++ results.push(taxonProducts.append(productTemplate({+ product: product,+ classification: classification,+ image: imageUrl+ })))}- results.push(el.append(productTemplate({- product: product- })))- }+ })+return results}})@@ -102,43 +128,18 @@ $(function () {}taxonProducts.on('click', '.js-delete-product', function (e) {- var currentTaxonId = $('#taxon_id').val()var product = $(this).parents('.product')- var productId = product.data('product-id')- var productTaxons = String(product.data('taxons')).split(',').map(Number)- var productIndex = productTaxons.indexOf(parseFloat(currentTaxonId))- productTaxons.splice(productIndex, 1)- var taxonIds = productTaxons.length > 0 ? productTaxons : ['']+ var classificationId = product.data('classification-id')$.ajax({- url: Spree.routes.products_api + '/' + productId,- data: {- product: {- taxon_ids: taxonIds- },- token: Spree.api_key- },- type: 'PUT'+ url: Spree.routes.classifications_api_v2 + '/' + classificationId.toString(),+ headers: Spree.apiV2Authentication(),+ type: 'DELETE'}).done(function () {product.fadeOut(400, function (e) {product.remove()})})})- $('.variant_autocomplete').variantAutocomplete()- function formatTaxon (taxon) {- if (taxon.loading) {- return taxon.text;- }- return taxon.pretty_name- }-- function formatTaxonList(values) {- return values.map(function (obj) {- return {- id: obj.id,- pretty_name: obj.pretty_name- }- })- }+ $('.variant_autocomplete').variantAutocomplete()})diff --git a/backend/app/views/spree/admin/products/_autocomplete.js.erb b/backend/app/views/spree/admin/products/_autocomplete.js.erbindex 86b12ba7d6..c286988cf0 100644--- a/backend/app/views/spree/admin/products/_autocomplete.js.erb+++ b/backend/app/views/spree/admin/products/_autocomplete.js.erb@@ -1,5 +1,5 @@<script type='text/template' id='product_template'>- <li id="product_{{ product.id }}" data-product-id='{{ product.id }}' data-taxons="{{ product.taxon_ids }}" class="product p-2 col-6 col-md-3 col-lg-2">+ <li id="product_{{ product.id }}" data-classification-id='{{ classification.id }}' data-product-id='{{ product.id }}' class="product p-2 col-6 col-md-3 col-lg-2"><div class="m-1 p-2 card bg-light"><nav class="navbar navbar-light bg-light justify-content-between px-0 pt-0" style="background-color:transparent !important;"><a class=" sort-handle js-sort-handle">@@ -16,15 +16,15 @@</div></nav><div class='image'>- {{#if product.image }}- <img src='{{product.image}}' alt='{{ product.name }}' class='thumbnail rounded border border' />+ {{#if image }}+ <img src='{{image}}' alt='{{ product.attributes.name }}' class='thumbnail rounded border border' />{{ else }}<img src='<%= image_path("noimage/small.png") %>' alt='{{product.name }}' class='thumbnail' />{{/if}}</div><div class="p-0 pt-1 text-center product-info">- <p class="name card-text">{{ product.name }}</p>- <p class="price card-text">{{ product.display_current_currency_price }}</p>+ <p class="name card-text">{{ product.attributes.name }}</p>+ <p class="price card-text">{{ product.attributes.display_price }}</p></div></div></li>diff --git a/backend/spec/features/admin/taxons_spec.rb b/backend/spec/features/admin/taxons_spec.rbindex cefb13ab52..b2eed30d4d 100644--- a/backend/spec/features/admin/taxons_spec.rb+++ b/backend/spec/features/admin/taxons_spec.rb@@ -7,7 +7,7 @@ describe 'Taxonomies and taxons', type: :feature, js: true dolet(:file_path) { Rails.root + '../../spec/support/ror_ringer.jpeg' }it 'admin should be able to edit taxon' do- visit spree.edit_admin_taxonomy_taxon_path(taxonomy, taxonomy.root.id)+ visit spree.edit_admin_taxonomy_taxon_path(taxonomy, taxonomy.root_id)fill_in 'taxon_name', with: 'Shirt'fill_in 'taxon_description', with: 'Discover our new rails shirts'@@ -18,7 +18,7 @@ describe 'Taxonomies and taxons', type: :feature, js: true doendit 'taxon without name should not be updated' do- visit spree.edit_admin_taxonomy_taxon_path(taxonomy, taxonomy.root.id)+ visit spree.edit_admin_taxonomy_taxon_path(taxonomy, taxonomy.root_id)fill_in 'taxon_name', with: ''fill_in 'taxon_description', with: 'Discover our new rails shirts'@@ -34,7 +34,7 @@ describe 'Taxonomies and taxons', type: :feature, js: true doproduct.taxons << taxon_1visit spree.admin_taxons_path- select_clothing_from_select2+ select_taxon_from_select2(taxon_1)find('.product').hoverfind('.product .dropdown-toggle').click@@ -44,20 +44,20 @@ describe 'Taxonomies and taxons', type: :feature, js: true doexpect(page).not_to have_css('.product')refresh- select_clothing_from_select2+ select_taxon_from_select2(taxon_1)expect(page).to have_content('No results')endit 'admin should be able to add taxon icon' do- visit spree.edit_admin_taxonomy_taxon_path(taxonomy, taxonomy.root.id)+ visit spree.edit_admin_taxonomy_taxon_path(taxonomy, taxonomy.root_id)attach_file('taxon_icon', file_path)click_button 'Update'expect(page).to have_content('successfully updated!')- visit spree.edit_admin_taxonomy_taxon_path(taxonomy, taxonomy.root.id)+ visit spree.edit_admin_taxonomy_taxon_path(taxonomy, taxonomy.root_id)expect(page).to have_css('#taxon_icon_field img')end@@ -65,21 +65,21 @@ describe 'Taxonomies and taxons', type: :feature, js: true doit 'admin should be able to remove taxon icon' doadd_icon_to_root_taxon- visit spree.edit_admin_taxonomy_taxon_path(taxonomy, taxonomy.root.id)+ visit spree.edit_admin_taxonomy_taxon_path(taxonomy, taxonomy.root_id)click_link 'Remove Image'expect(page).to have_content('Image has been successfully removed')end- def select_clothing_from_select2+ def select_taxon_from_select2(taxon)select2_open css: '.taxon-products-view'- select2_search 'Clothing', css: '.taxon-products-view'- select2_select Spree::Product.first.taxons.first&.pretty_name, css: '.taxon-products-view'+ select2_search taxon.name, css: '.taxon-products-view'+ select2_select taxon.pretty_name, css: '.taxon-products-view'enddef add_icon_to_root_taxon- visit spree.edit_admin_taxonomy_taxon_path(taxonomy, taxonomy.root.id)+ visit spree.edit_admin_taxonomy_taxon_path(taxonomy, taxonomy.root_id)attach_file('taxon_icon', file_path)click_button 'Update'end