From fdb6a3105aff246c8f739ddf4aae88f6eeb4c5d2 Mon Sep 17 00:00:00 2001 From: irismarie Date: Wed, 20 May 2020 10:08:51 +0200 Subject: [PATCH] add scenario data after importing from file --- src/scenario/scenarios.js | 154 +++++++++++++++++++++++++++++--------- 1 file changed, 119 insertions(+), 35 deletions(-) diff --git a/src/scenario/scenarios.js b/src/scenario/scenarios.js index e054344..c00d8f6 100644 --- a/src/scenario/scenarios.js +++ b/src/scenario/scenarios.js @@ -40,7 +40,7 @@ import DeleteDialog from '../common/dialogs/delete-dialog'; class Scenarios extends Component { static getStores() { - return [ ScenarioStore, LoginStore, DashboardStore, WidgetStore, ConfigStore]; + return [ScenarioStore, LoginStore, DashboardStore, WidgetStore, ConfigStore]; } static calculateState() { @@ -74,46 +74,104 @@ class Scenarios extends Component { } componentDidUpdate(prevProps, prevState) { - // load dashboards when length of scanario array has increased + // when length of scenarios array has increased, either add data (after import) + // or load data (after export) if (this.state.scenarios.length > prevState.scenarios.length) { - let scenarios = Object.assign([], this.state.scenarios); // copying neccessary? - for (var i = prevState.scenarios.length; i < scenarios.length; i++) { - AppDispatcher.dispatch({ - type: 'dashboards/start-load', - token: this.state.sessionToken, - param: '?scenarioID='+scenarios[i].id - }); - AppDispatcher.dispatch({ - type: 'configs/start-load', - token: this.state.sessionToken, - param: '?scenarioID='+scenarios[i].id - }); + if (this.addDashboards || this.addConfigs) { + let scenarioID = this.state.scenarios[this.state.scenarios.length - 1].id; + + if (this.addDashboards) { + this.dashboardsToAdd.forEach((dashboard) => { + if (dashboard.widgets) { + this.addWidgets = true; + } + dashboard.scenarioID = scenarioID; + AppDispatcher.dispatch({ + type: 'dashboards/start-add', + token: this.state.sessionToken, + data: dashboard + }); + }) + this.addDashboards = false; + } + + if (this.addConfigs) { + this.configsToAdd.forEach((config) => { + config.scenarioID = scenarioID; + AppDispatcher.dispatch({ + type: 'configs/start-add', + token: this.state.sessionToken, + data: config + }) + }) + delete this.configsToAdd; + this.addConfigs = false; + } + + } + else { + let scenarios = Object.assign([], this.state.scenarios); // copying neccessary? + for (var i = prevState.scenarios.length; i < scenarios.length; i++) { + AppDispatcher.dispatch({ + type: 'dashboards/start-load', + token: this.state.sessionToken, + param: '?scenarioID=' + scenarios[i].id + }); + AppDispatcher.dispatch({ + type: 'configs/start-load', + token: this.state.sessionToken, + param: '?scenarioID=' + scenarios[i].id + }); + } } } - // load widgets when length of dashboard array has increased + + // when length of dashboards array has increased, either add widgets (after import) + // or load widgets (after export) if (this.state.dashboards.length > prevState.dashboards.length) { - let dashboards = Object.assign([], this.state.dashboards); - for (var j = prevState.dashboards.length; j < dashboards.length; j++) { - AppDispatcher.dispatch({ - type: 'widgets/start-load', - token: this.state.sessionToken, - param: '?dashboardID='+dashboards[j].id - }) + if (this.addWidgets && !this.addDashboards) { // add widget data + let dashboards = Object.assign([], this.state.dashboards); + for (var j = prevState.dashboards.length; j < dashboards.length; j++) { + let dboard = dashboards[j]; + let dboardID = dboard.id; + let dashboard = this.dashboardsToAdd.shift(); + if (dashboard.name !== dboard.name) { + console.log("Cannot add widgets, dashboard was not added as expected!"); + this.addWidgets = false; + return; + } + dashboard.widgets.forEach((widget) => { + widget.dashboardID = dboardID; + AppDispatcher.dispatch({ + type: 'widgets/start-add', + token: this.state.sessionToken, + data: widget + }); + }); + } + + if (this.dashboardsToAdd.length === 0) { + delete this.dashboardsToAdd; + this.addWidgets = false; + } } + else { // load widget data + let dashboards = Object.assign([], this.state.dashboards); + for (var j = prevState.dashboards.length; j < dashboards.length; j++) { + AppDispatcher.dispatch({ + type: 'widgets/start-load', + token: this.state.sessionToken, + param: '?dashboardID=' + dashboards[j].id + }) + } + } + } } closeNewModal(data) { - this.setState({ newModal : false }); - - if (data) { - AppDispatcher.dispatch({ - type: 'scenarios/start-add', - data, - token: this.state.sessionToken - }); - } + this.setState({ newModal: false }); } showDeleteModal(id) { @@ -136,6 +194,16 @@ class Scenarios extends Component { return; } + this.state.dashboards.forEach((dashboard) => { + if (dashboard.id === this.state.modalScenario.id) { + AppDispatcher.dispatch({ + type: 'dashboards/start-remove', + data: dashboard, + token: this.state.sessionToken + }) + } + }); + AppDispatcher.dispatch({ type: 'scenarios/start-remove', data: this.state.modalScenario, @@ -157,7 +225,7 @@ class Scenarios extends Component { } closeEditModal(data) { - this.setState({ editModal : false }); + this.setState({ editModal: false }); if (data != null) { AppDispatcher.dispatch({ @@ -172,10 +240,21 @@ class Scenarios extends Component { this.setState({ importModal: false }); if (data) { + let newScenario = JSON.parse(JSON.stringify(data)); + // temporarily store dashboard data until scenario is created + if (data.dashboards) { + this.addDashboards = true; + this.dashboardsToAdd = data.dashboards; + } + if (data.configs) { + this.addConfigs = true; + this.configsToAdd = data.configs; + } + delete newScenario.dashboards; AppDispatcher.dispatch({ type: 'scenarios/start-add', - data, - token: this.state.sessionToken + data: newScenario, + token: this.state.sessionToken, }); } } @@ -198,10 +277,15 @@ class Scenarios extends Component { // create JSON object and add component configs delete scenario.id; let jsonObj = scenario; + + configs.forEach((config) => { + delete config.id; + delete config.scenarioID; + }) jsonObj["configs"] = configs; // add Dashboards and Widgets to JSON object - dashboards.forEach((dboard) => { + dashboards.forEach((dboard) => { let widgets = JSON.parse(JSON.stringify(WidgetStore.getState().filter(w => w.dashboardID === parseInt(dboard.id, 10)))); widgets.forEach((widget) => { delete widget.dashboardID;