chore(Panes): Move non-redux to utils

This commit is contained in:
Berkeley Martinez
2018-01-05 16:31:13 -08:00
parent 8498abc95a
commit 8e87a8991e
2 changed files with 72 additions and 70 deletions

View File

@ -1,5 +1,4 @@
import _ from 'lodash';
import invariant from 'invariant';
import {
composeReducers,
createAction,
@ -7,11 +6,11 @@ import {
handleActions
} from 'berkeleys-redux-utils';
import { types as challengeTypes } from '../../routes/Challenges/redux';
import ns from '../ns.json';
import * as utils from './utils.js';
import windowEpic from './window-epic.js';
import dividerEpic from './divider-epic.js';
import ns from '../ns.json';
import { types as challengeTypes } from '../../routes/Challenges/redux';
export const epics = [
windowEpic,
@ -78,71 +77,14 @@ export const pressedDividerSelector =
export const widthSelector = state => getNS(state).width;
export const panesMapSelector = state => getNS(state).panesMap;
function isPanesAction({ type } = {}, panesMap) {
return !!panesMap[type];
}
function getDividerLeft(numOfPanes, index) {
let dividerLeft = null;
if (numOfPanes > 1 && numOfPanes !== index + 1) {
dividerLeft = (100 / numOfPanes) * (index + 1);
}
return dividerLeft;
}
function checkForTypeKeys(panesMap) {
_.forEach(panesMap, (_, actionType) => {
invariant(
actionType !== 'undefined',
`action type for ${panesMap[actionType]} is undefined`
);
});
return panesMap;
}
const getPane = (panesByName, panes, index) => _.get(
panesByName,
getPaneName(panes, index),
null
);
const getPaneName = (panes, index) => _.get(
panes,
index,
''
);
const createGetBound = isRight => (pane, buffer) =>
(pane && !pane.isHidden && pane.dividerLeft || (isRight ? 100 : 0)) - buffer;
const getRightBound = createGetBound(true);
const getLeftBound = createGetBound(false);
function normalizePanesMapCreator(createPanesMap) {
invariant(
_.isFunction(createPanesMap),
'createPanesMap should be a function but got %s',
createPanesMap
);
const panesMap = createPanesMap({}, { type: '@@panes/test' });
if (typeof panesMap === 'function') {
return normalizePanesMapCreator(panesMap);
}
invariant(
!panesMap,
'panesMap test should return undefined or null on test action but got %s',
panesMap
);
return createPanesMap;
}
export default function createPanesAspects({ createPanesMap }) {
createPanesMap = normalizePanesMapCreator(createPanesMap);
createPanesMap = utils.normalizePanesMapCreator(createPanesMap);
function middleware({ getState }) {
return next => action => {
let finalAction = action;
const panesMap = panesMapSelector(getState());
if (isPanesAction(action, panesMap)) {
if (utils.isPanesAction(action, panesMap)) {
finalAction = {
...action,
meta: {
@ -155,7 +97,7 @@ export default function createPanesAspects({ createPanesMap }) {
const result = next(finalAction);
const nextPanesMap = createPanesMap(getState(), action);
if (nextPanesMap) {
checkForTypeKeys(nextPanesMap);
utils.checkForTypeKeys(nextPanesMap);
next(panesMapUpdated(action.type, nextPanesMap));
}
return result;
@ -181,10 +123,10 @@ export default function createPanesAspects({ createPanesMap }) {
const paneIndex =
_.findIndex(state.panes, ({ name }) => paneName === name);
const currentPane = panesByName[paneName];
const rightPane = getPane(panesByName, panes, paneIndex + 1);
const leftPane = getPane(panesByName, panes, paneIndex - 1);
const rightBound = getRightBound(rightPane, dividerBuffer);
const leftBound = getLeftBound(leftPane, dividerBuffer);
const rightPane = utils.getPane(panesByName, panes, paneIndex + 1);
const leftPane = utils.getPane(panesByName, panes, paneIndex - 1);
const rightBound = utils.getRightBound(rightPane, dividerBuffer);
const leftBound = utils.getLeftBound(leftPane, dividerBuffer);
const newPosition = _.clamp(
(clientX / width) * 100,
leftBound,
@ -235,7 +177,7 @@ export default function createPanesAspects({ createPanesMap }) {
panesMap,
panes,
panesByName: panes.reduce((panes, { name }, index) => {
const dividerLeft = getDividerLeft(numOfPanes, index);
const dividerLeft = utils.getDividerLeft(numOfPanes, index);
panes[name] = {
name,
dividerLeft,
@ -265,7 +207,7 @@ export default function createPanesAspects({ createPanesMap }) {
panesByName: state.panes.reduce(
(panesByName, { name }, index) => {
if (!panesByName[name].isHidden) {
const dividerLeft = getDividerLeft(
const dividerLeft = utils.getDividerLeft(
numOfPanes,
index - numOfHidden
);

View File

@ -0,0 +1,60 @@
import _ from 'lodash';
import invariant from 'invariant';
export function isPanesAction({ type } = {}, panesMap) {
return !!panesMap[type];
}
export function getDividerLeft(numOfPanes, index) {
let dividerLeft = null;
if (numOfPanes > 1 && numOfPanes !== index + 1) {
dividerLeft = (100 / numOfPanes) * (index + 1);
}
return dividerLeft;
}
export function checkForTypeKeys(panesMap) {
_.forEach(panesMap, (_, actionType) => {
invariant(
actionType !== 'undefined',
`action type for ${panesMap[actionType]} is undefined`
);
});
return panesMap;
}
export const getPane = (panesByName, panes, index) => _.get(
panesByName,
getPaneName(panes, index),
null
);
export const getPaneName = (panes, index) => _.get(
panes,
[index, 'name'],
''
);
export const getRightBound = (pane, buffer) =>
((pane && !pane.isHidden && pane.dividerLeft) || 100) - buffer;
export const getLeftBound = (pane, buffer) =>
((pane && !pane.isHidden && pane.dividerLeft) || 0) + buffer;
export function normalizePanesMapCreator(createPanesMap) {
invariant(
_.isFunction(createPanesMap),
'createPanesMap should be a function but got %s',
createPanesMap
);
const panesMap = createPanesMap({}, { type: '@@panes/test' });
if (typeof panesMap === 'function') {
return normalizePanesMapCreator(panesMap);
}
invariant(
!panesMap,
'panesMap test should return undefined or null on test action but got %s',
panesMap
);
return createPanesMap;
}