-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathstore.js
44 lines (36 loc) · 1.3 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
import getConfig from 'next/config'
import { createStore, applyMiddleware } from 'redux'
import { composeWithDevTools } from 'redux-devtools-extension'
import logger from 'redux-logger'
import thunk from 'redux-thunk'
import { compareVersion } from './lib'
import reducer from './reducers'
const { publicRuntimeConfig } = getConfig()
export function makeStore(initialState, { isServer }) {
initialState = initialState || reducer()
const middlewares = [thunk]
if (publicRuntimeConfig.logReduxAction) {
middlewares.push(logger)
}
const enhancer = composeWithDevTools(applyMiddleware(...middlewares))
if (isServer) {
return createStore(reducer, initialState, enhancer)
} else {
const { persistReducer, persistStore } = require('redux-persist')
const storage = require('redux-persist/lib/storage').default
const persistedReducer = persistReducer({
key: 'jwpay',
whitelist: ['common', 'form', 'account'],
storage,
migrate: state => {
if (state && compareVersion(state.common.version, process.env.version, 2) !== 0) {
state = initialState
}
return Promise.resolve(state)
},
}, reducer)
const store = createStore(persistedReducer, initialState, enhancer)
store.__persistor = persistStore(store)
return store
}
}