Overal improvement
This commit is contained in:
parent
f11aec082d
commit
7125e35103
15 changed files with 199 additions and 86 deletions
|
|
@ -8,47 +8,61 @@ import { fetchLang } from './actions'
|
|||
|
||||
import Main from './main.jsx'
|
||||
|
||||
const logger = process.env.DEBUG ? store => next => action => {
|
||||
console.group(action.type)
|
||||
console.info('dispatching', action)
|
||||
let result = next(action)
|
||||
console.log('next state', store.getState())
|
||||
console.groupEnd()
|
||||
return result
|
||||
} : null
|
||||
export const init_app = () => {
|
||||
const logger = process.env.DEBUG ? store => next => action => {
|
||||
console.group(action.type)
|
||||
console.info('dispatching', action)
|
||||
let result = next(action)
|
||||
console.log('next state', store.getState())
|
||||
console.groupEnd()
|
||||
return result
|
||||
} : null
|
||||
|
||||
const thunk = store => next => action =>
|
||||
typeof action === 'function'
|
||||
? action(store.dispatch, store.getState)
|
||||
: next(action)
|
||||
const thunk = store => next => action =>
|
||||
typeof action === 'function'
|
||||
? action(store.dispatch, store.getState)
|
||||
: next(action)
|
||||
|
||||
const persistedState = {
|
||||
theme: localStorage.getItem('theme') || themeReducer(undefined, {type: null}),
|
||||
lang: localStorage.getItem('lang') ?
|
||||
{...langReducer(undefined, {type: null}), lang: localStorage.getItem('lang')} :
|
||||
langReducer(undefined, {type: null})
|
||||
const persistedState = {
|
||||
theme: localStorage.getItem('theme') || themeReducer(undefined, {type: null}),
|
||||
lang: localStorage.getItem('lang') ?
|
||||
{...langReducer(undefined, {type: null}), lang: localStorage.getItem('lang')} :
|
||||
langReducer(undefined, {type: null})
|
||||
}
|
||||
//const initState = persistedState ? JSON.parse(persistedState) : {}
|
||||
|
||||
const reducers = combineReducers({
|
||||
theme: themeReducer,
|
||||
lang: langReducer
|
||||
})
|
||||
|
||||
const store = createStore(
|
||||
reducers,
|
||||
persistedState,
|
||||
process.env.DEBUG ? applyMiddleware(thunk, logger) : applyMiddleware(thunk))
|
||||
|
||||
let savedState = {
|
||||
theme: store.getState().theme,
|
||||
lang: store.getState().lang.lang
|
||||
}
|
||||
store.subscribe(() => {
|
||||
const state = store.getState()
|
||||
if(savedState.theme !== state.theme)
|
||||
{
|
||||
savedState.theme = state.theme
|
||||
localStorage.setItem('theme', state.theme)
|
||||
}
|
||||
if(savedState.lang !== state.lang.lang)
|
||||
{
|
||||
savedState.lang = state.lang.lang
|
||||
localStorage.setItem('lang', state.lang.lang)
|
||||
}
|
||||
})
|
||||
|
||||
return store
|
||||
}
|
||||
//const initState = persistedState ? JSON.parse(persistedState) : {}
|
||||
|
||||
const reducers = combineReducers({
|
||||
theme: themeReducer,
|
||||
lang: langReducer
|
||||
})
|
||||
|
||||
const store = createStore(
|
||||
reducers,
|
||||
persistedState,
|
||||
process.env.DEBUG ? applyMiddleware(thunk, logger) : applyMiddleware(thunk))
|
||||
|
||||
let savedState = {
|
||||
theme: store.getState().theme,
|
||||
lang: store.getState().lang.lang
|
||||
}
|
||||
store.subscribe(() => {
|
||||
const state = store.getState()
|
||||
if(savedState.theme !== state.theme) localStorage.setItem('theme', store.getState().theme)
|
||||
if(savedState.lang !== state.lang.lang) localStorage.setItem('lang', store.getState().lang.lang)
|
||||
})
|
||||
const store = init_app()
|
||||
|
||||
store.dispatch(fetchLang(store.getState().lang.lang)).then(() => {
|
||||
render(
|
||||
|
|
@ -56,4 +70,4 @@ store.dispatch(fetchLang(store.getState().lang.lang)).then(() => {
|
|||
<Main />
|
||||
</Provider>,
|
||||
document.getElementById("root"))
|
||||
})
|
||||
})
|
||||
Loading…
Add table
Add a link
Reference in a new issue