update state correctly. save to server correctly

This commit is contained in:
Riley Davis
2016-11-08 16:16:27 -07:00
parent 2823232c12
commit ff52714f27
3 changed files with 12 additions and 16 deletions
+2 -3
View File
@@ -49,12 +49,11 @@ export const fetchSettings = () => dispatch => {
}
export const updateSettings = settings => {
console.log('updated settings', settings)
return {type: SETTINGS_UPDATED, settings}
}
export const saveSettingsToServer = (dispatch, getState) => {
const settings = getState().settings.settings
export const saveSettingsToServer = () => (dispatch, getState) => {
const settings = getState().settings.toJS().settings
dispatch({type: SAVE_SETTINGS_LOADING})
fetch(`${base}/settings`, getInit('PUT', settings))
.then(handleResp)
@@ -113,10 +113,6 @@ class Configure extends React.Component {
}
}
const mapStateToProps = state => {
return {
settings: state.settings.toJS()
}
}
const mapStateToProps = state => state.settings.toJS()
export default connect(mapStateToProps)(Configure)
+9 -8
View File
@@ -12,9 +12,9 @@ const initialState = Map({
export default (state = initialState, action) => {
switch (action.type) {
case types.SETTINGS_LOADING: return state.set('fetchingSettings', true).set('fetchSettingsError', null)
case types.SETTINGS_RECEIVED: return receivedSettings(state, action)
case types.SETTINGS_RECEIVED: return updateSettings(state, action)
case types.SETTINGS_FETCH_ERROR: return settingsFetchFailed(state, action)
case types.SETTINGS_UPDAETD: return state.get('settings').merge(action.settings)
case types.SETTINGS_UPDATED: return updateSettings(state, action)
case types.SAVE_SETTINGS_LOADING: return state.set('fetchingSettings', true).set('saveSettingsError', null)
case types.SAVE_SETTINGS_SUCCESS: return saveComplete(state, action)
case types.SAVE_SETTINGS_FAILED: return settingsSaveFailed(state, action)
@@ -22,15 +22,16 @@ export default (state = initialState, action) => {
}
}
const receivedSettings = (state, action) => {
state.set('fetchingSettings', false).set('fetchSettingsError', null)
return state.get('settings').merge(action.settings)
const updateSettings = (state, action) => {
const s = state.set('fetchingSettings', false).set('fetchSettingsError', null)
const settings = s.get('settings').merge(action.settings)
return s.set('settings', settings)
}
const saveComplete = (state, action) => {
state.set('fetchingSettings', false).set('saveSettingsError', null)
console.log('saveComplete', action.settings)
return state.get('settings').merge(action.settings)
const s = state.set('fetchingSettings', false).set('saveSettingsError', null)
const settings = s.get('settings').merge(action.settings)
return s.set('settings', settings)
}
const settingsFetchFailed = (state, action) => {