-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10812 from spree/fix/clear-turbolinks-cache-on-cu…
…rrency-change Fix turbolinks caching issues with currency change
- Loading branch information
Showing
16 changed files
with
141 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 40 additions & 8 deletions
48
frontend/app/assets/javascripts/spree/frontend/currency.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,43 @@ | ||
Spree.ready(function ($) { | ||
var currencySelect = $('#currency-select select') | ||
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 (currencySelect.length) { | ||
currencySelect.on('change', function () { | ||
currencySelect.attr('disabled') | ||
Spree.showProgressBar() | ||
window.location = Spree.routes.set_currency(this.value) | ||
}) | ||
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) | ||
} | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
Spree.ready(function ($) { | ||
var localeSelect = $('#locale-select select') | ||
document.addEventListener('turbolinks:load', function () { | ||
var localeSelect = document.querySelectorAll('select[name=switch_to_locale]') | ||
|
||
if (localeSelect.length) { | ||
localeSelect.on('change', function () { | ||
localeSelect.attr('disabled') | ||
Spree.showProgressBar() | ||
window.location = Spree.routes.set_locale(this.value) | ||
localeSelect.forEach(function (element) { | ||
element.addEventListener('change', function () { | ||
Spree.clearCache() | ||
Spree.showProgressBar() | ||
this.form.submit() | ||
}) | ||
}) | ||
} | ||
}) |
13 changes: 0 additions & 13 deletions
13
frontend/app/assets/javascripts/spree/frontend/multi_currency.js
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
frontend/app/views/spree/shared/_locale_and_currency.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters