From ef38b086cdd908fd9c334be322d6458ea2627552 Mon Sep 17 00:00:00 2001 From: Qolzam Date: Thu, 10 May 2018 10:44:10 +0700 Subject: [PATCH] [Enhancement] Move localization to redux-saga (#54). --- src/constants/globalActionType.ts | 3 ++- src/index.tsx | 24 ++++++++++---------- src/store/actions/globalActions.ts | 11 ++++++++++ src/store/actions/localeActions.ts | 23 -------------------- src/store/sagas/localeSaga.ts | 35 ++++++++++++++++++++++++++++++ src/store/sagas/rootSaga.ts | 5 ++++- tslint.json | 2 +- yarn.lock | 32 +++++---------------------- 8 files changed, 70 insertions(+), 65 deletions(-) create mode 100644 src/store/sagas/localeSaga.ts diff --git a/src/constants/globalActionType.ts b/src/constants/globalActionType.ts index 26de92d..3fe90df 100644 --- a/src/constants/globalActionType.ts +++ b/src/constants/globalActionType.ts @@ -17,6 +17,7 @@ export enum GlobalActionType { TEMP = 'TEMP', CLEAR_ALL_GLOBAL = 'CLEAR_ALL_GLOBAL', OPEN_POST_WRITE = 'OPEN_POST_WRITE', - CLOSE_POST_WRITE = 'CLOSE_POST_WRITE' + CLOSE_POST_WRITE = 'CLOSE_POST_WRITE', + INIT_LOCALE = 'INIT_LOCALE' } \ No newline at end of file diff --git a/src/index.tsx b/src/index.tsx index d12cf5a..3660ab4 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -14,17 +14,28 @@ import { ConnectedRouter } from 'react-router-redux' // - Actions import * as localeActions from 'store/actions/localeActions' +import * as globalActions from 'store/actions/globalActions' // - Import app components import Master from 'containers/master' // import { App } from 'components/AWS' +// App css +import './styles/app.css' + +/** + * Execute startup functions + */ +import './socialEngine' +import rootSaga from 'store/sagas/rootSaga' + +configureStore.runSaga(rootSaga) // Set default data // tslint:disable-next-line:no-empty configureStore.store.subscribe(() => { }) // - Initialize languages -configureStore.store.dispatch(localeActions.initTranslation()) +configureStore.store.dispatch(globalActions.initLocale()) // Needed for onClick // http://stackoverflow.com/a/34015469/988941 @@ -37,17 +48,6 @@ const theme = createMuiTheme({ } }) -// App css -import './styles/app.css' - -/** - * Execute startup functions - */ -import './socialEngine' -import rootSaga from 'store/sagas/rootSaga' - -configureStore.runSaga(rootSaga) - const supportsHistory = 'pushState' in window.history ReactDOM.render( diff --git a/src/store/actions/globalActions.ts b/src/store/actions/globalActions.ts index c24b14c..fc8a1be 100644 --- a/src/store/actions/globalActions.ts +++ b/src/store/actions/globalActions.ts @@ -78,6 +78,17 @@ export const progressChange = (percent: number, visible: Boolean) => { } +/** + * Progress change + */ +export const initLocale = () => { + return { + type: GlobalActionType.INIT_LOCALE, + payload: null + } + +} + /** * Default data loaded status will be true */ diff --git a/src/store/actions/localeActions.ts b/src/store/actions/localeActions.ts index 06ff9f9..b5c66f9 100644 --- a/src/store/actions/localeActions.ts +++ b/src/store/actions/localeActions.ts @@ -4,29 +4,6 @@ import { setActiveLanguage } from 'react-localize-redux' import { LanguageType } from 'store/reducers/locale/langugeType' import config from 'src/config' -/** - * Initialize translation - */ -export const initTranslation = () => { - return (dispatch: Function , getState: Function) => { - // - Intialize language - const languages = [ - { name: 'English', code: LanguageType.English }, - { name: 'French', code: LanguageType.French }, - { name: 'Spanish', code: LanguageType.Spanish } - ] - dispatch(initialize(languages)) - - // To set a different default active language set the `defaultLanguage` option. - dispatch(initialize(languages, { defaultLanguage: config.settings.defaultLanguage })) - const englishLocale = require('locale/en.json') - const spanishLocale = require('locale/es.json') - dispatch(addTranslationForLanguage(englishLocale, LanguageType.English)) - dispatch(addTranslationForLanguage(spanishLocale, LanguageType.Spanish)) - - } -} - /** * Set active language for translation */ diff --git a/src/store/sagas/localeSaga.ts b/src/store/sagas/localeSaga.ts new file mode 100644 index 0000000..c188026 --- /dev/null +++ b/src/store/sagas/localeSaga.ts @@ -0,0 +1,35 @@ + +import { take, fork, select, put, call, cancelled, all, takeLatest } from 'redux-saga/effects' +import { eventChannel, Channel } from 'redux-saga' +import { initialize } from 'react-localize-redux' +import { addTranslationForLanguage } from 'react-localize-redux' +import { setActiveLanguage } from 'react-localize-redux' +import { LanguageType } from 'store/reducers/locale/langugeType' +import config from 'src/config' +import { GlobalActionType } from 'constants/globalActionType' +/***************************** Subroutines ************************************/ + +/** + * Initialize localization + */ +function* initLocalization() { + debugger + const languages = [ + { name: 'English', code: LanguageType.English }, + { name: 'French', code: LanguageType.French }, + { name: 'Spanish', code: LanguageType.Spanish } + ] + yield put(initialize(languages)) + // To set a different default active language set the `defaultLanguage` option. + yield put(initialize(languages, { defaultLanguage: config.settings.defaultLanguage })) + const englishLocale = require('locale/en.json') + const spanishLocale = require('locale/es.json') + yield put(addTranslationForLanguage(englishLocale, LanguageType.English)) + yield put(addTranslationForLanguage(spanishLocale, LanguageType.Spanish)) +} + +export default function* localeSaga() { + yield all([ + takeLatest(GlobalActionType.INIT_LOCALE, initLocalization) + ]) +} diff --git a/src/store/sagas/rootSaga.ts b/src/store/sagas/rootSaga.ts index afea681..9d339dd 100644 --- a/src/store/sagas/rootSaga.ts +++ b/src/store/sagas/rootSaga.ts @@ -1,9 +1,12 @@ import { all, fork } from 'redux-saga/effects' import commentSaga from './commentSaga' +import localeSaga from './localeSaga' export default function* root() { + debugger yield all([ - commentSaga() + localeSaga(), + commentSaga(), ]) } \ No newline at end of file diff --git a/tslint.json b/tslint.json index ee9aaa1..89fafaf 100644 --- a/tslint.json +++ b/tslint.json @@ -45,7 +45,7 @@ ], "no-consecutive-blank-lines": true, "no-construct": true, - "no-debugger": true, + "no-debugger": false, "no-duplicate-variable": true, "no-empty": false, "no-eval": true, diff --git a/yarn.lock b/yarn.lock index e8d40dc..664fbdf 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2378,7 +2378,7 @@ debug@3.1.0, debug@^3.1.0: dependencies: ms "2.0.0" -debuglog@*, debuglog@^1.0.1: +debuglog@^1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/debuglog/-/debuglog-1.0.1.tgz#aa24ffb9ac3df9a2351837cfb2d279360cd78492" @@ -4049,7 +4049,7 @@ import-local@^1.0.0: pkg-dir "^2.0.0" resolve-cwd "^2.0.0" -imurmurhash@*, imurmurhash@^0.1.4: +imurmurhash@^0.1.4: version "0.1.4" resolved "https://registry.yarnpkg.com/imurmurhash/-/imurmurhash-0.1.4.tgz#9218b9b2b928a238b13dc4fb6b6d576f231453ea" @@ -5242,10 +5242,6 @@ lodash-es@^4.17.4, lodash-es@^4.17.5, lodash-es@^4.2.1: version "4.17.8" resolved "https://registry.yarnpkg.com/lodash-es/-/lodash-es-4.17.8.tgz#6fa8c8c5d337481df0bdf1c0d899d42473121e45" -lodash._baseindexof@*: - version "3.1.0" - resolved "https://registry.yarnpkg.com/lodash._baseindexof/-/lodash._baseindexof-3.1.0.tgz#fe52b53a1c6761e42618d654e4a25789ed61822c" - lodash._baseuniq@~4.6.0: version "4.6.0" resolved "https://registry.yarnpkg.com/lodash._baseuniq/-/lodash._baseuniq-4.6.0.tgz#0ebb44e456814af7905c6212fa2c9b2d51b841e8" @@ -5253,25 +5249,11 @@ lodash._baseuniq@~4.6.0: lodash._createset "~4.0.0" lodash._root "~3.0.0" -lodash._bindcallback@*: - version "3.0.1" - resolved "https://registry.yarnpkg.com/lodash._bindcallback/-/lodash._bindcallback-3.0.1.tgz#e531c27644cf8b57a99e17ed95b35c748789392e" - -lodash._cacheindexof@*: - version "3.0.2" - resolved "https://registry.yarnpkg.com/lodash._cacheindexof/-/lodash._cacheindexof-3.0.2.tgz#3dc69ac82498d2ee5e3ce56091bafd2adc7bde92" - -lodash._createcache@*: - version "3.1.2" - resolved "https://registry.yarnpkg.com/lodash._createcache/-/lodash._createcache-3.1.2.tgz#56d6a064017625e79ebca6b8018e17440bdcf093" - dependencies: - lodash._getnative "^3.0.0" - lodash._createset@~4.0.0: version "4.0.3" resolved "https://registry.yarnpkg.com/lodash._createset/-/lodash._createset-4.0.3.tgz#0f4659fbb09d75194fa9e2b88a6644d363c9fe26" -lodash._getnative@*, lodash._getnative@^3.0.0: +lodash._getnative@^3.0.0: version "3.9.1" resolved "https://registry.yarnpkg.com/lodash._getnative/-/lodash._getnative-3.9.1.tgz#570bc7dede46d61cdcde687d65d3eecbaa3aaff5" @@ -5341,10 +5323,6 @@ lodash.mergewith@^4.6.0: version "4.6.1" resolved "https://registry.yarnpkg.com/lodash.mergewith/-/lodash.mergewith-4.6.1.tgz#639057e726c3afbdb3e7d42741caa8d6e4335927" -lodash.restparam@*: - version "3.6.1" - resolved "https://registry.yarnpkg.com/lodash.restparam/-/lodash.restparam-3.6.1.tgz#936a4e309ef330a7645ed4145986c85ae5b20805" - lodash.sortby@^4.7.0: version "4.7.0" resolved "https://registry.yarnpkg.com/lodash.sortby/-/lodash.sortby-4.7.0.tgz#edd14c824e2cc9c1e0b0a1b42bb5210516a42438" @@ -7694,7 +7672,7 @@ readable-stream@~1.1.10: isarray "0.0.1" string_decoder "~0.10.x" -readdir-scoped-modules@*, readdir-scoped-modules@^1.0.0: +readdir-scoped-modules@^1.0.0: version "1.0.2" resolved "https://registry.yarnpkg.com/readdir-scoped-modules/-/readdir-scoped-modules-1.0.2.tgz#9fafa37d286be5d92cbaebdee030dc9b5f406747" dependencies: @@ -9504,7 +9482,7 @@ uuid@^3.0.0, uuid@^3.0.1, uuid@^3.1.0, uuid@^3.2.1: version "3.2.1" resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.2.1.tgz#12c528bb9d58d0b9265d9a2f6f0fe8be17ff1f14" -validate-npm-package-license@*, validate-npm-package-license@^3.0.1: +validate-npm-package-license@^3.0.1: version "3.0.3" resolved "https://registry.yarnpkg.com/validate-npm-package-license/-/validate-npm-package-license-3.0.3.tgz#81643bcbef1bdfecd4623793dc4648948ba98338" dependencies: