Created
November 21, 2016 18:24
-
-
Save aaronmcadam/4f8abcc1dd65a911904eb420f7a82ab6 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
/* @flow */ | |
import R from 'ramda'; | |
import * as ActionTypes from './constants'; | |
import type { State, Action } from '../../types'; | |
import { setGrandchild } from '../../services/reducerHelpers'; | |
const initialState = {}; | |
/* eslint space-infix-ops: "off" */ | |
const comparison = (state: State = initialState, action: Action): State => { | |
const setState = setGrandchild( | |
state, | |
getProjectKey(action), | |
getSegmentKey(action) | |
); | |
switch (action.type) { | |
case ActionTypes.LOAD_COMPARISON: | |
return setState({ | |
leftTaskId: action.payload.leftTaskId, | |
rightTaskId: action.payload.rightTaskId | |
}); | |
case ActionTypes.FETCH_COMPARISON_REQUEST: | |
return setState({ | |
isFetching: true | |
}); | |
case ActionTypes.FETCH_COMPARISON_SUCCESS: | |
return setState({ | |
isFetching: false, | |
leftTaskId: action.payload.leftTaskId, | |
rightTaskId: action.payload.rightTaskId, | |
[getComparisonKey(action)]: { | |
payload: action.payload.comparison | |
} | |
}); | |
case ActionTypes.FETCH_COMPARISON_FAILURE: | |
return setState({ | |
isFetching: false, | |
leftTaskId: action.payload.leftTaskId, | |
rightTaskId: action.payload.rightTaskId, | |
error: action.payload.error | |
}); | |
default: | |
return state; | |
} | |
}; | |
const getProjectId = R.path(['payload', 'projectId']); | |
const getProjectKey = R.compose(R.concat('project'), getProjectId); | |
const getTaskLeftId = R.pathOr('All', ['payload', 'leftTaskId']); | |
const getTaskRightId = R.pathOr('All', ['payload', 'rightTaskId']); | |
const getSegmentId = R.pathOr('All', ['payload', 'segmentId']); | |
const getSegmentKey = R.compose(R.concat('segment'), getSegmentId); | |
const getComparisonKey = (action) => ( | |
`left${getTaskLeftId(action)}_right${getTaskRightId(action)}` | |
); | |
export default comparison; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment