Skip to content

Commit

Permalink
Merge pull request #1210 from vuestorefront/M2-918-currency-cookie-br…
Browse files Browse the repository at this point in the history
…eaks-the-application

fix: currency cookie breaks the application
  • Loading branch information
Frodigo authored Jul 6, 2022
2 parents 664454e + 55a59e8 commit 3361878
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
3 changes: 3 additions & 0 deletions packages/theme/plugins/__tests__/i18n.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,5 +118,8 @@ describe('i18n plugin', () => {
expect(testCaseAppMock.$vsf.$magento.config.state.setLocale).toHaveBeenCalledWith('de_DE');
expect(testCaseAppMock.$vsf.$magento.config.state.setStore).toHaveBeenCalledWith('de_DE');
expect(testCaseAppMock.$vsf.$magento.config.state.setCurrency).toHaveBeenCalledWith('EUR');
expect(callbackRequest.headers.cookie).toMatchInlineSnapshot(
'"vsf-store=de_DE; vsf-locale=de_DE; vsf-currency=EUR; vsf-country=PL; vsf-customer=12fg45; vsf-cart=123 "',
);
});
});
38 changes: 35 additions & 3 deletions packages/theme/plugins/i18n.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Context } from '@nuxt/types';
import { Context, NuxtAppOptions } from '@nuxt/types';
import { LocaleObject } from 'nuxt-i18n';

const findLocaleBasedOnMagentoStoreCode = (storeCode: string, locales: Array<string | LocaleObject>) => locales.find((locale) => ((typeof locale === 'object' ? locale.code : locale) === storeCode));
Expand All @@ -8,6 +8,35 @@ const findCurrencyBasedOnMagentoStoreCode = (storeCode: string, locales: Array<s
return match?.defaultCurrency;
};

/**
* Prepare new cookie string based on app state.
*
* @param app {NuxtAppOptions}
* @param newStoreCode {string}
* @param currency {string}
* @returns {string}
*/
const prepareNewCookieString = (app: NuxtAppOptions, newStoreCode: string, currency: string) => {
const apiState = app.$vsf.$magento.config.state;
const customerTokenCookie = apiState.getCustomerToken();
const cartIdCookie = apiState.getCartId();

let cookie = `vsf-store=${newStoreCode}; `;
cookie += `vsf-locale=${newStoreCode}; `;
cookie += `vsf-currency=${currency}; `;
cookie += `vsf-country=${apiState.getCountry()}; `;

if (customerTokenCookie) {
cookie += `vsf-customer=${customerTokenCookie}; `;
}

if (cartIdCookie) {
cookie += `vsf-cart=${cartIdCookie} `;
}

return cookie;
};

export default ({ app, route }: Context) => app.$vsf.$magento.client.interceptors.request.use(async (request) => {
const {
$vsf: { $magento: { config: { state } } },
Expand All @@ -25,11 +54,14 @@ export default ({ app, route }: Context) => app.$vsf.$magento.client.interceptor
const shouldLocaleBeRefreshed = i18nCurrentLocaleCode !== state.getLocale();

if (shouldLocaleBeRefreshed) {
const currency = findCurrencyBasedOnMagentoStoreCode(currentStoreCode, i18n.locales);
state.setCurrency(currency);
const currency = findCurrencyBasedOnMagentoStoreCode(i18nCurrentLocaleCode, i18n.locales);

state.setStore(i18nCurrentLocaleCode);
state.setLocale(i18nCurrentLocaleCode);
state.setCurrency(currency);

// eslint-disable-next-line no-param-reassign
request.headers.cookie = prepareNewCookieString(app, i18nCurrentLocaleCode, currency);
}

return request;
Expand Down

0 comments on commit 3361878

Please sign in to comment.