2016-12-22 05:10:54 +00:00
|
|
|
import { createStore, applyMiddleware } from 'redux'
|
2017-05-25 16:37:10 +00:00
|
|
|
import { composeWithDevTools } from 'redux-devtools-extension'
|
2016-12-22 05:10:54 +00:00
|
|
|
import thunkMiddleware from 'redux-thunk'
|
|
|
|
|
2017-04-30 20:44:24 +00:00
|
|
|
const exampleInitialState = {
|
|
|
|
lastUpdate: 0,
|
|
|
|
light: false,
|
|
|
|
count: 0
|
|
|
|
}
|
|
|
|
|
|
|
|
export const actionTypes = {
|
2018-05-15 07:49:07 +00:00
|
|
|
TICK: 'TICK',
|
|
|
|
INCREMENT: 'INCREMENT',
|
|
|
|
DECREMENT: 'DECREMENT',
|
|
|
|
RESET: 'RESET'
|
2017-04-30 20:44:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// REDUCERS
|
|
|
|
export const reducer = (state = exampleInitialState, action) => {
|
2016-12-22 05:10:54 +00:00
|
|
|
switch (action.type) {
|
2017-04-30 20:44:24 +00:00
|
|
|
case actionTypes.TICK:
|
2018-05-15 07:49:07 +00:00
|
|
|
return Object.assign({}, state, {
|
|
|
|
lastUpdate: action.ts,
|
|
|
|
light: !!action.light
|
|
|
|
})
|
|
|
|
case actionTypes.INCREMENT:
|
2017-04-30 20:44:24 +00:00
|
|
|
return Object.assign({}, state, {
|
|
|
|
count: state.count + 1
|
|
|
|
})
|
2018-05-15 07:49:07 +00:00
|
|
|
case actionTypes.DECREMENT:
|
|
|
|
return Object.assign({}, state, {
|
|
|
|
count: state.count - 1
|
|
|
|
})
|
|
|
|
case actionTypes.RESET:
|
|
|
|
return Object.assign({}, state, {
|
|
|
|
count: exampleInitialState.count
|
|
|
|
})
|
2018-12-17 16:34:32 +00:00
|
|
|
default:
|
|
|
|
return state
|
2016-12-22 05:10:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-30 20:44:24 +00:00
|
|
|
// ACTIONS
|
2018-12-17 16:34:32 +00:00
|
|
|
export const serverRenderClock = isServer => dispatch => {
|
2017-04-30 20:44:24 +00:00
|
|
|
return dispatch({ type: actionTypes.TICK, light: !isServer, ts: Date.now() })
|
|
|
|
}
|
|
|
|
|
2018-05-07 13:03:54 +00:00
|
|
|
export const startClock = dispatch => {
|
|
|
|
return setInterval(() => {
|
|
|
|
// Dispatch `TICK` every 1 second
|
|
|
|
dispatch({ type: actionTypes.TICK, light: true, ts: Date.now() })
|
|
|
|
}, 1000)
|
2016-12-22 05:10:54 +00:00
|
|
|
}
|
|
|
|
|
2018-05-15 07:49:07 +00:00
|
|
|
export const incrementCount = () => dispatch => {
|
|
|
|
return dispatch({ type: actionTypes.INCREMENT })
|
|
|
|
}
|
|
|
|
|
|
|
|
export const decrementCount = () => dispatch => {
|
|
|
|
return dispatch({ type: actionTypes.DECREMENT })
|
|
|
|
}
|
|
|
|
|
|
|
|
export const resetCount = () => dispatch => {
|
|
|
|
return dispatch({ type: actionTypes.RESET })
|
2017-04-30 20:44:24 +00:00
|
|
|
}
|
|
|
|
|
2018-05-07 13:03:54 +00:00
|
|
|
export function initializeStore (initialState = exampleInitialState) {
|
2018-12-17 16:34:32 +00:00
|
|
|
return createStore(
|
|
|
|
reducer,
|
|
|
|
initialState,
|
|
|
|
composeWithDevTools(applyMiddleware(thunkMiddleware))
|
|
|
|
)
|
2016-12-22 05:10:54 +00:00
|
|
|
}
|