Skip to content
Snippets Groups Projects
Commit 34d13c3d authored by Chris Beer's avatar Chris Beer
Browse files

Convert window selectors to use reselect

parent 946fd839
No related branches found
No related tags found
No related merge requests found
......@@ -60,17 +60,17 @@ describe('getWindowViewType', () => {
};
it('should return view type if window exists', () => {
const received = getWindowViewType(state, 'a');
const received = getWindowViewType(state, { windowId: 'a' });
expect(received).toBe('single');
});
it('should return undefined if view type does not exist in window', () => {
const received = getWindowViewType(state, 'b');
const received = getWindowViewType(state, { windowId: 'b' });
expect(received).toBeUndefined();
});
it('should return undefined if window does not exists', () => {
const received = getWindowViewType(state, 'c');
const received = getWindowViewType(state, { windowId: 'c' });
expect(received).toBeUndefined();
});
});
......@@ -242,19 +242,19 @@ describe('getCompanionWindowForPosition', () => {
};
it('the companion window type based on the given position', () => {
const received = getCompanionWindowForPosition(state, 'a', 'right');
const received = getCompanionWindowForPosition(state, { windowId: 'a', position: 'right' });
expect(received.id).toEqual('abc');
});
it('returns undefined if the given window does not exist', () => {
const received = getCompanionWindowForPosition(state, 'c', 'right');
const received = getCompanionWindowForPosition(state, { windowId: 'c', position: 'right' });
expect(received).toBeUndefined();
});
it('returns undefined if a companion window at the given position does not exist', () => {
const received = getCompanionWindowForPosition(state, 'a', 'bottom');
const received = getCompanionWindowForPosition(state, { windowId: 'a', position: 'bottom' });
expect(received).toBeUndefined();
});
......@@ -295,7 +295,7 @@ describe('getCompanionWindowsOfWindow', () => {
};
it('should return companion windows for a given window id', () => {
const received = getCompanionWindowsOfWindow(state, 'abc123');
const received = getCompanionWindowsOfWindow(state, { windowId: 'abc123' });
expect(received).toEqual([
{ id: 'foo', content: 'info' },
......
......@@ -10,7 +10,7 @@ import { CompanionArea } from '../components/CompanionArea';
/** */
const mapStateToProps = (state, { windowId, position }) => ({
sideBarOpen: state.windows[windowId].sideBarOpen,
companionWindows: getCompanionWindowsOfWindow(state, windowId)
companionWindows: getCompanionWindowsOfWindow(state, { windowId })
.filter(cw => cw.position === position),
companionAreaOpen: position !== 'left' || state.windows[windowId].companionAreaOpen,
});
......
......@@ -36,7 +36,7 @@ const mapStateToProps = (state, { windowId }) => ({
getSelectedTargetAnnotations(state, (getSelectedCanvas(state, { windowId }) || {}).id),
['oa:commenting', 'sc:painting'],
).length > 0,
sideBarPanel: (getCompanionWindowForPosition(state, windowId, 'left') || {}).content,
sideBarPanel: (getCompanionWindowForPosition(state, { windowId, position: 'left' }) || {}).content,
});
/** */
......
......@@ -19,9 +19,9 @@ const mapDispatchToProps = { setWindowViewType: actions.setWindowViewType };
* @memberof WindowViewer
* @private
*/
const mapStateToProps = (state, props) => (
const mapStateToProps = (state, { windowId }) => (
{
windowViewType: getWindowViewType(state, props.windowId),
windowViewType: getWindowViewType(state, { windowId }),
}
);
......
import { createSelector } from 'reselect';
import filter from 'lodash/filter';
import flatten from 'lodash/flatten';
import Annotation from '../../lib/Annotation';
......@@ -88,6 +89,10 @@ export function getThumbnailNavigationPosition(state, windowId) {
&& state.companionWindows[state.windows[windowId].thumbnailNavigationId].position;
}
/** */
function getWindow(state, { windowId }) {
return state.windows && state.windows[windowId];
}
/** Return type of view in a certain window.
* @param {object} state
......@@ -96,9 +101,10 @@ export function getThumbnailNavigationPosition(state, windowId) {
* @param {string} props.windowId
* @param {String}
*/
export function getWindowViewType(state, windowId) {
return state.windows[windowId] && state.windows[windowId].view;
}
export const getWindowViewType = createSelector(
[getWindow],
window => window && window.view,
);
/**
* Return canvas label, or alternatively return the given index + 1 to be displayed
......@@ -126,38 +132,36 @@ export function getCanvasDescription(canvas) {
}
/**
* Return the companion window string from state in a given windowId and position
* @param {object} state
* Return compantion window ids from a window
* @param {String} windowId
* @param {String} position
* @return {String}
* @return {Array}
*/
export function getCompanionWindowForPosition(state, windowId, position) {
return ((state.windows[windowId] || {}).companionWindowIds || []).map(key => (
state.companionWindows[key]
)).find(cw => (
cw.position === position
));
}
export const getCompanionWindowIds = createSelector(
[getWindow],
window => (window && window.companionWindowIds) || [],
);
/**
* Return compantion window ids from a window
* Return companion windows of a window
* @param {String} windowId
* @return {Array}
*/
export function getCompanionWindowIds(state, windowId) {
return state.windows[windowId].companionWindowIds;
}
export const getCompanionWindowsOfWindow = createSelector(
[getCompanionWindowIds, state => state.companionWindows],
(companionWindowIds, companionWindows) => companionWindowIds.map(id => companionWindows[id]),
);
/**
* Return companion windows of a window
* Return the companion window string from state in a given windowId and position
* @param {object} state
* @param {String} windowId
* @return {Array}
* @param {String} position
* @return {String}
*/
export function getCompanionWindowsOfWindow(state, windowId) {
return getCompanionWindowIds(state, windowId)
.map(id => state.companionWindows[id]);
}
export const getCompanionWindowForPosition = createSelector(
[getCompanionWindowsOfWindow, (state, { position }) => position],
(companionWindows, position) => companionWindows.find(cw => cw.position === position),
);
/**
* Return languages from config (in state) and indicate which is currently set
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment