diff --git a/__tests__/src/sagas/windows.test.js b/__tests__/src/sagas/windows.test.js index 034e08edf8fc8ee13ceaaff4552126bf3464a2e0..7a69c828b640ae383aba6a9759e2cf0b5b94720f 100644 --- a/__tests__/src/sagas/windows.test.js +++ b/__tests__/src/sagas/windows.test.js @@ -28,6 +28,7 @@ import { panToFocusedWindow, setCurrentAnnotationsOnCurrentCanvas, fetchInfoResponses, + setCanvasOnNewSequence, setCollectionPath, } from '../../../src/state/sagas/windows'; import fixture from '../../fixtures/version-2/019.json'; @@ -79,6 +80,36 @@ describe('window-level sagas', () => { }); }); + describe('setCanvasOnNewSequence', () => { + it('when a sequenceId is provided, set the canvasId', () => { + const action = { + id: 'x', + payload: { + sequenceId: 'foo', + }, + }; + + return expectSaga(setCanvasOnNewSequence, action) + .provide([ + [select(getCanvases, { windowId: 'x' }), [{ id: 'a' }, { id: 'b' }]], + [call(setCanvas, 'x', 'a'), { type: 'setCanvasThunk' }], + ]) + .put({ type: 'setCanvasThunk' }) + .run(); + }); + it('when a sequenceId is not provided, return', () => { + const action = { + id: 'x', + }; + + return expectSaga(setCanvasOnNewSequence, action) + .provide([ + ]) + .run() + .then(({ allEffects }) => allEffects.length === 0); + }); + }); + describe('setWindowStartingCanvas', () => { it('calls setCanvas if the canvas id was provided', () => { const action = { diff --git a/src/components/WindowSideBarCanvasPanel.js b/src/components/WindowSideBarCanvasPanel.js index b327a3db97ca0767abe3aad679e10466c506219b..4ddf0218fea08087ded7e0a0ae361377443b4f81 100644 --- a/src/components/WindowSideBarCanvasPanel.js +++ b/src/components/WindowSideBarCanvasPanel.js @@ -40,13 +40,10 @@ export class WindowSideBarCanvasPanel extends Component { } /** @private */ - async handleSequenceChange(event) { - const { setCanvas, updateSequence } = this.props; - await updateSequence(event.target.value); + handleSequenceChange(event) { + const { updateSequence } = this.props; - const { windowId, canvases } = this.props; - const firstCanvasId = canvases[0].id; - setCanvas(windowId, firstCanvasId); + updateSequence(event.target.value); } /** @private */ @@ -162,13 +159,11 @@ export class WindowSideBarCanvasPanel extends Component { } WindowSideBarCanvasPanel.propTypes = { - canvases: PropTypes.arrayOf(PropTypes.object), classes: PropTypes.objectOf(PropTypes.string).isRequired, collection: PropTypes.object, // eslint-disable-line react/forbid-prop-types id: PropTypes.string.isRequired, sequenceId: PropTypes.string, sequences: PropTypes.arrayOf(PropTypes.object), - setCanvas: PropTypes.func.isRequired, showMultipart: PropTypes.func.isRequired, showToc: PropTypes.bool, t: PropTypes.func.isRequired, diff --git a/src/containers/WindowSideBarCanvasPanel.js b/src/containers/WindowSideBarCanvasPanel.js index 57f4f3e590c8c720e068f93cc280c63cad999a3a..51d82d2dba96ad8b3b1d37316b33473a702077e0 100644 --- a/src/containers/WindowSideBarCanvasPanel.js +++ b/src/containers/WindowSideBarCanvasPanel.js @@ -26,7 +26,6 @@ const mapStateToProps = (state, { id, windowId }) => { const collectionPath = window.collectionPath || []; const collectionId = collectionPath && collectionPath[collectionPath.length - 1]; return { - canvases: getCanvases(state, { windowId }), collection: collectionId && getManifestoInstance(state, { manifestId: collectionId }), config, sequenceId: getSequence(state, { windowId }).id, @@ -42,7 +41,6 @@ const mapStateToProps = (state, { id, windowId }) => { * @private */ const mapDispatchToProps = (dispatch, { id, windowId }) => ({ - setCanvas: (...args) => dispatch(actions.setCanvas(...args)), showMultipart: () => dispatch( actions.addOrUpdateCompanionWindow(windowId, { content: 'collection', position: 'right' }), ), diff --git a/src/state/sagas/windows.js b/src/state/sagas/windows.js index 1bfab78d5e0eaf206969e8b5aa4ebbc6a9973b57..8ad878a35124110d53c32ac10cb1e03fe1b87ff3 100644 --- a/src/state/sagas/windows.js +++ b/src/state/sagas/windows.js @@ -52,6 +52,18 @@ export function* fetchWindowManifest(action) { yield call(determineAndShowCollectionDialog, manifestId, id); } +/** */ +export function* setCanvasOnNewSequence(action) { + const windowId = action.id; + if (!action || !action.payload || !action.payload.sequenceId) return; + + const canvases = yield select(getCanvases, { windowId }); + if (!canvases || !canvases[0] || !canvases[0].id) return; + + const thunk = yield call(setCanvas, windowId, canvases[0].id); + yield put(thunk); +} + /** */ export function* setCollectionPath({ manifestId, windowId }) { const manifestoInstance = yield select(getManifestoInstance, { manifestId }); @@ -245,6 +257,7 @@ export default function* windowsSaga() { yield all([ takeEvery(ActionTypes.ADD_WINDOW, fetchWindowManifest), takeEvery(ActionTypes.UPDATE_WINDOW, fetchWindowManifest), + takeEvery(ActionTypes.UPDATE_WINDOW, setCanvasOnNewSequence), takeEvery(ActionTypes.SET_CANVAS, setCurrentAnnotationsOnCurrentCanvas), takeEvery(ActionTypes.SET_CANVAS, fetchInfoResponses), takeEvery(ActionTypes.UPDATE_COMPANION_WINDOW, fetchCollectionManifests),