diff options
author | Samy Pesse <samypesse@gmail.com> | 2016-10-02 15:28:25 +0200 |
---|---|---|
committer | Samy Pesse <samypesse@gmail.com> | 2016-10-02 15:28:25 +0200 |
commit | cd2d5e5101edb466b13ada19b09ea42ef726ad96 (patch) | |
tree | 108b4aaf2c87f60a790e318ecaaa64eeecf59062 | |
parent | 5abac3a79a01698d69c3273a3f1398b63012a4b8 (diff) | |
download | gitbook-cd2d5e5101edb466b13ada19b09ea42ef726ad96.zip gitbook-cd2d5e5101edb466b13ada19b09ea42ef726ad96.tar.gz gitbook-cd2d5e5101edb466b13ada19b09ea42ef726ad96.tar.bz2 |
Pass actions to thunk acitons
-rw-r--r-- | packages/gitbook-core/src/actions/TYPES.js | 2 | ||||
-rw-r--r-- | packages/gitbook-core/src/actions/navigation.js | 22 | ||||
-rw-r--r-- | packages/gitbook-core/src/lib/createContext.js | 12 | ||||
-rw-r--r-- | packages/gitbook-core/src/reducers/navigation.js | 17 | ||||
-rw-r--r-- | packages/gitbook-plugin-search/src/actions/search.js | 3 |
5 files changed, 49 insertions, 7 deletions
diff --git a/packages/gitbook-core/src/actions/TYPES.js b/packages/gitbook-core/src/actions/TYPES.js index e758a44..3177dbd 100644 --- a/packages/gitbook-core/src/actions/TYPES.js +++ b/packages/gitbook-core/src/actions/TYPES.js @@ -7,6 +7,8 @@ module.exports = { PAGE_FETCH_START: 'navigation/fetch:start', PAGE_FETCH_END: 'navigation/fetch:end', PAGE_FETCH_ERROR: 'navigation/fetch:error', + PAGE_UPDATE_ANCHOR: 'navigation/update:anchor', + PAGE_UPDATE_QUERY: 'navigation/update:query', // i18n I18N_REGISTER_LOCALE: 'i18n/register:locale' }; diff --git a/packages/gitbook-core/src/actions/navigation.js b/packages/gitbook-core/src/actions/navigation.js index 60be593..f13e878 100644 --- a/packages/gitbook-core/src/actions/navigation.js +++ b/packages/gitbook-core/src/actions/navigation.js @@ -123,8 +123,28 @@ function fetchArticle(article) { return fetchPage(article.path); } +/** + * Update anchor for current page + * @param {String} anchor + * @return {Action} action + */ +function updateAnchor(anchor) { + return { type: ACTION_TYPES.PAGE_UPDATE_ANCHOR, anchor }; +} + +/** + * Update query for current page + * @param {Object|Map} query + * @return {Action} action + */ +function updateQuery(query) { + return { type: ACTION_TYPES.PAGE_UPDATE_QUERY, query }; +} + module.exports = { pushURI, fetchPage, - fetchArticle + fetchArticle, + updateAnchor, + updateQuery }; diff --git a/packages/gitbook-core/src/lib/createContext.js b/packages/gitbook-core/src/lib/createContext.js index d19e0f6..ec240be 100644 --- a/packages/gitbook-core/src/lib/createContext.js +++ b/packages/gitbook-core/src/lib/createContext.js @@ -11,6 +11,8 @@ const Components = require('../actions/components'); const I18n = require('../actions/i18n'); const Navigation = require('../actions/navigation'); +const isBrowser = (typeof window !== 'undefined'); + const corePlugin = new Plugin({ reduce: coreReducers, actions: { @@ -38,13 +40,19 @@ function createContext(plugins, initialState) { return Object.assign(accu, plugin.actions); }, {}); + // Create thunk middleware which include actions + const thunk = ReduxThunk.withExtraArgument(actions); + + // Create the redux store const store = Redux.createStore( (state, action) => { - console.log('[store]', action.type); + if (isBrowser) { + console.log('[store]', action.type); + } return reducer(state, action); }, initialState, - Redux.compose(Redux.applyMiddleware(ReduxThunk)) + Redux.compose(Redux.applyMiddleware(thunk)) ); // Initialize the plugins diff --git a/packages/gitbook-core/src/reducers/navigation.js b/packages/gitbook-core/src/reducers/navigation.js index a895186..625adef 100644 --- a/packages/gitbook-core/src/reducers/navigation.js +++ b/packages/gitbook-core/src/reducers/navigation.js @@ -1,9 +1,15 @@ -const { Record } = require('immutable'); +const { Record, Map } = require('immutable'); const ACTION_TYPES = require('../actions/TYPES'); const NavigationState = Record({ - loading: false, - error: null + // Are we loading a new page + loading: Boolean(false), + // Did we fail loading a page? + error: null, + // Query string + query: Map(), + // Current anchor + anchor: String('') }); function reduceNavigation(state, action) { @@ -26,6 +32,11 @@ function reduceNavigation(state, action) { error: action.error }); + case ACTION_TYPES.PAGE_UPDATE_ANCHOR: + return state.merge({ + anchor: action.anchor + }); + default: return state; diff --git a/packages/gitbook-plugin-search/src/actions/search.js b/packages/gitbook-plugin-search/src/actions/search.js index 52afc3c..7d42917 100644 --- a/packages/gitbook-plugin-search/src/actions/search.js +++ b/packages/gitbook-plugin-search/src/actions/search.js @@ -12,9 +12,10 @@ function query(q) { return clear(); } - return (dispatch, getState) => { + return (dispatch, getState, { Navigation }) => { const { handlers } = getState().search; + dispatch(Navigation.updateQuery({ q })); dispatch({ type: TYPES.START, query: q }); return Promise.reduce( |