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 query based dropdown not adding quote marks correctly #4186

Merged
merged 6 commits into from
Oct 6, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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
41 changes: 26 additions & 15 deletions client/app/components/QueryBasedParameterInput.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { find, isFunction, isArray, isEqual, toString, map, intersection } from 'lodash';
import { find, isArray, map, intersection, isEqual } from 'lodash';
import React from 'react';
import PropTypes from 'prop-types';
import { react2angular } from 'react2angular';
Expand Down Expand Up @@ -29,6 +29,7 @@ export class QueryBasedParameterInput extends React.Component {
super(props);
this.state = {
options: [],
value: null,
loading: false,
};
}
Expand All @@ -41,6 +42,24 @@ export class QueryBasedParameterInput extends React.Component {
if (this.props.queryId !== prevProps.queryId) {
this._loadOptions(this.props.queryId);
}
if (this.props.value !== prevProps.value) {
this.setValue(this.props.value);
}
}

setValue(value) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now using a value from state + this method to make sure no invalid values will go to the Select component.

const { options } = this.state;
if (this.props.mode === 'multiple') {
value = isArray(value) ? value : [value];
const optionValues = map(options, option => option.value);
const validValues = intersection(value, optionValues);
this.setState({ value: validValues });
return validValues;
}
const found = find(options, option => option.value === this.props.value) !== undefined;
value = found ? value : options[0].value;
this.setState({ value });
return value;
}

async _loadOptions(queryId) {
Expand All @@ -50,20 +69,12 @@ export class QueryBasedParameterInput extends React.Component {

// stale queryId check
if (this.props.queryId === queryId) {
this.setState({ options, loading: false });

if (this.props.mode === 'multiple' && isArray(this.props.value)) {
const optionValues = map(options, option => option.value);
const validValues = intersection(this.props.value, optionValues);
if (!isEqual(this.props.value, validValues)) {
this.props.onSelect(validValues);
}
} else {
const found = find(options, option => option.value === this.props.value) !== undefined;
if (!found && isFunction(this.props.onSelect)) {
this.props.onSelect(options[0].value);
this.setState({ options, loading: false }, () => {
const updatedValue = this.setValue(this.props.value);
if (!isEqual(updatedValue, this.props.value)) {
this.props.onSelect(updatedValue);
}
}
});
}
}
}
Expand All @@ -78,7 +89,7 @@ export class QueryBasedParameterInput extends React.Component {
disabled={loading || (options.length === 0)}
loading={loading}
mode={mode}
value={isArray(value) ? value : toString(value)}
value={this.state.value}
onChange={onSelect}
dropdownMatchSelectWidth={false}
optionFilterProp="children"
Expand Down
9 changes: 8 additions & 1 deletion client/app/services/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,12 @@ export class Parameter {
}
}

if (this.type === 'query' && !isEmptyValue(value)) {
if (this.multiValuesOptions && !isArray(value) && value !== null) {
ranbena marked this conversation as resolved.
Show resolved Hide resolved
value = [value];
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

QueryBasedDropdown with multi-values should always hold array values in its structure.

}
}

if (isDateRangeParameter(this.type)) {
this.value = null;
this.$$value = null;
Expand Down Expand Up @@ -387,7 +393,8 @@ export class Parameter {
if (has(query, key)) {
if (this.multiValuesOptions) {
try {
this.setValue(JSON.parse(query[key]));
const valueFromJson = JSON.parse(query[key]);
this.setValue(isArray(valueFromJson) ? valueFromJson : query[key]);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Turns out numbers are also parsed and converted 😅, so this makes sure we don't convert it unless it's an array.

} catch (e) {
this.setValue(query[key]);
}
Expand Down