2020-01-09 16:31:49 +01:00
|
|
|
import { createStore, applyMiddleware, combineReducers } from 'redux'
|
2020-05-20 14:22:10 +02:00
|
|
|
import { HYDRATE, createWrapper } from 'next-redux-wrapper'
|
2020-01-09 16:31:49 +01:00
|
|
|
import thunkMiddleware from 'redux-thunk'
|
|
|
|
import count from './count/reducer'
|
|
|
|
import tick from './tick/reducer'
|
|
|
|
|
2020-05-18 21:24:37 +02:00
|
|
|
const bindMiddleware = (middleware) => {
|
2020-01-09 16:31:49 +01:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
const { composeWithDevTools } = require('redux-devtools-extension')
|
|
|
|
return composeWithDevTools(applyMiddleware(...middleware))
|
|
|
|
}
|
|
|
|
return applyMiddleware(...middleware)
|
|
|
|
}
|
|
|
|
|
2020-05-20 14:22:10 +02:00
|
|
|
const combinedReducer = combineReducers({
|
|
|
|
count,
|
|
|
|
tick,
|
|
|
|
})
|
|
|
|
|
|
|
|
const reducer = (state, action) => {
|
|
|
|
if (action.type === HYDRATE) {
|
|
|
|
const nextState = {
|
|
|
|
...state, // use previous state
|
|
|
|
...action.payload, // apply delta from hydration
|
|
|
|
}
|
2020-09-24 21:35:46 +02:00
|
|
|
if (state.count.count) nextState.count.count = state.count.count // preserve count value on client side navigation
|
2020-05-20 14:22:10 +02:00
|
|
|
return nextState
|
|
|
|
} else {
|
|
|
|
return combinedReducer(state, action)
|
|
|
|
}
|
2020-01-09 16:31:49 +01:00
|
|
|
}
|
2020-05-20 14:22:10 +02:00
|
|
|
|
|
|
|
const initStore = () => {
|
|
|
|
return createStore(reducer, bindMiddleware([thunkMiddleware]))
|
|
|
|
}
|
|
|
|
|
|
|
|
export const wrapper = createWrapper(initStore)
|