Skip to content

Commit 1a2bfc7

Browse files
committed
Alternative implementation
1 parent 3176914 commit 1a2bfc7

File tree

1 file changed

+35
-23
lines changed

1 file changed

+35
-23
lines changed

client/app/components/dashboards/EditParameterMappingsDialog.jsx

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,34 @@
1-
import { map } from 'lodash';
1+
import { isMatch, map, find, sortBy } from 'lodash';
22
import React from 'react';
33
import PropTypes from 'prop-types';
44
import Modal from 'antd/lib/modal';
55
import { wrap as wrapDialog, DialogPropType } from '@/components/DialogWrapper';
66
import {
7-
MappingType,
87
ParameterMappingListInput,
98
parameterMappingsToEditableMappings,
109
editableMappingsToParameterMappings,
1110
synchronizeWidgetTitles,
1211
} from '@/components/ParameterMappingInput';
12+
import { ParameterMappingType } from '@/services/widget';
13+
14+
export function getParamValuesSnapshot(mappings, dashboardParameters) {
15+
return map(
16+
sortBy(mappings, m => m.name),
17+
(m) => {
18+
let param;
19+
switch (m.type) {
20+
case ParameterMappingType.StaticValue:
21+
return [m.name, m.value];
22+
case ParameterMappingType.WidgetLevel:
23+
return [m.name, m.param.value];
24+
case ParameterMappingType.DashboardLevel:
25+
param = find(dashboardParameters, p => p.name === m.mapTo);
26+
return [m.name, param ? param.value : null];
27+
// no default
28+
}
29+
},
30+
);
31+
}
1332

1433
class EditParameterMappingsDialog extends React.Component {
1534
static propTypes = {
@@ -18,39 +37,34 @@ class EditParameterMappingsDialog extends React.Component {
1837
dialog: DialogPropType.isRequired,
1938
};
2039

21-
originalParamValuesSig = null
22-
2340
constructor(props) {
2441
super(props);
2542

26-
const parameterMappings = parameterMappingsToEditableMappings(
27-
props.widget.options.parameterMappings,
28-
props.widget.query.getParametersDefs(),
29-
map(this.props.dashboard.getParametersDefs(), p => p.name),
30-
);
31-
32-
this.originalParamValuesSig = this.constructor.getParamValuesSignature(parameterMappings);
33-
3443
this.state = {
3544
saveInProgress: false,
36-
parameterMappings,
45+
parameterMappings: parameterMappingsToEditableMappings(
46+
props.widget.options.parameterMappings,
47+
props.widget.query.getParametersDefs(),
48+
map(this.props.dashboard.getParametersDefs(), p => p.name),
49+
),
3750
};
3851
}
3952

40-
static getParamValuesSignature(mappings) {
41-
const s = JSON.stringify;
42-
return mappings
43-
.map(m => (m.type === MappingType.StaticValue ? s(m.value) : s(m.param.value)))
44-
.join();
45-
}
46-
4753
saveWidget() {
4854
const toastr = this.props.toastr; // eslint-disable-line react/prop-types
4955
const widget = this.props.widget;
5056

5157
this.setState({ saveInProgress: true });
5258

53-
widget.options.parameterMappings = editableMappingsToParameterMappings(this.state.parameterMappings);
59+
const prevMappings = widget.options.parameterMappings;
60+
const newMappings = editableMappingsToParameterMappings(this.state.parameterMappings);
61+
widget.options.parameterMappings = newMappings;
62+
63+
const dashboardParameters = this.props.dashboard.getParametersDefs();
64+
const valuesChanged = !isMatch(
65+
getParamValuesSnapshot(prevMappings, dashboardParameters),
66+
getParamValuesSnapshot(newMappings, dashboardParameters),
67+
);
5468

5569
const widgetsToSave = [
5670
widget,
@@ -59,8 +73,6 @@ class EditParameterMappingsDialog extends React.Component {
5973

6074
Promise.all(map(widgetsToSave, w => w.save()))
6175
.then(() => {
62-
const paramValuesSig = this.constructor.getParamValuesSignature(this.state.parameterMappings);
63-
const valuesChanged = this.originalParamValuesSig !== paramValuesSig;
6476
this.props.dialog.close(valuesChanged);
6577
})
6678
.catch(() => {

0 commit comments

Comments
 (0)