mirror of
https://git.rwth-aachen.de/acs/public/villas/web/
synced 2025-03-09 00:00:01 +01:00
Import Dashboard
plus (#216): copy by value, delete IDs, restrict number of times loading data
This commit is contained in:
parent
730b393671
commit
967f17c4f3
2 changed files with 58 additions and 32 deletions
|
@ -140,15 +140,32 @@ class Scenario extends React.Component {
|
|||
}
|
||||
|
||||
componentDidUpdate(prevProps, prevState) {
|
||||
// load widgets when dashboard id(s) are available
|
||||
if (this.state.dashboards.length !== prevState.dashboards.length) {
|
||||
let dashboards = Object.assign([], this.state.dashboards);
|
||||
dashboards.forEach(dboard => {
|
||||
AppDispatcher.dispatch({
|
||||
type: 'widgets/start-load',
|
||||
token: this.state.sessionToken,
|
||||
param: '?dashboardID='+dboard.id
|
||||
})})
|
||||
if (this.state.dashboards.length > prevState.dashboards.length) {
|
||||
if (this.addWidgets) { // add widgets
|
||||
// this can only be true after dashboard import, so there is only one dashboard
|
||||
// (the newest) and this dashboards ID is used
|
||||
let dashboardID = this.state.dashboards[this.state.dashboards.length - 1].id;
|
||||
this.widgetsToAdd.forEach((widget) => {
|
||||
widget.dashboardID = dashboardID;
|
||||
AppDispatcher.dispatch({
|
||||
type: 'widgets/start-add',
|
||||
data: widget,
|
||||
token: this.state.sessionToken,
|
||||
})
|
||||
})
|
||||
this.addWidgets = false;
|
||||
this.widgetsToAdd = [];
|
||||
}
|
||||
else { // get widgets
|
||||
let dashboards = Object.assign([], this.state.dashboards);
|
||||
for (var i = prevState.dashboards.length; i < this.state.dashboards.length; i++) {
|
||||
AppDispatcher.dispatch({
|
||||
type: 'widgets/start-load',
|
||||
token: this.state.sessionToken,
|
||||
param: '?dashboardID=' + dashboards[i].id
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -337,9 +354,17 @@ class Scenario extends React.Component {
|
|||
this.setState({ importDashboardModal: false });
|
||||
|
||||
if (data) {
|
||||
let newDashboard = JSON.parse(JSON.stringify(data));
|
||||
newDashboard["scenarioID"] = this.state.scenario.id;
|
||||
// temporarily store widget data until dashboard is created
|
||||
if (data.widgets) {
|
||||
this.addWidgets = true;
|
||||
this.widgetsToAdd = data.widgets;
|
||||
}
|
||||
delete newDashboard.widgets;
|
||||
AppDispatcher.dispatch({
|
||||
type: 'dashboards/start-add',
|
||||
data,
|
||||
data: newDashboard,
|
||||
token: this.state.sessionToken,
|
||||
});
|
||||
}
|
||||
|
|
|
@ -74,33 +74,32 @@ class Scenarios extends Component {
|
|||
}
|
||||
|
||||
componentDidUpdate(prevProps, prevState) {
|
||||
// TODO check/change conditions
|
||||
|
||||
// load dashboards when scanario(s) are available
|
||||
if (this.state.scenarios.length !== prevState.scenarios.length) {
|
||||
let scenarios = Object.assign([], this.state.scenarios);
|
||||
scenarios.forEach(scenario => {
|
||||
// load dashboards when length of scanario array has increased
|
||||
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='+scenario.id
|
||||
param: '?scenarioID='+scenarios[i].id
|
||||
});
|
||||
AppDispatcher.dispatch({
|
||||
type: 'configs/start-load',
|
||||
token: this.state.sessionToken,
|
||||
param: '?scenarioID='+scenario.id
|
||||
param: '?scenarioID='+scenarios[i].id
|
||||
});
|
||||
})
|
||||
}
|
||||
}
|
||||
// load widgets when dashboard id(s) are available
|
||||
if (this.state.dashboards.length !== prevState.dashboards.length) {
|
||||
// load widgets when length of dashboard array has increased
|
||||
if (this.state.dashboards.length > prevState.dashboards.length) {
|
||||
let dashboards = Object.assign([], this.state.dashboards);
|
||||
dashboards.forEach(dboard => {
|
||||
for (var j = prevState.dashboards.length; j < dashboards.length; j++) {
|
||||
AppDispatcher.dispatch({
|
||||
type: 'widgets/start-load',
|
||||
token: this.state.sessionToken,
|
||||
param: '?dashboardID='+dboard.id
|
||||
})})
|
||||
param: '?dashboardID='+dashboards[j].id
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -190,10 +189,11 @@ class Scenarios extends Component {
|
|||
};
|
||||
|
||||
exportScenario(index) {
|
||||
console.log("exportScenario")
|
||||
let scenario = this.state.scenarios[index];
|
||||
let configs = this.state.configs.filter(config => config.scenarioID === scenario.id);
|
||||
let dashboards = this.state.dashboards.filter(dashb => dashb.scenarioID === scenario.id);
|
||||
// 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;
|
||||
|
@ -201,16 +201,17 @@ class Scenarios extends Component {
|
|||
jsonObj["configs"] = configs;
|
||||
|
||||
// add Dashboards and Widgets to JSON object
|
||||
let json_dashboards = dashboards;
|
||||
json_dashboards.forEach((dboard) => {
|
||||
let widgets = WidgetStore.getState().filter(w => w.dashboardID === parseInt(dboard.id, 10));
|
||||
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;
|
||||
delete widget.id;
|
||||
})
|
||||
dboard["widgets"] = widgets;
|
||||
delete dboard.scenarioID;
|
||||
delete dboard.id;
|
||||
});
|
||||
jsonObj["dashboards"] = json_dashboards;
|
||||
jsonObj["dashboards"] = dashboards;
|
||||
|
||||
|
||||
// create JSON string and show save dialog
|
||||
|
|
Loading…
Add table
Reference in a new issue