Skip to content

Commit

Permalink
[Bug fix] Widget oblivious to updated parameter values (#3445)
Browse files Browse the repository at this point in the history
  • Loading branch information
ranbena authored and kravets-levko committed Feb 18, 2019
1 parent 3147a0b commit 8fc2ecf
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
35 changes: 32 additions & 3 deletions client/app/components/dashboards/EditParameterMappingsDialog.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { map } from 'lodash';
import { isMatch, map, find, sortBy } from 'lodash';
import React from 'react';
import PropTypes from 'prop-types';
import Modal from 'antd/lib/modal';
Expand All @@ -9,6 +9,26 @@ import {
editableMappingsToParameterMappings,
synchronizeWidgetTitles,
} from '@/components/ParameterMappingInput';
import { ParameterMappingType } from '@/services/widget';

export function getParamValuesSnapshot(mappings, dashboardParameters) {
return map(
sortBy(mappings, m => m.name),
(m) => {
let param;
switch (m.type) {
case ParameterMappingType.StaticValue:
return [m.name, m.value];
case ParameterMappingType.WidgetLevel:
return [m.name, m.param.value];
case ParameterMappingType.DashboardLevel:
param = find(dashboardParameters, p => p.name === m.mapTo);
return [m.name, param ? param.value : null];
// no default
}
},
);
}

class EditParameterMappingsDialog extends React.Component {
static propTypes = {
Expand All @@ -19,6 +39,7 @@ class EditParameterMappingsDialog extends React.Component {

constructor(props) {
super(props);

this.state = {
saveInProgress: false,
parameterMappings: parameterMappingsToEditableMappings(
Expand All @@ -35,7 +56,15 @@ class EditParameterMappingsDialog extends React.Component {

this.setState({ saveInProgress: true });

widget.options.parameterMappings = editableMappingsToParameterMappings(this.state.parameterMappings);
const prevMappings = widget.options.parameterMappings;
const newMappings = editableMappingsToParameterMappings(this.state.parameterMappings);
widget.options.parameterMappings = newMappings;

const dashboardParameters = this.props.dashboard.getParametersDefs();
const valuesChanged = !isMatch(
getParamValuesSnapshot(prevMappings, dashboardParameters),
getParamValuesSnapshot(newMappings, dashboardParameters),
);

const widgetsToSave = [
widget,
Expand All @@ -44,7 +73,7 @@ class EditParameterMappingsDialog extends React.Component {

Promise.all(map(widgetsToSave, w => w.save()))
.then(() => {
this.props.dialog.close();
this.props.dialog.close(valuesChanged);
})
.catch(() => {
toastr.error('Widget cannot be updated');
Expand Down
9 changes: 7 additions & 2 deletions client/app/components/dashboards/widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const EditTextBoxComponent = {
},
};

function DashboardWidgetCtrl($scope, $location, $uibModal, $window, $rootScope, Events, currentUser) {
function DashboardWidgetCtrl($scope, $location, $uibModal, $window, $rootScope, $timeout, Events, currentUser) {
this.canViewQuery = currentUser.hasPermission('view_query');

this.editTextBox = () => {
Expand Down Expand Up @@ -82,8 +82,13 @@ function DashboardWidgetCtrl($scope, $location, $uibModal, $window, $rootScope,
EditParameterMappingsDialog.showModal({
dashboard: this.dashboard,
widget: this.widget,
}).result.then(() => {
}).result.then((valuesChanged) => {
this.localParameters = null;

// refresh widget if any parameter value has been updated
if (valuesChanged) {
$timeout(() => this.refresh());
}
$scope.$applyAsync();
$rootScope.$broadcast('dashboard.update-parameters');
});
Expand Down

0 comments on commit 8fc2ecf

Please sign in to comment.