Skip to content

Instantly share code, notes, and snippets.

@aaronmcadam
Created November 21, 2016 18:24
Show Gist options
  • Save aaronmcadam/4f8abcc1dd65a911904eb420f7a82ab6 to your computer and use it in GitHub Desktop.
Save aaronmcadam/4f8abcc1dd65a911904eb420f7a82ab6 to your computer and use it in GitHub Desktop.
/* @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