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
Tidy up JavaScript + allow users to clear selected taxon or product.
Matthew Kennedy784931f8d6de
diff --git a/backend/app/assets/javascripts/spree/backend/global/utils.es6 b/backend/app/assets/javascripts/spree/backend/global/utils.es6index 5138088095..9c7106cbbc 100644--- a/backend/app/assets/javascripts/spree/backend/global/utils.es6+++ b/backend/app/assets/javascripts/spree/backend/global/utils.es6@@ -1,8 +1,9 @@/**- populateSelectOptionsFromApi()+ populateSelectOptionsFromApi(params)Allows you to easily fetch data from API (Platfrom v2)- and populate an empty Select2 with options, including a pre selected option.+ and populate an empty <select> with <option> tags,+ including a selected <option> tag.## EXAMPLE USE CASE called from ERB view file:@@ -16,48 +17,45 @@<% end %>})**/+// eslint-disable-next-line no-unused-varsconst populateSelectOptionsFromApi = function(params) {createRequest(params, updateSelectSuccess, updateSelectError)-};+}++const handleErrors = function(response) {+ if (!response.ok) throw new Error((response.status + ': ' + response.statusText))++ return response.json()+}const createRequest = function(params, succeed, fail) {const targetElement = params.targetElementconst apiUrl = params.apiUrlconst returnAttribute = params.returnAttributeconst selectedOption = params.selectedOption+ const selectEl = document.querySelector(targetElement)fetch(apiUrl, { headers: Spree.apiV2Authentication() }).then((response) => handleErrors(response))- .then((json) => succeed(json.data, returnAttribute, targetElement, selectedOption))- .catch((error) => fail(error, targetElement));-};--const handleErrors = function(response) {- if (!response.ok) {- throw new Error((response.status + ': ' + response.statusText));- }- return response.json();+ .then((json) => succeed(json.data, returnAttribute, selectEl, selectedOption))+ .catch((error) => fail(error, selectEl))}-const updateSelectSuccess = function(parsedData, returnAttribute, targetElement, selectedOption) {- const selectEl = document.querySelector(targetElement)+const updateSelectSuccess = function(parsedData, returnAttribute, selectEl, selectedOption) {+ parsedData.forEach((object) => {+ const optionEl = document.createElement('option')+ optionEl.value = object.id+ optionEl.innerHTML = object.attributes[returnAttribute]- parsedData.forEach((item) => {- const oppt = document.createElement('option');- oppt.value = item.id;- oppt.innerHTML = item.attributes[returnAttribute];+ if (parseInt(selectedOption, 10) === parseInt(object.id, 10)) optionEl.selected = true- if (parseInt(selectedOption, 10) === parseInt(item.id, 10)) {- oppt.selected = true;- }- selectEl.appendChild(oppt)+ selectEl.appendChild(optionEl)})}-const updateSelectError = function(error, targetElement) {- const selectEl = document.querySelector(targetElement)+const updateSelectError = function(error, selectEl) {selectEl.disabled = trueconsole.log(error)-};+}diff --git a/core/app/models/spree/menu_item.rb b/core/app/models/spree/menu_item.rbindex d987740b61..00af1eec60 100644--- a/core/app/models/spree/menu_item.rb+++ b/core/app/models/spree/menu_item.rb@@ -34,17 +34,17 @@ module Spreedef build_pathcase linked_resource_typewhen 'Spree::Taxon'- return if linked_resource.nil?-- self.destination = if frontend_available?+ self.destination = if linked_resource.nil?+ nil+ elsif frontend_available?Spree::Core::Engine.routes.url_helpers.nested_taxons_path(linked_resource.permalink)else"/#{Spree::Config[:storefront_taxon_path]}/#{linked_resource.permalink}"endwhen 'Spree::Product'- return if linked_resource.nil?-- self.destination = if frontend_available?+ self.destination = if linked_resource.nil?+ nil+ elsif frontend_available?Spree::Core::Engine.routes.url_helpers.product_path(linked_resource)else"/#{Spree::Config[:storefront_product_path]}/#{linked_resource.slug}"diff --git a/frontend/app/helpers/spree/navigation_helper.rb b/frontend/app/helpers/spree/navigation_helper.rbindex d5044e3d21..a6296ccc80 100644--- a/frontend/app/helpers/spree/navigation_helper.rb+++ b/frontend/app/helpers/spree/navigation_helper.rb@@ -45,23 +45,23 @@ module Spree# You can use .children to retrieve the top level of menu items,# or .descendants to retrieve all menu items.def root_item_for_menu(unique_code)- if menu(unique_code).present?- menu(unique_code).root- end+ return unless menu(unique_code).present?++ menu(unique_code).rootend# Returns only the top level items for the menu by unique_codedef top_level_items_for_menu(unique_code)- if menu(unique_code).present?- menu(unique_code).root.children- end+ return unless root_item_for_menu(unique_code).present?++ root_item_for_menu(unique_code).childrenend# Returns all items for the menu by unique_codedef all_items_for_menu(unique_code)- if menu(unique_code).present?- menu(unique_code).root.descendants- end+ return unless root_item_for_menu(unique_code).present?++ root_item_for_menu(unique_code).descendantsendprivate