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.

Fixed back button issue by forcing to reload page with the currently chosen currency

Damian Legawiec3574f42626b5
diff --git a/.eslintrc b/.eslintrc
index ec4972afd1..3102f5e4e7 100644
--- a/.eslintrc
+++ b/.eslintrc
@@ -18,6 +18,8 @@ globals:
AUTH_TOKEN: true
SPREE_LOCALE: true
SPREE_CURRENCY: true
+ SPREE_DEFAULT_CURRENCY: true
+ SPREE_DEFAULT_LOCALE: true
update_state: true
env:
diff --git a/core/lib/spree/testing_support/locale_helpers.rb b/core/lib/spree/testing_support/locale_helpers.rb
index da6fbf81c0..88e7eb0f62 100644
--- a/core/lib/spree/testing_support/locale_helpers.rb
+++ b/core/lib/spree/testing_support/locale_helpers.rb
@@ -55,11 +55,18 @@ module Spree
def open_i18n_menu
find('#header #internationalization-button-desktop').click
+ expect(page).to have_selector('#internationalization-options-desktop')
+ end
+
+ def close_i18n_menu
+ find('#header #internationalization-button-desktop').click
+ expect(page).not_to have_selector('#internationalization-options-desktop')
end
def switch_to_currency(currency)
open_i18n_menu
select currency, from: 'switch_to_currency'
+ expect(page).to have_no_css '.turbolinks-progress-bar'
end
def switch_to_locale(locale)
diff --git a/frontend/app/assets/javascripts/spree/frontend.js b/frontend/app/assets/javascripts/spree/frontend.js
index 511ecd48fa..fd1b01934a 100644
--- a/frontend/app/assets/javascripts/spree/frontend.js
+++ b/frontend/app/assets/javascripts/spree/frontend.js
@@ -63,3 +63,20 @@ Spree.clearCache = function () {
Turbolinks.clearCache()
}
+
+Spree.setCurrency = function (currency) {
+ Spree.clearCache()
+
+ var params = (new URL(window.location)).searchParams
+ if (currency === SPREE_DEFAULT_CURRENCY) {
+ params.delete('currency')
+ } else {
+ params.set('currency', currency)
+ }
+ var queryString = params.toString()
+ if (queryString !== '') { queryString = '?' + queryString }
+
+ SPREE_CURRENCY = currency
+
+ Turbolinks.visit(window.location.pathname + queryString, { action: 'replace' })
+}
diff --git a/frontend/app/assets/javascripts/spree/frontend/currency.js b/frontend/app/assets/javascripts/spree/frontend/currency.js
index 21cae8160f..8b7ad051e3 100644
--- a/frontend/app/assets/javascripts/spree/frontend/currency.js
+++ b/frontend/app/assets/javascripts/spree/frontend/currency.js
@@ -1,13 +1,43 @@
-Spree.ready(function ($) {
- var currencySelectForm = $('#currency-select')
- var currencySelectDropdown = currencySelectForm.find('select#switch_to_currency')
+document.addEventListener('turbolinks:load', function(event) {
+ // this condition checks if this is the first initial load of turbolinks application
+ if (!event.data.timing.visitStart) {
+ var currencySelect = document.querySelectorAll('select[name=switch_to_currency]')
- if (currencySelectForm.length && currencySelectDropdown.length) {
- currencySelectDropdown.on('change', function () {
- Spree.clearCache()
- currencySelectDropdown.attr('disabled')
- Spree.showProgressBar()
- currencySelectForm.submit()
- })
+ if (currencySelect.length) {
+ currencySelect.forEach(function (element) {
+ element.addEventListener('change', function () {
+ Spree.showProgressBar()
+ var newCurrency = this.value
+
+ // we need to make AJAX call here to the backend to set currency in session
+ fetch(Spree.routes.set_currency(newCurrency), {
+ method: 'GET'
+ }).then(function (response) {
+ switch (response.status) {
+ case 200:
+ Spree.setCurrency(newCurrency)
+ document.getElementById('internationalization-options-desktop').classList.remove('show')
+ break
+ }
+ })
+ })
+ })
+ }
+ }
+})
+
+// fix back button issue with different currency set
+// invalidate page if cached page has different currency then the current one
+document.addEventListener('turbolinks:load', function(event) {
+ if (SPREE_CURRENCY === SPREE_DEFAULT_CURRENCY) {
+ var regexAnyCurrency = new RegExp('currency=')
+ if (event.data.url.match(regexAnyCurrency) && !event.data.url.match(SPREE_CURRENCY)) {
+ Spree.setCurrency(SPREE_CURRENCY)
+ }
+ } else {
+ var regex = new RegExp('currency=' + SPREE_CURRENCY)
+ if (!event.data.url.match(regex)) {
+ Spree.setCurrency(SPREE_CURRENCY)
+ }
}
})
diff --git a/frontend/app/assets/javascripts/spree/frontend/locale.js b/frontend/app/assets/javascripts/spree/frontend/locale.js
index 810ff6b1ad..6010bfefc7 100644
--- a/frontend/app/assets/javascripts/spree/frontend/locale.js
+++ b/frontend/app/assets/javascripts/spree/frontend/locale.js
@@ -1,12 +1,13 @@
-Spree.ready(function ($) {
- var localeSelectForm = $('#locale-select')
- var localeSelectDropdown = localeSelectForm.find('select#switch_to_locale')
+document.addEventListener('turbolinks:load', function () {
+ var localeSelect = document.querySelectorAll('select[name=switch_to_locale]')
- if (localeSelectForm.length && localeSelectDropdown.length) {
- localeSelectDropdown.on('change', function () {
- localeSelectDropdown.attr('disabled')
- Spree.showProgressBar()
- localeSelectForm.submit()
+ if (localeSelect.length) {
+ localeSelect.forEach(function (element) {
+ element.addEventListener('change', function () {
+ Spree.clearCache()
+ Spree.showProgressBar()
+ this.form.submit()
+ })
})
}
})
diff --git a/frontend/app/controllers/spree/currency_controller.rb b/frontend/app/controllers/spree/currency_controller.rb
index b67befdbf6..5139c1fd0b 100644
--- a/frontend/app/controllers/spree/currency_controller.rb
+++ b/frontend/app/controllers/spree/currency_controller.rb
@@ -7,7 +7,11 @@ module Spree
current_order&.update(currency: new_currency)
session[:currency] = new_currency
end
- redirect_back fallback_location: spree.root_path(currency: new_currency)
+
+ respond_to do |format|
+ format.html { redirect_back fallback_location: spree.root_path(currency: new_currency) }
+ format.json { head :ok }
+ end
end
end
end
diff --git a/frontend/app/views/spree/shared/_currency_dropdown.html.erb b/frontend/app/views/spree/shared/_currency_dropdown.html.erb
index a897e1d2f6..cf60e9ccb1 100644
--- a/frontend/app/views/spree/shared/_currency_dropdown.html.erb
+++ b/frontend/app/views/spree/shared/_currency_dropdown.html.erb
@@ -1,5 +1,5 @@
<% if defined?(should_render_currency_dropdown?) && should_render_currency_dropdown? %>
- <%= form_tag spree.set_currency_path, method: :get, class: 'px-4 py-3 w-100', id: 'currency-select' do %>
+ <%= form_tag spree.set_currency_path, method: :get, class: 'px-4 py-3 w-100 currency-select' do %>
<div class="form-group">
<label for="switch_to_currency" class="dropdown-header text-center">
<%= Spree.t(:choose_currency) %>
diff --git a/frontend/app/views/spree/shared/_head.html.erb b/frontend/app/views/spree/shared/_head.html.erb
index 42193d4f84..9034e4a802 100644
--- a/frontend/app/views/spree/shared/_head.html.erb
+++ b/frontend/app/views/spree/shared/_head.html.erb
@@ -8,9 +8,6 @@
<%= stylesheet_link_tag 'spree/frontend/all', media: 'screen', 'data-turbolinks-track': 'reload' %>
<%= csrf_meta_tags %>
<%= render 'spree/shared/paths' %>
-<script>
- var SPREE_LOCALE = '<%= I18n.locale %>'
- var SPREE_CURRENCY = '<%= current_currency %>'
-</script>
+<%= render 'spree/shared/locale_and_currency' %>
<%= javascript_include_tag 'spree/frontend/all', defer: true, 'data-turbolinks-track': 'reload' %>
<%= yield :head %>
diff --git a/frontend/app/views/spree/shared/_locale_and_currency.html.erb b/frontend/app/views/spree/shared/_locale_and_currency.html.erb
new file mode 100644
index 0000000000..9e2698801f
--- /dev/null
+++ b/frontend/app/views/spree/shared/_locale_and_currency.html.erb
@@ -0,0 +1,6 @@
+<script>
+ var SPREE_LOCALE = '<%= I18n.locale %>'
+ var SPREE_CURRENCY = '<%= current_currency %>'
+ var SPREE_DEFAULT_CURRENCY = '<%= current_store.default_currency %>'
+ var SPREE_DEFAULT_LOCALE = '<%= current_store.default_locale %>'
+</script>
diff --git a/frontend/app/views/spree/shared/_locale_dropdown.html.erb b/frontend/app/views/spree/shared/_locale_dropdown.html.erb
index ceb0c66c23..ee26fcd5e4 100644
--- a/frontend/app/views/spree/shared/_locale_dropdown.html.erb
+++ b/frontend/app/views/spree/shared/_locale_dropdown.html.erb
@@ -1,5 +1,5 @@
<% if should_render_locale_dropdown? %>
- <%= form_tag spree.set_locale_path, method: :get, class: 'px-4 py-3 w-100', id: 'locale-select' do %>
+ <%= form_tag spree.set_locale_path, method: :get, class: 'px-4 py-3 w-100 locale-select' do %>
<div class="form-group">
<label for="switch_to_locale" class="dropdown-header text-center">
<%= Spree.t('i18n.language') %>
diff --git a/frontend/spec/features/product_prices_spec.rb b/frontend/spec/features/product_prices_spec.rb
index 78cb4dd374..5ce3ace7cc 100644
--- a/frontend/spec/features/product_prices_spec.rb
+++ b/frontend/spec/features/product_prices_spec.rb
@@ -16,13 +16,20 @@ describe 'Product with prices in multiple currencies', type: :feature, js: true
expect(page).to have_text '$19.99'
switch_to_currency('EUR')
expect(page).to have_text '€16.00'
+ expect(page).to have_current_path(spree.product_path(product, currency: 'EUR'))
switch_to_currency('GBP')
expect(page).to have_text '£23.00'
+ expect(page).to have_current_path(spree.product_path(product, currency: 'GBP'))
visit spree.products_path
expect(page).to have_text '£23.00'
expect(page).to have_link product.name, href: "/products/#{product.slug}?currency=GBP"
open_i18n_menu
expect(page).to have_select('switch_to_currency', selected: 'GBP')
+ close_i18n_menu
+ switch_to_currency('USD')
+ expect(page).to have_text '$19.99'
+ expect(page).to have_current_path(spree.products_path)
+ expect(page).to have_link product.name, href: "/products/#{product.slug}"
end
end
end