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 _ from 'lodash';
import invariant from 'invariant';
import { import {
composeReducers, composeReducers,
createAction, createAction,
@ -7,11 +6,11 @@ import {
handleActions handleActions
} from 'berkeleys-redux-utils'; } from 'berkeleys-redux-utils';
import { types as challengeTypes } from '../../routes/Challenges/redux'; import * as utils from './utils.js';
import ns from '../ns.json';
import windowEpic from './window-epic.js'; import windowEpic from './window-epic.js';
import dividerEpic from './divider-epic.js'; import dividerEpic from './divider-epic.js';
import ns from '../ns.json';
import { types as challengeTypes } from '../../routes/Challenges/redux';
export const epics = [ export const epics = [
windowEpic, windowEpic,
@ -78,71 +77,14 @@ export const pressedDividerSelector =
export const widthSelector = state => getNS(state).width; export const widthSelector = state => getNS(state).width;
export const panesMapSelector = state => getNS(state).panesMap; 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 }) { export default function createPanesAspects({ createPanesMap }) {
createPanesMap = normalizePanesMapCreator(createPanesMap); createPanesMap = utils.normalizePanesMapCreator(createPanesMap);
function middleware({ getState }) { function middleware({ getState }) {
return next => action => { return next => action => {
let finalAction = action; let finalAction = action;
const panesMap = panesMapSelector(getState()); const panesMap = panesMapSelector(getState());
if (isPanesAction(action, panesMap)) { if (utils.isPanesAction(action, panesMap)) {
finalAction = { finalAction = {
...action, ...action,
meta: { meta: {
@ -155,7 +97,7 @@ export default function createPanesAspects({ createPanesMap }) {
const result = next(finalAction); const result = next(finalAction);
const nextPanesMap = createPanesMap(getState(), action); const nextPanesMap = createPanesMap(getState(), action);
if (nextPanesMap) { if (nextPanesMap) {
checkForTypeKeys(nextPanesMap); utils.checkForTypeKeys(nextPanesMap);
next(panesMapUpdated(action.type, nextPanesMap)); next(panesMapUpdated(action.type, nextPanesMap));
} }
return result; return result;
@ -181,10 +123,10 @@ export default function createPanesAspects({ createPanesMap }) {
const paneIndex = const paneIndex =
_.findIndex(state.panes, ({ name }) => paneName === name); _.findIndex(state.panes, ({ name }) => paneName === name);
const currentPane = panesByName[paneName]; const currentPane = panesByName[paneName];
const rightPane = getPane(panesByName, panes, paneIndex + 1); const rightPane = utils.getPane(panesByName, panes, paneIndex + 1);
const leftPane = getPane(panesByName, panes, paneIndex - 1); const leftPane = utils.getPane(panesByName, panes, paneIndex - 1);
const rightBound = getRightBound(rightPane, dividerBuffer); const rightBound = utils.getRightBound(rightPane, dividerBuffer);
const leftBound = getLeftBound(leftPane, dividerBuffer); const leftBound = utils.getLeftBound(leftPane, dividerBuffer);
const newPosition = _.clamp( const newPosition = _.clamp(
(clientX / width) * 100, (clientX / width) * 100,
leftBound, leftBound,
@ -235,7 +177,7 @@ export default function createPanesAspects({ createPanesMap }) {
panesMap, panesMap,
panes, panes,
panesByName: panes.reduce((panes, { name }, index) => { panesByName: panes.reduce((panes, { name }, index) => {
const dividerLeft = getDividerLeft(numOfPanes, index); const dividerLeft = utils.getDividerLeft(numOfPanes, index);
panes[name] = { panes[name] = {
name, name,
dividerLeft, dividerLeft,
@ -265,7 +207,7 @@ export default function createPanesAspects({ createPanesMap }) {
panesByName: state.panes.reduce( panesByName: state.panes.reduce(
(panesByName, { name }, index) => { (panesByName, { name }, index) => {
if (!panesByName[name].isHidden) { if (!panesByName[name].isHidden) {
const dividerLeft = getDividerLeft( const dividerLeft = utils.getDividerLeft(
numOfPanes, numOfPanes,
index - numOfHidden 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;
}