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.

allow filtering Products by price range on mobile

Łukasz Adamczyk11f4ff8ca1d7
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 169c4bb707..251507bbdb 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
@@ -99,13 +99,4 @@ Spree.ready(function ($) {
$('.plp-overlay-ul-li').removeClass('plp-overlay-ul-li--active')
$(this).addClass('plp-overlay-ul-li--active');
});
-
- $('.plp-overlay-buttons .done-btn').click(function (event) {
- if (window.URLSearchParams) {
- event.preventDefault();
- var searchParams = new URLSearchParams(location.search);
- searchParams.delete('menu_open');
- setNewUrl(searchParams);
- }
- });
});
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
index 65eeb78499..c7d5c01021 100644
--- 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
@@ -1,26 +1,44 @@
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"]')
+ class PriceRangeFilter {
+ constructor(inputsContainer, filterButton) {
+ this.inputsContainer = inputsContainer
+ this.filterButton = filterButton
- priceInputs.forEach((priceInput) => {
- priceInput.addEventListener('change', () => {
- updatePriceRangeFilter(
- parseInt(minPriceInput.value) || 0,
- parseInt(maxPriceInput.value) || 0
- )
- })
- })
+ this.priceInputs = inputsContainer.querySelectorAll('input')
+ this.minPriceInput = inputsContainer.querySelector('input[name="min_price"]')
+ this.maxPriceInput = inputsContainer.querySelector('input[name="max_price"]')
+ }
- function updatePriceRangeFilter(minPrice, maxPrice) {
- const formattedPriceRange = `${minPrice} - ${maxPrice}`
+ handlePriceChange() {
+ this.priceInputs.forEach((priceInput) => {
+ priceInput.addEventListener('change', () => {
+ this.updatePricesForFiltering(
+ parseInt(this.minPriceInput.value) || 0,
+ parseInt(this.maxPriceInput.value) || 0
+ )
+ })
+ })
+ }
- const priceRangeSubmitButton = priceRangeFilterElement.querySelector('a')
- const dataParams = JSON.parse(priceRangeSubmitButton.dataset.params)
- const urlParams = new URLSearchParams(dataParams)
+ updatePricesForFiltering(minPrice, maxPrice) {
+ const formattedPriceRange = `${minPrice} - ${maxPrice}`
- urlParams.set('price', formattedPriceRange)
- priceRangeSubmitButton.href = decodeURIComponent(`${location.pathname}?${urlParams.toString()}`)
+ const dataParams = JSON.parse(this.filterButton.dataset.params)
+ const urlParams = new URLSearchParams(dataParams)
+
+ urlParams.set('price', formattedPriceRange)
+ this.filterButton.href = decodeURIComponent(`${location.pathname}?${urlParams.toString()}`)
+ }
}
+
+ // we have 2 elements for filtering prices - desktop and mobile
+ const desktopElement = document.getElementById('filterPriceRangeDesktop')
+ const desktopFilterButton = desktopElement.querySelector('a')
+ const desktopPriceRangeFilter = new PriceRangeFilter(desktopElement, desktopFilterButton)
+ desktopPriceRangeFilter.handlePriceChange()
+
+ const mobileElement = document.getElementById('filterPriceRangeMobile')
+ const mobileFilterButton = document.getElementById('filterProductsButtonMobile')
+ const mobilePriceRangeFilter = new PriceRangeFilter(mobileElement, mobileFilterButton)
+ mobilePriceRangeFilter.handlePriceChange()
});
diff --git a/frontend/app/helpers/spree/products_filters_helper.rb b/frontend/app/helpers/spree/products_filters_helper.rb
index 101338f677..46308f1a96 100644
--- a/frontend/app/helpers/spree/products_filters_helper.rb
+++ b/frontend/app/helpers/spree/products_filters_helper.rb
@@ -10,7 +10,7 @@ module Spree
def price_filter_input(name:, value:, placeholder:)
price_value = value&.zero? ? '' : value
- number_field_tag(name, price_value, min: 0, step: 1, placeholder: placeholder)
+ number_field_tag(name, price_value, id: name, min: 0, step: 1, placeholder: placeholder)
end
private
diff --git a/frontend/app/views/spree/products/_filters_desktop.html.erb b/frontend/app/views/spree/products/_filters_desktop.html.erb
index fa5ac2fe9b..bca23cebd8 100644
--- a/frontend/app/views/spree/products/_filters_desktop.html.erb
+++ b/frontend/app/views/spree/products/_filters_desktop.html.erb
@@ -84,7 +84,7 @@
<% end %>
<% end %>
- <div id="filterPriceRange">
+ <div id="filterPriceRangeDesktop">
<%= 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 } %>
diff --git a/frontend/app/views/spree/products/_filters_mobile.html.erb b/frontend/app/views/spree/products/_filters_mobile.html.erb
index f71b5a4e69..27b3b41898 100644
--- a/frontend/app/views/spree/products/_filters_mobile.html.erb
+++ b/frontend/app/views/spree/products/_filters_mobile.html.erb
@@ -74,6 +74,11 @@
</div>
<% end %>
<% end %>
+
+ <div id="filterPriceRangeMobile">
+ <%= min_price_filter_input %>
+ <%= max_price_filter_input %>
+ </div>
</div>
</div>
</div>
@@ -87,6 +92,6 @@
<div class="container position-absolute text-center plp-overlay-buttons pt-3">
<%= link_to Spree.t('plp.clear_all'), permitted_params.select { |key, value| key == "sort_by"}, class: 'btn spree-btn btn-outline-primary w-100 mb-4', data: { params: permitted_params.select { |key, value| key == "sort_by"} } %>
- <%= link_to Spree.t('plp.done'), permitted_params, class: 'btn btn-primary spree-btn w-100 done-btn', data: { params: permitted_params } %>
+ <%= link_to Spree.t('plp.done'), permitted_params, id: 'filterProductsButtonMobile', class: 'btn btn-primary spree-btn w-100 done-btn', data: { params: permitted_params } %>
</div>
</div>