From 7eca59bc7c7cec7eb295fe269341b2ea342ae11b Mon Sep 17 00:00:00 2001 From: irismarie Date: Fri, 13 Nov 2020 13:12:15 +0100 Subject: [PATCH] duplicate scenario, #263 --- src/common/table-column.js | 1 + src/common/table.js | 5 ++++ src/scenario/scenarios.js | 52 ++++++++++++++++++++++++++++---------- 3 files changed, 45 insertions(+), 13 deletions(-) diff --git a/src/common/table-column.js b/src/common/table-column.js index 53b99e5..df68140 100644 --- a/src/common/table-column.js +++ b/src/common/table-column.js @@ -25,6 +25,7 @@ class TableColumn extends Component { editButton: false, deleteButton: false, exportButton: false, + duplicateButton: false, link: '/', linkKey: '', dataIndex: false, diff --git a/src/common/table.js b/src/common/table.js index 87a52be..05680aa 100644 --- a/src/common/table.js +++ b/src/common/table.js @@ -123,6 +123,11 @@ class CustomTable extends Component { ); } + if (child.props.duplicateButton) { + cell.push( Duplicate } > + ); + } + return cell; } // addCell diff --git a/src/scenario/scenarios.js b/src/scenario/scenarios.js index f093d7a..d176d0f 100644 --- a/src/scenario/scenarios.js +++ b/src/scenario/scenarios.js @@ -52,6 +52,7 @@ class Scenarios extends Component { sessionToken: localStorage.getItem("token"), newModal: false, + duplicateModal: false, deleteModal: false, editModal: false, importModal: false, @@ -148,17 +149,8 @@ class Scenarios extends Component { } }; - exportScenario(index) { - // copy by value by converting to JSON and back - // otherwise, IDs of state objects will be deleted - let scenario = JSON.parse(JSON.stringify(this.state.scenarios[index])); - let configs = JSON.parse(JSON.stringify(this.state.configs.filter(config => config.scenarioID === scenario.id))); - let dashboards = JSON.parse(JSON.stringify(this.state.dashboards.filter(dashb => dashb.scenarioID === scenario.id))); - - // create JSON object and add component configs - delete scenario.id; - let jsonObj = scenario; - + getConfigs(scenarioID) { + let configs = JSON.parse(JSON.stringify(this.state.configs.filter(config => config.scenarioID === scenarioID))); configs.forEach((config) => { let signals = JSON.parse(JSON.stringify(SignalStore.getState().filter(s => s.configID === parseInt(config.id, 10)))); signals.forEach((signal) => { @@ -179,8 +171,12 @@ class Scenarios extends Component { delete config.inputLength; delete config.outputLength; }) - jsonObj["configs"] = configs; + return configs; + } + + getDashboards(scenarioID) { + let dashboards = JSON.parse(JSON.stringify(this.state.dashboards.filter(dashb => dashb.scenarioID === scenarioID))); // add Dashboards and Widgets to JSON object dashboards.forEach((dboard) => { let widgets = JSON.parse(JSON.stringify(WidgetStore.getState().filter(w => w.dashboardID === parseInt(dboard.id, 10)))); @@ -192,14 +188,42 @@ class Scenarios extends Component { delete dboard.scenarioID; delete dboard.id; }); - jsonObj["dashboards"] = dashboards; + return dashboards; + } + exportScenario(index) { + // copy by value by converting to JSON and back + // otherwise, IDs of state objects will be deleted + let scenario = JSON.parse(JSON.stringify(this.state.scenarios[index])); + let scenarioID = scenario.id; + delete scenario.id; + + let jsonObj = scenario; + jsonObj["configs"] = this.getConfigs(scenarioID); + jsonObj["dashboards"] = this.getDashboards(scenarioID); // create JSON string and show save dialog const blob = new Blob([JSON.stringify(jsonObj, null, 2)], { type: 'application/json' }); FileSaver.saveAs(blob, 'scenario - ' + scenario.name + '.json'); } + duplicateScenario(index) { + let scenario = JSON.parse(JSON.stringify(this.state.scenarios[index])); + scenario.name = scenario.name + '_copy'; + let jsonObj = scenario; + + jsonObj["configs"] = this.getConfigs(scenario.id); + jsonObj["dashboards"] = this.getDashboards(scenario.id); + + if(jsonObj) { + AppDispatcher.dispatch({ + type: 'scenarios/start-add', + data: jsonObj, + token: this.state.sessionToken, + }); + } + } + modifyRunningColumn(running){ if(running){ @@ -228,9 +252,11 @@ class Scenarios extends Component { editButton deleteButton exportButton + duplicateButton onEdit={index => this.setState({ editModal: true, modalScenario: this.state.scenarios[index] })} onDelete={index => this.setState({ deleteModal: true, modalScenario: this.state.scenarios[index] })} onExport={index => this.exportScenario(index)} + onDuplicate={index => this.duplicateScenario(index)} />