From fe11412a290d288b7b1430d2eec2a3440a47ff1d Mon Sep 17 00:00:00 2001 From: Diana Huang Date: Mon, 26 Feb 2018 12:09:47 -0500 Subject: [PATCH] Assume the cookie is set and don't try to get it again. --- .../static/course_experience/js/currency.js | 69 +++---------------- .../js/spec/Currency_spec.js | 27 ++------ 2 files changed, 13 insertions(+), 83 deletions(-) diff --git a/openedx/features/course_experience/static/course_experience/js/currency.js b/openedx/features/course_experience/static/course_experience/js/currency.js index 0367e61b31..be5c3c63a0 100644 --- a/openedx/features/course_experience/static/course_experience/js/currency.js +++ b/openedx/features/course_experience/static/course_experience/js/currency.js @@ -4,34 +4,6 @@ import $ from 'jquery'; // eslint-disable-line import/extensions export class Currency { // eslint-disable-line import/prefer-default-export - setCookie(countryCode, l10nData) { - function pick(curr, arr) { - const obj = {}; - arr.forEach((key) => { - obj[key] = curr[key]; - }); - return obj; - } - const userCountryData = pick(l10nData, [countryCode]); - let countryL10nData = userCountryData[countryCode]; - - if (countryL10nData) { - countryL10nData.countryCode = countryCode; - $.cookie('edx-price-l10n', JSON.stringify(countryL10nData), { - domain: 'edx.org', - expires: 1, - }); - } else { - countryL10nData = { - countryCode: 'USA', - symbol: '$', - rate: '1', - code: 'USD', - }; - } - this.countryL10nData = countryL10nData; - } - setPrice() { const l10nCookie = this.countryL10nData; const lmsregex = /(\$)(\d*)( USD)/g; @@ -45,42 +17,17 @@ export class Currency { // eslint-disable-line import/prefer-default-export price.value = price.value.replace(regexMatch[0], string); } - getL10nData(countryCode) { - const l10nData = JSON.parse($('#currency_data').attr('value')); - if (l10nData) { - this.setCookie(countryCode, l10nData); - } - } - - getCountry(position) { - const countryCode = whichCountry([position.coords.longitude, position.coords.latitude]); + getCountry() { this.countryL10nData = JSON.parse($.cookie('edx-price-l10n')); - - if (countryCode) { - if (!(this.countryL10nData && this.countryL10nData.countryCode === countryCode)) { - // If pricing cookie has not been set or the country is not correct - // Make API call and set the cookie - this.getL10nData(countryCode); - } + if (this.countryL10nData) { + window.analytics.track('edx.bi.user.track_selection.local_currency_cookie_set'); + this.setPrice(); } - this.setPrice(); } - getCountryCaller(position) { - const caller = function callerFunction() { - this.getCountry(position); - }.bind(this); - $(document).ready(caller); - } - - getUserLocation() { - // Get user location from browser - navigator.geolocation.getCurrentPosition(this.getCountryCaller.bind(this)); - } - - constructor(skipInitialize) { - if (!skipInitialize) { - this.getUserLocation(); - } + constructor() { + $(document).ready(() => { + this.getCountry(); + }); } } diff --git a/openedx/features/course_experience/static/course_experience/js/spec/Currency_spec.js b/openedx/features/course_experience/static/course_experience/js/spec/Currency_spec.js index 8e6db6b8f0..f6f57de098 100644 --- a/openedx/features/course_experience/static/course_experience/js/spec/Currency_spec.js +++ b/openedx/features/course_experience/static/course_experience/js/spec/Currency_spec.js @@ -11,7 +11,6 @@ describe('Currency factory', () => { beforeEach(() => { loadFixtures('course_experience/fixtures/course-currency-fragment.html'); - currency = new Currency(true); canadaPosition = { coords: { latitude: 58.773884, @@ -34,30 +33,14 @@ describe('Currency factory', () => { }); describe('converts price to local currency', () => { - it('when location is US', () => { - currency.getCountry(usaPosition); + it('when location is the default (US)', () => { + $.cookie('edx-price-l10n', '{"rate":1,"code":"USD","symbol":"$","countryCode":"US"}', { path: '/' }); + currency = new Currency(); expect($('input[name="verified_mode"]').filter(':visible')[0].value).toEqual('Pursue a Verified Certificate ($100 USD)'); }); - - it('when location is an unsupported country', () => { - currency.getCountry(japanPosition); - expect($('input[name="verified_mode"]').filter(':visible')[0].value).toEqual('Pursue a Verified Certificate ($100 USD)'); - }); - - it('when cookie is not set and country is supported', () => { - currency.getCountry(canadaPosition); - expect($('input[name="verified_mode"]').filter(':visible')[0].value).toEqual('Pursue a Verified Certificate ($220 CAD)'); - }); - - it('when cookie is set to same country', () => { - currency.getCountry(canadaPosition); + it('when cookie is set to a different country', () => { $.cookie('edx-price-l10n', '{"rate":2.2,"code":"CAD","symbol":"$","countryCode":"CAN"}', { expires: 1 }); - expect($('input[name="verified_mode"]').filter(':visible')[0].value).toEqual('Pursue a Verified Certificate ($220 CAD)'); - }); - - it('when cookie is set to different country', () => { - currency.getCountry(canadaPosition); - $.cookie('edx-price-l10n', '{"rate":1,"code":"USD","symbol":"$","countryCode":"USA"}', { expires: 1 }); + currency = new Currency(); expect($('input[name="verified_mode"]').filter(':visible')[0].value).toEqual('Pursue a Verified Certificate ($220 CAD)'); }); });