Skip to content

Commit d3c8082

Browse files
committed
fix(theme): fix setting proper store and locale if there is no cookie
1 parent a34e99c commit d3c8082

File tree

3 files changed

+59
-29
lines changed

3 files changed

+59
-29
lines changed

packages/theme/composables/useMagentoConfiguration.ts

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,25 +52,30 @@ export const useMagentoConfiguration: UseMagentoConfiguration = () => {
5252
updateCookies,
5353
updateLocale,
5454
} = params;
55-
loadConfig();
56-
loadStores();
57-
loadCurrencies();
5855

59-
if (!app.$cookies.get(cookieNames.storeCookieName) || updateCookies) {
60-
app.$cookies.set(cookieNames.storeCookieName, storeConfigGetters.getCode(storeConfig.value));
61-
}
56+
// eslint-disable-next-line promise/catch-or-return
57+
loadConfig().then(() => {
58+
if (!app.$cookies.get(cookieNames.storeCookieName) || updateCookies) {
59+
app.$cookies.set(cookieNames.storeCookieName, storeConfigGetters.getCode(storeConfig.value));
60+
}
61+
62+
if (!app.$cookies.get(cookieNames.localeCookieName) || updateCookies) {
63+
app.$cookies.set(cookieNames.localeCookieName, storeConfigGetters.getCode(storeConfig.value));
64+
}
6265

63-
if (!app.$cookies.get(cookieNames.localeCookieName) || updateCookies) {
64-
app.$cookies.set(cookieNames.localeCookieName, storeConfigGetters.getLocale(storeConfig.value));
65-
}
66+
if (!app.$cookies.get(cookieNames.currencyCookieName) || updateCookies) {
67+
app.$cookies.set(cookieNames.currencyCookieName, storeConfigGetters.getCurrency(storeConfig.value));
68+
}
6669

67-
if (!app.$cookies.get(cookieNames.currencyCookieName) || updateCookies) {
68-
app.$cookies.set(cookieNames.currencyCookieName, storeConfigGetters.getCurrency(storeConfig.value));
69-
}
70+
if (updateLocale) {
71+
app.i18n.setLocale(storeConfigGetters.getLocale(storeConfig.value));
72+
}
7073

71-
if (updateLocale) {
72-
app.i18n.setLocale(storeConfigGetters.getLocale(storeConfig.value));
73-
}
74+
return true;
75+
});
76+
77+
loadStores();
78+
loadCurrencies();
7479
};
7580

7681
return {

packages/theme/plugins/__tests__/i18n.spec.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,19 @@ const localesMock = [
55
code: 'default',
66
file: 'en.js',
77
iso: 'en_US',
8+
defaultCurrency: 'USD',
89
},
910
{
1011
code: 'de_DE',
1112
file: 'de.js',
1213
iso: 'de_DE',
14+
defaultCurrency: 'EUR',
1315
},
1416
{
1517
code: 'nl_NL',
1618
file: 'en.js',
1719
iso: 'en_US',
20+
defaultCurrency: 'EUR',
1821
},
1922
];
2023

@@ -29,14 +32,19 @@ const callbackRequest = {
2932
headers: {},
3033
};
3134

35+
const routeMock = {
36+
path: 'https://domain/german',
37+
};
3238
const appMock = {
3339
$cookies: {
3440
get: jest.fn(),
3541
},
3642
i18n: {
3743
defaultLocale: 'en',
44+
defaultCurrency: 'EUR',
3845
setLocale: jest.fn(),
3946
locales: localesMock,
47+
locale: 'de_DE',
4048
},
4149
$vsf: {
4250
$magento: {
@@ -54,6 +62,7 @@ const appMock = {
5462
...apiStateMock,
5563
setStore: jest.fn(),
5664
setLocale: jest.fn(),
65+
setCurrency: jest.fn(),
5766
},
5867
axios: {
5968
headers: {
@@ -71,14 +80,14 @@ describe('i18n plugin', () => {
7180
});
7281

7382
it('Should read vsf-store cookie value', () => {
74-
i18nPlugin({ app: appMock });
83+
i18nPlugin({ app: appMock, route: routeMock });
7584

7685
expect(appMock.$cookies.get).toHaveBeenCalledWith('vsf-store');
7786
});
7887

7988
it('Should find locale based on magento store code', () => {
8089
appMock.$cookies.get.mockReturnValue('default');
81-
i18nPlugin({ app: appMock });
90+
i18nPlugin({ app: appMock, route: routeMock });
8291

8392
expect(appMock.i18n.setLocale).not.toHaveBeenCalled();
8493
});
@@ -91,7 +100,7 @@ describe('i18n plugin', () => {
91100

92101
it('Should set default locale when vsf-store cookie is not exist', () => {
93102
appMock.$cookies.get.mockReturnValue(null);
94-
i18nPlugin({ app: appMock });
103+
i18nPlugin({ app: appMock, route: routeMock });
95104

96105
expect(appMock.i18n.setLocale).toHaveBeenCalledWith('en');
97106
});
@@ -107,12 +116,13 @@ describe('i18n plugin', () => {
107116

108117
testCaseAppMock.$cookies.get.mockReturnValueOnce('de_DE').mockReturnValueOnce('default');
109118

110-
i18nPlugin({ app: testCaseAppMock });
119+
i18nPlugin({ app: testCaseAppMock, route: routeMock });
111120

112121
expect(testCaseAppMock.$vsf.$magento.config.state.setLocale).toHaveBeenCalledWith('de_DE');
113122
expect(testCaseAppMock.$vsf.$magento.config.state.setStore).toHaveBeenCalledWith('de_DE');
123+
expect(testCaseAppMock.$vsf.$magento.config.state.setCurrency).toHaveBeenCalledWith('EUR');
114124
expect(callbackRequest.headers.cookie).toMatchInlineSnapshot(
115-
`"vsf-store=de_DE; vsf-locale=de_DE; vsf-currency=USD; vsf-country=PL; vsf-customer=12fg45; vsf-cart=123 "`
125+
'"vsf-store=de_DE; vsf-locale=de_DE; vsf-currency=EUR; vsf-country=PL; vsf-customer=12fg45; vsf-cart=123 "',
116126
);
117127
});
118128
});

packages/theme/plugins/i18n.ts

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,26 @@ const readStoreCookie = (app: NuxtAppOptions) => app.$cookies.get(cookieNames.st
1515
* Find locale code based on magento store code
1616
* @param storeCode {string} - magento store code
1717
* @param locales {array} - array with locales
18-
* @returns boolean
18+
* @returns string
1919
*/
2020
const findLocaleBasedOnStoreCode = (storeCode: string, locales: string[] | LocaleObject[]) => {
2121
if (locales.some((l) => typeof l !== 'string')) {
22-
return !!(locales as LocaleObject[]).find((locale) => locale.code === storeCode);
22+
return (locales as LocaleObject[]).find((locale) => locale.code === storeCode);
2323
}
2424

25-
return (locales as string[]).includes(storeCode);
25+
return (locales as string[]).find((locale) => locale === storeCode);
26+
};
27+
28+
/**
29+
* Find defaultCurrency code based on magento store code
30+
* @param storeCode {string} - magento store code
31+
* @param locales {array} - array with locales
32+
* @returns string
33+
*/
34+
const findCurrencyBasedOnStoreCode = (storeCode: string, locales: string[] | LocaleObject[]): string => {
35+
const match = (locales as LocaleObject[]).find((locale) => locale.code === storeCode);
36+
37+
return match.defaultCurrency;
2638
};
2739

2840
/**
@@ -39,15 +51,16 @@ const setDefaultLocale = async (i18n) => {
3951
*
4052
* @param apiState {ConfigState}
4153
* @param newStoreCode {string}
54+
* @param currency {string}
4255
* @returns {string}
4356
*/
44-
const prepareNewCookieString = (apiState: ConfigState, newStoreCode: string) => {
57+
const prepareNewCookieString = (apiState: ConfigState, newStoreCode: string, currency: string) => {
4558
const customerTokenCookie = apiState.getCustomerToken();
4659
const cartIdCookie = apiState.getCartId();
4760

4861
let cookie = `vsf-store=${newStoreCode}; `;
4962
cookie += `vsf-locale=${newStoreCode}; `;
50-
cookie += `vsf-currency=${apiState.getCurrency()}; `;
63+
cookie += `vsf-currency=${currency}; `;
5164
cookie += `vsf-country=${apiState.getCountry()}; `;
5265

5366
if (customerTokenCookie) {
@@ -61,11 +74,10 @@ const prepareNewCookieString = (apiState: ConfigState, newStoreCode: string) =>
6174
return cookie;
6275
};
6376

64-
export default async ({ app }: Context) => {
77+
export default async ({ app, route }: Context) => {
6578
await app.$vsf.$magento.client.interceptors.request.use(async (request) => {
6679
const { i18n } = app;
67-
const currentStoreCode = readStoreCookie(app);
68-
80+
const currentStoreCode = readStoreCookie(app) ?? route.path.split('/').find((element) => String(element));
6981
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
7082
if (!currentStoreCode || !findLocaleBasedOnStoreCode(currentStoreCode, i18n.locales)) {
7183
await setDefaultLocale(i18n);
@@ -74,16 +86,19 @@ export default async ({ app }: Context) => {
7486
}
7587

7688
const i18nCurrentLocaleCode = i18n.locale;
89+
7790
const localeCookie = app.$cookies.get(cookieNames.localeCookieName);
7891

7992
if (i18nCurrentLocaleCode !== localeCookie) {
8093
const apiState = app.$vsf.$magento.config.state as ConfigState;
94+
const currency = findCurrencyBasedOnStoreCode(i18nCurrentLocaleCode, i18n.locales);
8195

8296
apiState.setStore(i18nCurrentLocaleCode);
8397
apiState.setLocale(i18nCurrentLocaleCode);
98+
apiState.setCurrency(currency);
8499

85100
// eslint-disable-next-line no-param-reassign
86-
request.headers.cookie = prepareNewCookieString(apiState, i18nCurrentLocaleCode);
101+
request.headers.cookie = prepareNewCookieString(apiState, i18nCurrentLocaleCode, currency);
87102
}
88103

89104
return request;

0 commit comments

Comments
 (0)