diff --git a/__tests__/src/selectors/index.test.js b/__tests__/src/selectors/index.test.js index fdd9ae417dffce6bafbda95487853d8e2d2dde61..41f0400122f8ca32667f2f7c4541ee74005c5fa1 100644 --- a/__tests__/src/selectors/index.test.js +++ b/__tests__/src/selectors/index.test.js @@ -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' }, diff --git a/src/containers/CompanionArea.js b/src/containers/CompanionArea.js index f8b29c6825ac731664f881c0093551852e0b6477..f9d577e2489addb37b578fb708a282a7271a7269 100644 --- a/src/containers/CompanionArea.js +++ b/src/containers/CompanionArea.js @@ -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, }); diff --git a/src/containers/WindowSideBarButtons.js b/src/containers/WindowSideBarButtons.js index 2cec11da4ce48d811b600db0899b272341063da8..17dbfd7bc913e57fc69f5e10cb1a938417098c63 100644 --- a/src/containers/WindowSideBarButtons.js +++ b/src/containers/WindowSideBarButtons.js @@ -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, }); /** */ diff --git a/src/containers/WindowViewSettings.js b/src/containers/WindowViewSettings.js index 1a7d8c44ed4911d6f23f24e6fe71115508d973ea..dcd29e9ea9ed3cf4ceb058c31ac2512b1359d054 100644 --- a/src/containers/WindowViewSettings.js +++ b/src/containers/WindowViewSettings.js @@ -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 }), } ); diff --git a/src/state/selectors/index.js b/src/state/selectors/index.js index c51fdf2df3a9c7a298bffb59357da71d29a90c83..98e2ddb4169d23db1883abc858c7bc0b6a83e513 100644 --- a/src/state/selectors/index.js +++ b/src/state/selectors/index.js @@ -1,3 +1,4 @@ +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 @@ -125,39 +131,37 @@ export function getCanvasDescription(canvas) { && canvas.getProperty('description'); } -/** -* Return the companion window string from state in a given windowId and position -* @param {object} state -* @param {String} windowId -* @param {String} position -* @return {String} -*/ -export function getCompanionWindowForPosition(state, windowId, position) { - return ((state.windows[windowId] || {}).companionWindowIds || []).map(key => ( - state.companionWindows[key] - )).find(cw => ( - cw.position === position - )); -} - /** * Return compantion window ids from a window * @param {String} windowId * @return {Array} */ -export function getCompanionWindowIds(state, windowId) { - return state.windows[windowId].companionWindowIds; -} +export const getCompanionWindowIds = createSelector( + [getWindow], + window => (window && window.companionWindowIds) || [], +); /** * Return companion windows of a window * @param {String} windowId * @return {Array} */ -export function getCompanionWindowsOfWindow(state, windowId) { - return getCompanionWindowIds(state, windowId) - .map(id => state.companionWindows[id]); -} +export const getCompanionWindowsOfWindow = createSelector( + [getCompanionWindowIds, state => state.companionWindows], + (companionWindowIds, companionWindows) => companionWindowIds.map(id => companionWindows[id]), +); + +/** +* Return the companion window string from state in a given windowId and position +* @param {object} state +* @param {String} windowId +* @param {String} position +* @return {String} +*/ +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