diff --git a/src/common/data-managers/rest-data-manager.js b/src/common/data-managers/rest-data-manager.js index 271f4c6..5076fb8 100644 --- a/src/common/data-managers/rest-data-manager.js +++ b/src/common/data-managers/rest-data-manager.js @@ -133,16 +133,51 @@ class RestDataManager { } - add(object, token = null, param = null) { + add(object, token = null, param = null, subObjects = null) { var obj = {}; obj[this.type] = this.filterKeys(object); - RestAPI.post(this.requestURL('load/add',null,param), obj, token).then(response => { AppDispatcher.dispatch({ type: this.type + 's/added', data: response[this.type], token: token }); + + // check if POST is done for import of object and issue dispatches of sub-objects + if (subObjects !== null){ + // there are sub-objects to be added for an import + for (let objectType of subObjects){ + let type = Object.keys(objectType) // type can be dashboards, configs, widgets, ... + type = type[0]; + for (let newObj of objectType[type]){ + + // set the ID of the object that the sub-object shall be associated with + if(type === "configs" || type === "dashboards"){ + // the main object is a scenario + newObj.scenarioID = response[this.type].id + } else if (type === "widgets") { + // the main object is a dashboard + newObj.dashboardID = response[this.type].id + } else if (type === "signals") { + // the main object is a component configuration + newObj.configID = response[this.type].id + } + + console.log("Adding new object of type", type, "with content", newObj, "to object of type ", this.type, "with ID ", response[this.type].id) + // iterate over all objects of type 'type' add issue add dispatch + AppDispatcher.dispatch({ + type: type + '/start-add', + data: newObj, + token: token + }) + + } + } + + + } + + }).catch(error => { AppDispatcher.dispatch({ type: this.type + 's/add-error', diff --git a/src/componentconfig/config-store.js b/src/componentconfig/config-store.js index b67377d..990275d 100644 --- a/src/componentconfig/config-store.js +++ b/src/componentconfig/config-store.js @@ -33,6 +33,35 @@ class ConfigStore extends ArrayStore { ConfigsDataManager.loadFiles(action.token, action.data); return super.reduce(state, action); + case 'configs/start-add': + // Check if this is a recursive component config import or not + if (action.data.hasOwnProperty("outputMapping") || action.data.hasOwnProperty("inputMapping")) { + // import + let subObjects = [] + let outputMapping = {} + let inputMapping = {} + + if (action.data.hasOwnProperty("outputMapping")){ + outputMapping["signals"] = action.data.outputMapping + subObjects.push(outputMapping) + delete action.data.outputMapping; // remove outputMapping signals from config object + } + if (action.data.hasOwnProperty("inputMapping")){ + inputMapping["signals"] = action.data.inputMapping + subObjects.push(inputMapping) + delete action.data.inputMapping; // remove inputMapping signals from config object + } + + // action.data should now contain the config and no sub-objects + // sub-objects are treated in add method of RestDataManager + this.dataManager.add(action.data, action.token,action.param, subObjects); + return state + + } else { + // no import + return super.reduce(state, action); + } + default: return super.reduce(state, action); diff --git a/src/dashboard/dashboard-store.js b/src/dashboard/dashboard-store.js index 4e850ab..23b28d6 100644 --- a/src/dashboard/dashboard-store.js +++ b/src/dashboard/dashboard-store.js @@ -18,4 +18,39 @@ import ArrayStore from '../common/array-store'; import DashboardsDataManager from './dashboards-data-manager'; -export default new ArrayStore('dashboards', DashboardsDataManager); +class DashboardStore extends ArrayStore { + constructor() { + super('dashboards', DashboardsDataManager); + } + + reduce(state, action) { + switch (action.type) { + case 'dashboards/start-add': + + // Check if this is a recursive dashboard import or not + if (action.data.hasOwnProperty("widgets")) { + // import + let subObjects = [] + let widgets = {} + widgets["widgets"] = action.data.widgets + subObjects.push(widgets) + delete action.data.widgets; // remove widgets from dashboard object + + // action.data should now contain the dashboard and no sub-objects + // sub-objects are treated in add method of RestDataManager + this.dataManager.add(action.data, action.token,action.param, subObjects); + return state + + } else { + // no import + return super.reduce(state, action); + } + + default: + return super.reduce(state, action); + } + } + +} + +export default new DashboardStore(); diff --git a/src/scenario/scenario-store.js b/src/scenario/scenario-store.js index 777475c..6043b68 100644 --- a/src/scenario/scenario-store.js +++ b/src/scenario/scenario-store.js @@ -19,4 +19,49 @@ import ScenariosDataManager from './scenarios-data-manager'; import ArrayStore from '../common/array-store'; -export default new ArrayStore('scenarios', ScenariosDataManager); +class ScenarioStore extends ArrayStore{ + constructor() { + super('scenarios', ScenariosDataManager); + } + + reduce(state, action) { + switch (action.type) { + case 'scenarios/start-add': + + // Check if this is a recursive scenario import or not + if (action.data.hasOwnProperty("configs") || action.data.hasOwnProperty("dashboards")) { + // import + let subObjects = [] + let configs = {} + let dashboards = {} + + if (action.data.hasOwnProperty("configs")){ + configs["configs"] = action.data.configs + subObjects.push(configs) + delete action.data.configs; // remove configs from scenario object + } + if (action.data.hasOwnProperty("dashboards")){ + dashboards["dashboards"] = action.data.dashboards + subObjects.push(dashboards) + delete action.data.dashboards; // remove dashboards from scenario object + } + + // action.data should now contain the scenario and no sub-objects + // sub-objects are treated in add method of RestDataManager + this.dataManager.add(action.data, action.token,action.param, subObjects); + return state + + } else { + // no import + return super.reduce(state, action); + } + + default: + return super.reduce(state, action); + } + } + + +} + +export default new ScenarioStore(); diff --git a/src/scenario/scenarios.js b/src/scenario/scenarios.js index c00d8f6..19688bc 100644 --- a/src/scenario/scenarios.js +++ b/src/scenario/scenarios.js @@ -74,12 +74,27 @@ class Scenarios extends Component { } componentDidUpdate(prevProps, prevState) { + + for (let i = prevState.scenarios.length; i < this.state.scenarios.length; i++) { + AppDispatcher.dispatch({ + type: 'dashboards/start-load', + token: this.state.sessionToken, + param: '?scenarioID=' + this.state.scenarios[i].id + }); + AppDispatcher.dispatch({ + type: 'configs/start-load', + token: this.state.sessionToken, + param: '?scenarioID=' + this.state.scenarios[i].id + }); + } + // 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) { 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) { @@ -124,8 +139,11 @@ class Scenarios extends Component { }); } } + + } + // 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) { @@ -167,11 +185,16 @@ class Scenarios extends Component { } } + + + */ } closeNewModal(data) { this.setState({ newModal: false }); + + // TODO create dispatch here to add scenario to Backend database! } showDeleteModal(id) { @@ -240,9 +263,9 @@ class Scenarios extends Component { this.setState({ importModal: false }); if (data) { - let newScenario = JSON.parse(JSON.stringify(data)); + //let newScenario = JSON.parse(JSON.stringify(data)); // temporarily store dashboard data until scenario is created - if (data.dashboards) { + /*if (data.dashboards) { this.addDashboards = true; this.dashboardsToAdd = data.dashboards; } @@ -251,9 +274,11 @@ class Scenarios extends Component { this.configsToAdd = data.configs; } delete newScenario.dashboards; + */ + AppDispatcher.dispatch({ type: 'scenarios/start-add', - data: newScenario, + data: data, token: this.state.sessionToken, }); }