Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(i18n): changed i18n middleware to plugin and fix store switc… #425

Merged
merged 2 commits into from
Jan 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/.vuepress/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ module.exports = {
{
title: 'Reference',
children: [
['/middlewares/', 'Middlewares'],
['/plugins/', 'Plugins'],
['/api-reference/', 'API Reference'],
]
},
Expand Down
6 changes: 3 additions & 3 deletions docs/middlewares/index.md → docs/plugins/index.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Middlewares
# Plugins

Here you can find documentation for global Middlewares

## i18n

i18n middleware is responsible for handling store changes after URL changes.
i18n plugin is responsible for handling store changes after URL changes.

It checks if locale in i18n module is changes and update `vsf-store` and `vsf-locale` cookie to match new store on both
client and server side.
Expand All @@ -25,4 +25,4 @@ client and server side.
8. End


![i18n flow](./i18n-middleware-diagram.png)
![i18n flow](./i18n-plugin-diagram.png)
4 changes: 3 additions & 1 deletion packages/theme/nuxt.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,11 +227,13 @@ export default {
}
},
},
plugins: [
'~/plugins/i18n',
],
router: {
extendRoutes(routes) {
getRoutes(`${__dirname}/_theme`)
.forEach((route) => routes.unshift(route));
},
middleware: ['i18n'],
},
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import i18nMiddleware from '~/middleware/i18n';
import i18nPlugin from '~/plugins/i18n';

const localesMock = [
{
Expand All @@ -25,6 +25,10 @@ const apiStateMock = {
getCustomerToken: () => '12fg45',
};

const callbackRequest = {
headers: {},
};

const appMock = {
$cookies: {
get: jest.fn(),
Expand All @@ -36,6 +40,15 @@ const appMock = {
},
$vsf: {
$magento: {
client: {
interceptors: {
request: {
use: (callback) => {
callback(callbackRequest);
},
},
},
},
config: {
state: {
...apiStateMock,
Expand All @@ -52,20 +65,20 @@ const appMock = {
},
};

describe('i18n middleware', () => {
describe('i18n plugin', () => {
beforeEach(() => {
jest.resetAllMocks();
});

it('Should read vsf-store cookie value', () => {
i18nMiddleware({ app: appMock });
i18nPlugin({ app: appMock });

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

it('Should find locale based on magento store code', () => {
appMock.$cookies.get.mockReturnValue('default');
i18nMiddleware({ app: appMock });
i18nPlugin({ app: appMock });

expect(appMock.i18n.setLocale).not.toHaveBeenCalled();
});
Expand All @@ -78,7 +91,7 @@ describe('i18n middleware', () => {

it('Should set default locale when vsf-store cookie is not exist', () => {
appMock.$cookies.get.mockReturnValue(null);
i18nMiddleware({ app: appMock });
i18nPlugin({ app: appMock });

expect(appMock.i18n.setLocale).toHaveBeenCalledWith('en');
});
Expand All @@ -94,11 +107,11 @@ describe('i18n middleware', () => {

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

i18nMiddleware({ app: testCaseAppMock });
i18nPlugin({ app: testCaseAppMock });

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.axios.headers.cookie).toMatchInlineSnapshot(
expect(callbackRequest.headers.cookie).toMatchInlineSnapshot(
`"vsf-store=de_DE; vsf-locale=de_DE; vsf-currency=USD; vsf-country=PL; vsf-customer=12fg45; vsf-cart=123 "`
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,27 +53,32 @@ const prepareNewCookieString = (apiState, newStoreCode) => {
return cookie;
};

export default async ({ app }) => {
const { i18n } = app;
const currentStoreCode = readStoreCookie(app);
export default ({ app }) => {
app.$vsf.$magento.client.interceptors.request.use(async (request) => {

// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
if (!currentStoreCode || !findLocaleBasedOnStoreCode(currentStoreCode, i18n.locales)) {
await setDefaultLocale(i18n);
const { i18n } = app;
const currentStoreCode = readStoreCookie(app);

return;
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
if (!currentStoreCode || !findLocaleBasedOnStoreCode(currentStoreCode, i18n.locales)) {
await setDefaultLocale(i18n);

const i18nCurrentLocaleCode = i18n.locale;
const localeCookie = app.$cookies.get(cookieNames.localeCookieName);
return;
}

if (i18nCurrentLocaleCode !== localeCookie) {
const apiState = app.$vsf.$magento.config.state;
const i18nCurrentLocaleCode = i18n.locale;
const localeCookie = app.$cookies.get(cookieNames.localeCookieName);

apiState.setStore(i18nCurrentLocaleCode);
apiState.setLocale(i18nCurrentLocaleCode);
if (i18nCurrentLocaleCode !== localeCookie) {
const apiState = app.$vsf.$magento.config.state;

// eslint-disable-next-line no-param-reassign
app.$vsf.$magento.config.axios.headers.cookie = prepareNewCookieString(apiState, i18nCurrentLocaleCode);
}
apiState.setStore(i18nCurrentLocaleCode);
apiState.setLocale(i18nCurrentLocaleCode);

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

Frodigo marked this conversation as resolved.
Show resolved Hide resolved
return request;
});
};
Loading