Feature(langauge): Make client history language aware

Remove need for language aware links
This commit is contained in:
Berkeley Martinez
2016-06-20 21:01:14 -07:00
parent 1da593df26
commit 95aab958aa
11 changed files with 98 additions and 53 deletions

View File

@@ -10,6 +10,7 @@ import {
} from 'react-router-redux';
import { render } from 'redux-epic';
import { createHistory } from 'history';
import useLangRoutes from './use-lang-routes.js';
import createApp from '../common/app';
import provideStore from '../common/app/provide-store';
@@ -36,7 +37,7 @@ initialState.app.csrfToken = csrfToken;
const serviceOptions = { xhrPath: '/services', context: { _csrf: csrfToken } };
const history = createHistory();
const history = useLangRoutes(createHistory)();
const devTools = window.devToolsExtension ? window.devToolsExtension() : f => f;
const adjustUrlOnReplay = !!window.devToolsExtension;

44
client/use-lang-routes.js Normal file
View File

@@ -0,0 +1,44 @@
import { addLang, getLangFromPath } from '../common/app/utils/lang.js';
function addLangToLocation(location, lang) {
if (!location) {
return location;
}
if (typeof location === 'string') {
return addLang(location, lang);
}
return {
...location,
pathname: addLang(location.pathname, lang)
};
}
function getLangFromLocation(location) {
if (!location) {
return location;
}
if (typeof location === 'string') {
return getLangFromPath(location);
}
return getLangFromPath(location.pathname);
}
export default function useLangRoutes(createHistory) {
return (options = {}) => {
let lang = 'en';
const history = createHistory(options);
const unsubscribeFromHistory = history.listen(nextLocation => {
lang = getLangFromLocation(nextLocation);
});
const push = location => history.push(addLangToLocation(location, lang));
const replace = location => history.replace(
addLangToLocation(location, lang)
);
return {
...history,
push,
replace,
unsubscribe() { unsubscribeFromHistory(); }
};
};
}