-
Notifications
You must be signed in to change notification settings - Fork 6
/
store.js
115 lines (103 loc) · 4 KB
/
store.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import { createStore, compose, applyMiddleware } from 'redux';
import { syncHistoryWithStore, routerActions, routerMiddleware } from 'react-router-redux';
import thunk from 'redux-thunk';
import { browserHistory } from 'react-router';
import createLogger from 'redux-logger';
import rootReducer from './reducers';
import { UserAuthWrapper as userAuthWrapper } from 'redux-auth-wrapper';
import client from './apolloClient';
const isClient = typeof document !== 'undefined';
const isDeveloping = process.env.NODE_ENV !== 'production';
import { initialState as landing } from './containers/LandingContainer/reducer';
import { initialState as loginContainer } from './containers/LoginContainer/reducer';
import { initialState as signupContainer } from './containers/SignupContainer/reducer';
import { initialState as cmsEditorContainer } from './containers/CmsEditorContainer/reducer';
import { initialState as searchContainer } from './containers/SearchContainer/reducer';
import { initialState as userProfileContainer } from './containers/UserProfileContainer/reducer';
import { initialState as articleFeedContainer } from './containers/ArticleFeedContainer/reducer';
import { initialState as archiveContainer } from 'containers/ArticleArchiveContainer/reducer';
import { initialState as resetPassword } from 'containers/ResetPasswordContainer/reducer';
import {
initialState as singleArticleContainer,
} from './containers/SingleArticleContainer/reducer';
import {
initialState as contentDashboardContainer,
} from './containers/ContentDashboardContainer/reducer';
import { initialState as app } from './components/App/reducer';
import {
initialState as carouselWidgetContainer,
} from './containers/CarouselWidgetContainer/reducer';
const initialState = {
app,
landing,
loginContainer,
signupContainer,
cmsEditorContainer,
singleArticleContainer,
searchContainer,
contentDashboardContainer,
carouselWidgetContainer,
articleFeedContainer,
userProfileContainer,
archiveContainer,
resetPassword,
};
/* Commonly used middlewares and enhancers */
/* See: http://redux.js.org/docs/advanced/Middleware.html*/
const loggerMiddleware = createLogger();
const routingMiddleware = routerMiddleware(browserHistory);
const middlewares = [thunk, routingMiddleware, client.middleware()];
/*
if (isDeveloping) {
middlewares.push(loggerMiddleware);
}
*/
/* Everyone should use redux dev tools */
/* https://github.com/gaearon/redux-devtools */
/* https://medium.com/@meagle/understanding-87566abcfb7a */
const enhancers = [];
if (isClient && isDeveloping) {
const devToolsExtension = window.devToolsExtension;
if (typeof devToolsExtension === 'function') {
enhancers.push(devToolsExtension());
}
}
const composedEnhancers = compose(
applyMiddleware(...middlewares),
...enhancers
);
/* Hopefully by now you understand what a store is and how redux uses them,
* But if not, take a look at: https://github.com/reactjs/redux/blob/master/docs/api/createStore.md
* And https://egghead.io/lessons/javascript-redux-implementing-store-from-scratch
*/
const store = createStore(
rootReducer,
initialState,
composedEnhancers,
);
/* See: https://github.com/reactjs/react-router-redux/issues/305 */
export const history = isClient ?
syncHistoryWithStore(browserHistory, store) : undefined;
export const userIsAuthenticated = userAuthWrapper({
authSelector: state => state.app.user,
redirectAction: routerActions.replace,
failureRedirectPath: '/login',
wrapperDisplayName: 'userIsAuthenticated',
});
export const userIsAdmin = userAuthWrapper({
authSelector: state => state.app.user,
redirectAction: routerActions.replace,
failureRedirectPath: '/',
wrapperDisplayName: 'userIsAdmin',
predicate: user => user.role === 'admin',
});
/* Hot reloading of reducers. How futuristic!! */
if (module.hot) {
module.hot.accept('./reducers', () => {
/*eslint-disable */ // Allow require
const nextRootReducer = require('./reducers').default;
/*eslint-enable */
store.replaceReducer(nextRootReducer);
});
}
export default store;