Define `createReducer` helper
Copy const createReducer = (initialState = {}, handlers = {}) => (state, action) => {
const nextState = produce(state, draft => {
if (handlers[action.type]) {
return handlers[action.type](draft, action)
}
})
return nextState
}
Copy const initialState = {
locale: 'en',
theme: 'light',
}
Copy const types = new Proxy({
CHANGE_LOCALE: 'CHANGE_LOCALE',
CHANGE_THEME: 'CHANGE_THEME',
}, {
get: (target, prop) => `app_name/domain_namespace/${target[prop]}`,
})
Copy const actions = {
changeLocale: newLocale => ({ type: types.CHANGE_LOCALE, payload: newLocale }),
changeTheme: newTheme => ({ type: types.CHANGE_THEME, payload: newTheme }),
}
Copy const reducer = createReducer(initialState, {
[types.CHANGE_LOCALE]: (draft, { payload }) => {
draft.locale = payload
},
[types.CHANGE_THEME]: (draft, { payload }) => {
draft.theme = payload
},
})
Copy const selectors = {
selectApp: state => state.app || initialState,
makeSelectLocale() {
return createSelector(this.selectApp, ({ locale }) => locale)
},
makeSelectTheme() {
return createSelector(this.selectApp, ({ theme }) => theme)
},
}