From 5a3c0f511fdf582fb8c8445a96283750b23d3dc1 Mon Sep 17 00:00:00 2001 From: ivan Date: Sat, 13 Aug 2016 18:36:57 -0300 Subject: [PATCH] Ivan - Create system fixtures and fix language session issues [skip ci] --- client/src/actions/config-actions.js | 22 +++++++++++++++++++++ client/src/app/index.js | 2 ++ client/src/app/main/main-layout-header.js | 11 ++++++++++- client/src/core-components/drop-down.js | 2 +- client/src/data/fixtures/system-fixtures.js | 15 ++++++++++++++ client/src/lib-app/fixtures-loader.js | 1 + client/src/lib-app/session-store.js | 16 +++++++++++++++ client/src/reducers/config-reducer.js | 9 ++++++++- client/src/reducers/session-reducer.js | 4 ++-- 9 files changed, 77 insertions(+), 5 deletions(-) create mode 100644 client/src/data/fixtures/system-fixtures.js diff --git a/client/src/actions/config-actions.js b/client/src/actions/config-actions.js index c79ad6ad..70c41c46 100644 --- a/client/src/actions/config-actions.js +++ b/client/src/actions/config-actions.js @@ -1,4 +1,26 @@ +import API from 'lib-app/api-call'; +import sessionStore from 'lib-app/session-store'; + export default { + init() { + if (sessionStore.areConfigsStored()) { + return { + type: 'INIT_CONFIGS_FULFILLED', + payload: { + data: sessionStore.getConfigs() + } + }; + } else { + return { + type: 'INIT_CONFIGS', + payload: API.call({ + path: '/system/get-configs', + data: {} + }) + }; + } + }, + changeLanguage(newLanguage) { return { type: 'CHANGE_LANGUAGE', diff --git a/client/src/app/index.js b/client/src/app/index.js index ace1beec..5d686499 100644 --- a/client/src/app/index.js +++ b/client/src/app/index.js @@ -3,6 +3,7 @@ import {render} from 'react-dom' import { Provider } from 'react-redux'; import SessionActions from 'actions/session-actions'; +import ConfigActions from 'actions/config-actions'; import routes from './Routes'; import store from './store'; @@ -19,6 +20,7 @@ let renderApplication = function () { render({routes}, document.getElementById('app')); }; window.store = store; +store.dispatch(ConfigActions.init()); store.dispatch(SessionActions.initSession()); let unsubscribe = store.subscribe(() => { diff --git a/client/src/app/main/main-layout-header.js b/client/src/app/main/main-layout-header.js index 06879a3b..b6897723 100644 --- a/client/src/app/main/main-layout-header.js +++ b/client/src/app/main/main-layout-header.js @@ -24,7 +24,7 @@ class MainLayoutHeader extends React.Component { return (
{this.renderAccessLinks()} - +
); } @@ -51,6 +51,15 @@ class MainLayoutHeader extends React.Component { return result; } + getLanguageSelectorProps() { + return { + className: 'main-layout-header--languages', + items: this.getLanguageList(), + selectedIndex: Object.values(codeLanguages).indexOf(this.props.config.language), + onChange: this.changeLanguage.bind(this) + }; + } + getLanguageList() { return Object.keys(codeLanguages).map((language) => { return { diff --git a/client/src/core-components/drop-down.js b/client/src/core-components/drop-down.js index 38e85d48..969bc589 100644 --- a/client/src/core-components/drop-down.js +++ b/client/src/core-components/drop-down.js @@ -62,7 +62,7 @@ class DropDown extends React.Component { items: this.props.items, onItemClick: this.handleItemClick.bind(this), onMouseDown: this.handleListMouseDown.bind(this), - selectedIndex: this.state.selectedIndex + selectedIndex: this.getSelectedIndex() }; return ( diff --git a/client/src/data/fixtures/system-fixtures.js b/client/src/data/fixtures/system-fixtures.js new file mode 100644 index 00000000..c74613f9 --- /dev/null +++ b/client/src/data/fixtures/system-fixtures.js @@ -0,0 +1,15 @@ +module.exports = [ + { + path: '/system/get-configs', + time: 1000, + response: function () { + return { + status: 'success', + data: { + 'language': 'us', + 'reCaptchaKey': '6LfM5CYTAAAAAGLz6ctpf-hchX2_l0Ge-Bn-n8wS' + } + }; + } + } +]; diff --git a/client/src/lib-app/fixtures-loader.js b/client/src/lib-app/fixtures-loader.js index 113c8d9d..8cbb9dbd 100644 --- a/client/src/lib-app/fixtures-loader.js +++ b/client/src/lib-app/fixtures-loader.js @@ -17,6 +17,7 @@ let fixtures = (function () { // FIXTURES fixtures.add(require('data/fixtures/user-fixtures')); +fixtures.add(require('data/fixtures/system-fixtures')); _.each(fixtures.getAll(), function (fixture) { mockjax({ diff --git a/client/src/lib-app/session-store.js b/client/src/lib-app/session-store.js index bb05c992..a243c9fd 100644 --- a/client/src/lib-app/session-store.js +++ b/client/src/lib-app/session-store.js @@ -37,6 +37,22 @@ class SessionStore { this.setItem('rememberData-expiration', expiration); } + storeConfigs(configs) { + this.setItem('language', configs.language); + this.setItem('reCaptchaKey', configs.reCaptchaKey); + } + + getConfigs() { + return { + language: this.getItem('language'), + reCaptchaKey: this.getItem('reCaptchaKey') + }; + } + + areConfigsStored() { + return !!this.getItem('reCaptchaKey'); + } + isRememberDataExpired() { let rememberData = this.getRememberData(); diff --git a/client/src/reducers/config-reducer.js b/client/src/reducers/config-reducer.js index 4fcf57a5..832ec6ea 100644 --- a/client/src/reducers/config-reducer.js +++ b/client/src/reducers/config-reducer.js @@ -13,7 +13,8 @@ class ConfigReducer extends Reducer { getTypeHandlers() { return { - 'CHANGE_LANGUAGE': this.onLanguageChange + 'CHANGE_LANGUAGE': this.onLanguageChange, + 'INIT_CONFIGS_FULFILLED': this.onInitConfigs }; } @@ -24,6 +25,12 @@ class ConfigReducer extends Reducer { language: payload }); } + + onInitConfigs(state, payload) { + sessionStore.storeConfigs(payload.data); + + return _.extend({}, state, payload.data); + } } export default ConfigReducer.getInstance(); \ No newline at end of file diff --git a/client/src/reducers/session-reducer.js b/client/src/reducers/session-reducer.js index daa59f6a..42dbe9e2 100644 --- a/client/src/reducers/session-reducer.js +++ b/client/src/reducers/session-reducer.js @@ -91,9 +91,9 @@ class SessionReducer extends Reducer { userId: resultData.userId, expiration: resultData.rememberExpiration }); - } else { - sessionStore.createSession(resultData.userId, resultData.token); } + + sessionStore.createSession(resultData.userId, resultData.token); } }