Created
September 14, 2016 05:50
-
-
Save chengjianhua/ac2542153092a45597e11425a88ec2d9 to your computer and use it in GitHub Desktop.
update list item in immutable list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {List, Map} from 'immutable'; | |
import {ActionTypes} from '../constants/Constants'; | |
const defaultState = new Map({ | |
dataList: new Map({ | |
currentAccount: '', | |
currentPath: new List(), | |
data: new List(), | |
isLoading: false | |
}), | |
accountNames: new List() | |
}); | |
function fetchDataList(state, action) { | |
switch (action.type) { | |
case ActionTypes.FETCH_DATA_LIST_DOING: | |
return state.setIn(['dataList', 'isLoading'], true); | |
case ActionTypes.FETCH_DATA_LIST_SUCCESS: | |
return state.setIn(['dataList', 'isLoading'], false). | |
setIn(['dataList', 'data'], action.dataList). | |
setIn(['dataList', 'currentPath'], action.path). | |
setIn(['dataList', 'currentAccount'], action.account); | |
case ActionTypes.FETCH_DATA_LIST_FAILURE: | |
return state.setIn(['dataList', 'isLoading'], false); | |
default: | |
return state; | |
} | |
} | |
function fetchExternalAccountNames(state, action) { | |
switch (action.type) { | |
case ActionTypes.FETCH_EXTERNAL_ACCOUNTNAMES_SUCEESS: | |
return state.set('accountNames', action.data); | |
default: | |
return state; | |
} | |
} | |
function renameData(state, action) { | |
switch (action.type) { | |
case ActionTypes.RENAME_DATA_SUCCESS: | |
return state.setIn(['dataList', 'data', state.getIn(['dataList', 'data']) | |
.findKey((value) => value.get('entity_id') === action.entityId), 'name'], action.newName); | |
default: | |
return state; | |
} | |
} | |
function data(state = defaultState, action) { | |
switch (action.type) { | |
case ActionTypes.FETCH_DATA_LIST_DOING: | |
case ActionTypes.FETCH_DATA_LIST_SUCCESS: | |
case ActionTypes.FETCH_DATA_LIST_FAILURE: | |
return fetchDataList(state, action); | |
case ActionTypes.FETCH_EXTERNAL_ACCOUNTNAMES_SUCEESS: | |
return fetchExternalAccountNames(state, action); | |
case ActionTypes.RENAME_DATA_DOING: | |
case ActionTypes.RENAME_DATA_SUCCESS: | |
case ActionTypes.RENAME_DATA_FAILURE: | |
return renameData(state, action); | |
default: | |
return state; | |
} | |
} | |
export default data; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment