Created
November 1, 2017 13:17
-
-
Save pyldin601/d3d5d02401dc5a9682ea295b9babfaff to your computer and use it in GitHub Desktop.
This file contains hidden or 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 { fromJS } from 'immutable'; | |
import * as _ from 'lodash'; | |
import { | |
ACTION_CHAIN_UPDATE, | |
ACTION_NODE_ADD, | |
ACTION_NODE_REMOVE, | |
ACTION_NODE_REMOVE_MULTIPLE, | |
ACTION_NODE_UPDATE, | |
ACTION_RUN_ADD, ACTION_RUN_DELETE, ACTION_RUN_UPDATE, | |
ACTION_UPDATE_ACTIVE_CHAIN, | |
ACTION_UPDATE_ACTIVE_CHAIN_TITLE, | |
IAction | |
} from '../actions'; | |
import * as mixpanel from '../services/mixpanel'; | |
import { pathToChainPath } from './'; | |
export default (chainResource: any = null, { type, payload }: IAction) => { | |
switch (type) { | |
case ACTION_CHAIN_UPDATE: { | |
const { chainResource } = payload; | |
return (chainResource.id === chainResource.id) ? chainResource : chainResource; | |
} | |
case ACTION_UPDATE_ACTIVE_CHAIN: | |
return payload.chain; | |
case ACTION_NODE_UPDATE: { | |
const { action, path } = payload; | |
const immutableChainResource = fromJS(chainResource); | |
const chain = immutableChainResource.getIn(['chain']); | |
const pathToUpdate = pathToChainPath(chain, path); | |
return immutableChainResource.updateIn(pathToUpdate, () => action).toJS(); | |
} | |
case ACTION_UPDATE_ACTIVE_CHAIN_TITLE: { | |
const { title } = payload; | |
return { ...chainResource, title }; | |
} | |
case ACTION_NODE_REMOVE: { | |
const { path } = payload; | |
const immutableChainResource = fromJS(chainResource); | |
const chain = immutableChainResource.getIn(['chain', 'chain']); | |
const pathToRemove = [...pathToChainPath(chain, _.initial(path)), _.last(path)]; | |
const action = immutableChainResource.getIn(pathToRemove); | |
if (action) { | |
window.setTimeout(mixpanel.actionDeleted, 0, action.toJS()); | |
} | |
return immutableChainResource.deleteIn(pathToRemove).toJS(); | |
} | |
case ACTION_NODE_REMOVE_MULTIPLE: { | |
const { paths } = payload; | |
const immutableChainResource = fromJS(chainResource); | |
const chain = immutableChainResource.getIn(['chain', 'chain']); | |
const pathsToRemove = paths.map(path => [...pathToChainPath(chain, _.initial(path)), _.last(path)]); | |
pathsToRemove | |
.map(path => immutableChainResource.getIn(path)) | |
.forEach(action => setTimeout(mixpanel.actionDeleted, 0, action)); | |
return immutableChainResource | |
.withMutations(state => pathsToRemove.reduce((newState, path) => state.deleteIn(path), state)) | |
.toJS(); | |
} | |
case ACTION_NODE_ADD: { | |
const { action, path } = payload; | |
const immutableChainResource = fromJS(chainResource); | |
const chain = immutableChainResource.getIn(['chain', 'chain']); | |
const pathToAdd = pathToChainPath(chain, path); | |
window.setTimeout(mixpanel.actionCreated, 0, action); | |
return immutableChainResource | |
.updateIn(pathToAdd, nodes => nodes.push(action)) | |
.toJS(); | |
} | |
case ACTION_RUN_ADD: { | |
const { runResource } = payload; | |
if (runResource.chain_id !== chainResource.id) { | |
return chainResource; | |
} | |
return { | |
...chainResource, | |
runners: [...chainResource.runners, runResource], | |
}; | |
} | |
case ACTION_RUN_UPDATE: { | |
const { runResource } = payload; | |
if (runResource.chain_id !== chainResource.id) { | |
return chainResource; | |
} | |
const updatedRunners = chainResource.runners.map( | |
chainRunResource => ( | |
chainRunResource.id === runResource.id | |
? runResource | |
: chainRunResource | |
), | |
); | |
return { | |
...chainResource, | |
runners: updatedRunners, | |
}; | |
} | |
case ACTION_RUN_DELETE: { | |
const { runResource } = payload; | |
if (runResource.chain_id !== chainResource.id) { | |
return chainResource; | |
} | |
const updatedRunners = chainResource.runners.filter( | |
chainRunResource => chainRunResource.id !== runResource.id | |
); | |
return { | |
...chainResource, | |
runners: updatedRunners, | |
}; | |
} | |
default: | |
return chainResource; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment