Skip to content

Commit

Permalink
Moved widget and dashboard save logic out of dialog (#3522)
Browse files Browse the repository at this point in the history
  • Loading branch information
ranbena authored Mar 4, 2019
1 parent ade3cc7 commit dd0fab7
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 32 deletions.
31 changes: 4 additions & 27 deletions client/app/components/dashboards/AddWidgetDialog.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@ import { wrap as wrapDialog, DialogPropType } from '@/components/DialogWrapper';
import {
MappingType,
ParameterMappingListInput,
editableMappingsToParameterMappings,
synchronizeWidgetTitles,
} from '@/components/ParameterMappingInput';
import { QuerySelector } from '@/components/QuerySelector';

import { toastr } from '@/services/ng';
import { Widget } from '@/services/widget';

import { Query } from '@/services/query';

const { Option, OptGroup } = Select;
Expand All @@ -22,6 +20,7 @@ class AddWidgetDialog extends React.Component {
static propTypes = {
dashboard: PropTypes.object.isRequired, // eslint-disable-line react/forbid-prop-types
dialog: DialogPropType.isRequired,
onConfirm: PropTypes.func.isRequired,
};

state = {
Expand Down Expand Up @@ -76,34 +75,12 @@ class AddWidgetDialog extends React.Component {
}

saveWidget() {
const dashboard = this.props.dashboard;
const { selectedVis, parameterMappings } = this.state;

this.setState({ saveInProgress: true });

const widget = new Widget({
visualization_id: this.state.selectedVis && this.state.selectedVis.id,
dashboard_id: dashboard.id,
options: {
isHidden: false,
position: {},
parameterMappings: editableMappingsToParameterMappings(this.state.parameterMappings),
},
visualization: this.state.selectedVis,
text: '',
});

const position = dashboard.calculateNewWidgetPosition(widget);
widget.options.position.col = position.col;
widget.options.position.row = position.row;

const widgetsToSave = [
widget,
...synchronizeWidgetTitles(widget.options.parameterMappings, dashboard.widgets),
];

Promise.all(map(widgetsToSave, w => w.save()))
this.props.onConfirm(selectedVis, parameterMappings)
.then(() => {
dashboard.widgets.push(widget);
this.props.dialog.close();
})
.catch(() => {
Expand Down
4 changes: 2 additions & 2 deletions client/app/pages/dashboards/dashboard.html
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ <h2>
<span class="hidden-xs hidden-sm">Widgets are individual query visualizations or text boxes you can place on your dashboard in various arrangements.</span>
</h2>
<div>
<a class="btn btn-default" ng-click="$ctrl.addTextBox()">Add Textbox</a>
<a class="btn btn-primary m-l-10" ng-click="$ctrl.addWidget('widget')">Add Widget</a>
<a class="btn btn-default" ng-click="$ctrl.showAddTextboxDialog()">Add Textbox</a>
<a class="btn btn-primary m-l-10" ng-click="$ctrl.showAddWidgetDialog()">Add Widget</a>
</div>
</div>
</div>
42 changes: 39 additions & 3 deletions client/app/pages/dashboards/dashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import * as _ from 'lodash';
import PromiseRejectionError from '@/lib/promise-rejection-error';
import getTags from '@/services/getTags';
import { policy } from '@/services/policy';
import { Widget } from '@/services/widget';
import {
editableMappingsToParameterMappings,
synchronizeWidgetTitles,
} from '@/components/ParameterMappingInput';
import { durationHumanize } from '@/filters';
import template from './dashboard.html';
import ShareDashboardDialog from './ShareDashboardDialog';
Expand Down Expand Up @@ -319,7 +324,7 @@ function DashboardCtrl(
);
};

this.addTextBox = () => {
this.showAddTextboxDialog = () => {
$uibModal
.open({
component: 'addTextboxDialog',
Expand All @@ -330,14 +335,45 @@ function DashboardCtrl(
.result.then(this.onWidgetAdded);
};

this.addWidget = () => {
AddWidgetDialog.showModal({ dashboard: this.dashboard })
this.showAddWidgetDialog = () => {
AddWidgetDialog.showModal({
dashboard: this.dashboard,
onConfirm: this.addWidget,
})
.result.then(() => {
this.onWidgetAdded();
$scope.$applyAsync();
});
};

this.addWidget = (selectedVis, parameterMappings) => {
const widget = new Widget({
visualization_id: selectedVis && selectedVis.id,
dashboard_id: this.dashboard.id,
options: {
isHidden: false,
position: {},
parameterMappings: editableMappingsToParameterMappings(parameterMappings),
},
visualization: selectedVis,
text: '',
});

const position = this.dashboard.calculateNewWidgetPosition(widget);
widget.options.position.col = position.col;
widget.options.position.row = position.row;

const widgetsToSave = [
widget,
...synchronizeWidgetTitles(widget.options.parameterMappings, this.dashboard.widgets),
];

return Promise.all(widgetsToSave.map(w => w.save()))
.then(() => {
this.dashboard.widgets.push(widget);
});
};

this.onWidgetAdded = () => {
this.extractGlobalParameters();
// Save position of newly added widget (but not entire layout)
Expand Down

0 comments on commit dd0fab7

Please sign in to comment.