Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: widget oblivious to updated parameter values #3445

Merged
merged 6 commits into from
Feb 18, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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