Skip to content
Snippets Groups Projects
Commit 838e2fd7 authored by Glenn Fischer's avatar Glenn Fischer
Browse files

#1874: refactors config import

parent 467c60f4
No related branches found
No related tags found
No related merge requests found
......@@ -13,10 +13,11 @@ export class WorkspaceExport extends Component {
*/
exportableState() {
const { state } = this.props;
const { config, windows } = state;
const { config, viewers, windows } = state;
return JSON.stringify({
config,
viewers,
windows,
}, null, 2);
}
......
......@@ -12,7 +12,6 @@ import { App } from '../components/App';
*/
const mapStateToProps = state => (
{
errors: state.errors,
isFullscreenEnabled: state.workspace.isFullscreenEnabled,
language: state.config.language,
theme: state.config.theme,
......@@ -26,7 +25,6 @@ const mapStateToProps = state => (
* @private
*/
const mapDispatchToProps = {
removeError: actions.removeError,
setWorkspaceFullscreen: actions.setWorkspaceFullscreen,
};
......
......@@ -5,13 +5,6 @@ import { withPlugins } from '../extend';
import WorkspaceImport from '../components/WorkspaceImport';
import * as actions from '../state/actions';
/**
* mapStateToProps - to hook up connect
* @memberof WorkspaceImport
* @private
*/
const mapStateToProps = state => ({ state });
/**
* mapDispatchToProps - used to hook up connect to action creators
* @memberof App
......@@ -23,7 +16,7 @@ const mapDispatchToProps = {
};
const enhance = compose(
connect(mapStateToProps, mapDispatchToProps),
connect(null, mapDispatchToProps),
withTranslation(),
withPlugins('WorkspaceImport'),
);
......
import _ from 'lodash';
import { difference, values } from 'lodash';
import ActionTypes from './action-types';
import { importConfig } from './config';
import { removeWindow, addWindow, updateWindow } from './window';
......@@ -97,33 +97,33 @@ export function toggleWorkspaceExposeMode() {
export function importWorkspace(stateExport) {
return (dispatch, getState) => {
dispatch(importConfig(stateExport.config));
const { viewers } = stateExport;
const newWindows = stateExport.windows;
const newWindowsKeys = _.keys(newWindows);
const newWindowsCnt = newWindowsKeys.length;
const { viewers } = stateExport || {};
const imWins = values(stateExport.windows);
const existingWindows = getState().windows;
const existingWindowsKeys = _.keys(existingWindows);
const existingWindowsCnt = existingWindowsKeys.length;
/* re-use existing windows */
const exIds = values(getState().windows).map((exWin) => {
const imWin = imWins.shift();
const viewer = viewers[imWin.id];
delete imWin.id;
let currentKey = '';
// re-use existing windows
for (let i = 0; i < newWindowsCnt; i++) { // eslint-disable-line no-plusplus
dispatch(fetchManifest(newWindows[newWindowsKeys[i]].manifestId));
if (i < existingWindowsCnt) {
currentKey = existingWindowsKeys[i];
delete newWindows[newWindowsKeys[i]].id;
dispatch(updateWindow(existingWindowsKeys[i], newWindows[newWindowsKeys[i]]));
} else {
dispatch(addWindow(newWindows[newWindowsKeys[i]]));
currentKey = newWindowsKeys[i];
}
dispatch(updateViewport(currentKey, viewers[newWindowsKeys[i]]));
}
dispatch(fetchManifest(imWin.manifestId));
dispatch(updateWindow(exWin.id, imWin));
dispatch(updateViewport(exWin.id, viewer));
// clean up surplus windows if there are any
if (existingWindowsCnt > newWindowsCnt) {
Object.keys(existingWindows).slice(newWindowsCnt).map(windowId => dispatch(removeWindow(windowId))); // eslint-disable-line max-len
}
return exWin.id;
});
/* create new windows for additionally imported ones */
const imIds = imWins.map((imWin) => {
dispatch(fetchManifest(imWin.manifestId));
dispatch(addWindow(imWin));
dispatch(updateViewport(imWin.id, viewers[imWin.id]));
return imWin.id;
});
/* close surplus windows */
difference(getState().windows, exIds.concat(imIds))
.map(winId => dispatch(removeWindow(winId)));
};
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment