Pass actions to thunk acitons

This commit is contained in:
Samy Pesse
2016-10-02 15:28:25 +02:00
parent 5abac3a79a
commit cd2d5e5101
5 changed files with 49 additions and 7 deletions
@@ -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'
};
@@ -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
};
+10 -2
View File
@@ -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
@@ -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;
@@ -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(