Skip to content

armgit / spree

Public Imported from GitHub
0

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.

* reimplement price range inputs for filtering Products * switch from jQuery to JS ES6 for the implementation of inputs

Łukasz Adamczyk23cbef14ed8a
diff --git a/frontend/app/assets/javascripts/spree/frontend.js b/frontend/app/assets/javascripts/spree/frontend.js
index a72bf29b71..73a60d8a07 100644
--- a/frontend/app/assets/javascripts/spree/frontend.js
+++ b/frontend/app/assets/javascripts/spree/frontend.js
@@ -29,6 +29,7 @@
//= require spree/frontend/views/spree/products/description
//= require spree/frontend/views/spree/products/index
//= require spree/frontend/views/spree/products/modal_carousel
+//= require spree/frontend/views/spree/products/price_filters
//= require spree/frontend/views/spree/shared/carousel
//= require spree/frontend/views/spree/shared/carousel/single
//= require spree/frontend/views/spree/shared/carousel/swipes
diff --git a/frontend/app/assets/javascripts/spree/frontend/views/spree/products/index.js b/frontend/app/assets/javascripts/spree/frontend/views/spree/products/index.js
index dabf3d9b2a..169c4bb707 100644
--- a/frontend/app/assets/javascripts/spree/frontend/views/spree/products/index.js
+++ b/frontend/app/assets/javascripts/spree/frontend/views/spree/products/index.js
@@ -108,18 +108,4 @@ Spree.ready(function ($) {
setNewUrl(searchParams);
}
});
-
- $('#filterPriceRange').submit(function(event) {
- var $priceRangeForm = $(event.currentTarget);
- var minPrice = parseInt($priceRangeForm.find('[name="min_price"]').val());
- var maxPrice = parseInt($priceRangeForm.find('[name="max_price"]').val());
-
- var formattedPriceRange = [
- minPrice || 0,
- '-',
- maxPrice || 0
- ].join(' ');
-
- $priceRangeForm.find('[name="price"]').val(formattedPriceRange)
- });
});
diff --git a/frontend/app/assets/javascripts/spree/frontend/views/spree/products/price_filters.es6 b/frontend/app/assets/javascripts/spree/frontend/views/spree/products/price_filters.es6
new file mode 100644
index 0000000000..45646cd779
--- /dev/null
+++ b/frontend/app/assets/javascripts/spree/frontend/views/spree/products/price_filters.es6
@@ -0,0 +1,26 @@
+Spree.ready(function() {
+ const priceRangeFilterElement = document.getElementById('filterPriceRange')
+ const priceInputs = priceRangeFilterElement.querySelectorAll('input')
+ const minPriceInput = priceRangeFilterElement.querySelector('input[name="min_price"]')
+ const maxPriceInput = priceRangeFilterElement.querySelector('input[name="max_price"]')
+
+ priceInputs.forEach((priceInput) => {
+ priceInput.addEventListener('change', () => {
+ updatePriceRangeFilter(
+ parseInt(minPriceInput.value) || 0,
+ parseInt(maxPriceInput.value) || 0
+ )
+ })
+ })
+
+ function updatePriceRangeFilter(minPrice, maxPrice) {
+ const formattedPriceRange = `${minPrice} - ${maxPrice}`
+
+ const priceRangeSubmitButton = priceRangeFilterElement.querySelector('a')
+ const dataParams = JSON.parse(priceRangeSubmitButton.dataset.params)
+ const urlParams = new URLSearchParams(dataParams)
+
+ urlParams.set('price', formattedPriceRange)
+ priceRangeSubmitButton.href = `${location.pathname}?${decodeURIComponent(urlParams.toString())}`
+ }
+});
diff --git a/frontend/app/helpers/spree/products_filters_helper.rb b/frontend/app/helpers/spree/products_filters_helper.rb
new file mode 100644
index 0000000000..2997b9172c
--- /dev/null
+++ b/frontend/app/helpers/spree/products_filters_helper.rb
@@ -0,0 +1,23 @@
+module Spree
+ module ProductsFiltersHelper
+ def min_price_filter_input
+ price_filter_input(name: :min_price, value: prices.min, placeholder: Spree.t(:min))
+ end
+
+ def max_price_filter_input
+ price_filter_input(name: :max_price, value: prices.max, placeholder: Spree.t(:max))
+ end
+
+ def price_filter_input(name:, value:, placeholder:)
+ number_field_tag(name, value, min: 0, step: 1, placeholder: placeholder)
+ end
+
+ private
+
+ def prices
+ price_param = params[:price].to_s
+ split_prices = price_param.split('-')
+ split_prices.map(&:to_i)
+ end
+ end
+end
diff --git a/frontend/app/views/spree/products/_filters_desktop.html.erb b/frontend/app/views/spree/products/_filters_desktop.html.erb
index c6ae23f722..fa5ac2fe9b 100644
--- a/frontend/app/views/spree/products/_filters_desktop.html.erb
+++ b/frontend/app/views/spree/products/_filters_desktop.html.erb
@@ -84,13 +84,11 @@
<% end %>
<% end %>
- <%= form_with url: permitted_params, id: 'filterPriceRange', method: :get do |f| %>
- <%= f.hidden_field :price %>
- <%= f.number_field :min_price, value: params[:min_price], min: 0, step: 1, placeholder: Spree.t(:min) %>
- <%= f.number_field :max_price, value: params[:max_price], min: 0, step: 1, placeholder: Spree.t(:max) %>
-
- <%= f.submit Spree.t(:search) %>
- <% end %>
+ <div id="filterPriceRange">
+ <%= min_price_filter_input %>
+ <%= max_price_filter_input %>
+ <%= link_to Spree.t('plp.done'), permitted_params, class: 'btn btn-primary spree-btn', data: { params: permitted_params } %>
+ </div>
</div>
</div>
</div>
diff --git a/frontend/spec/features/products_spec.rb b/frontend/spec/features/products_spec.rb
index bb1a66bb63..03e423fede 100644
--- a/frontend/spec/features/products_spec.rb
+++ b/frontend/spec/features/products_spec.rb
@@ -298,7 +298,7 @@ describe 'Visiting Products', type: :feature, inaccessible: true do
fill_in Spree.t(:min), with: '55'
fill_in Spree.t(:max), with: '103'
- click_on Spree.t(:search)
+ click_on Spree.t('plp.done')
end
expect(page).to have_css('.product-component-name').exactly(3).times
@@ -316,7 +316,7 @@ describe 'Visiting Products', type: :feature, inaccessible: true do
click_on Spree.t('plp.price')
fill_in Spree.t(:min), with: '56'
- click_on Spree.t(:search)
+ click_on Spree.t('plp.done')
end
expect(page).to have_css('.product-component-name').exactly(7).times
@@ -338,7 +338,7 @@ describe 'Visiting Products', type: :feature, inaccessible: true do
click_on Spree.t('plp.price')
fill_in Spree.t(:max), with: '56'
- click_on Spree.t(:search)
+ click_on Spree.t('plp.done')
end
expect(page).to have_css('.product-component-name').exactly(2).times