Skip to content

Commit

Permalink
feat: reset metrics on dataset change (#12782)
Browse files Browse the repository at this point in the history
* reset metrics on dataset change take one

* remove code

* part 2 or reseting adhoc controls

* update input controls and customize defaults

* remove conosles

* remove extra method

* simplify logic for controls reset and have them use their defaults

* remove consoles

* add annotation control to defaultvalues

* remove line
  • Loading branch information
pkdotson authored Feb 5, 2021
1 parent ac3e16d commit 3bb14ab
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,28 @@ class ControlPanelsContainer extends React.Component {
this.renderControlPanelSection = this.renderControlPanelSection.bind(this);
}

componentDidUpdate(prevProps) {
const {
actions: { setControlValue },
} = this.props;
if (this.props.form_data.datasource !== prevProps.form_data.datasource) {
const defaultValues = [
'MetricsControl',
'AdhocFilterControl',
'TextControl',
'SelectControl',
'CheckboxControl',
'AnnotationLayerControl',
];
Object.entries(this.props.controls).forEach(([controlName, control]) => {
const { type, default: defaultValue } = control;
if (defaultValues.includes(type)) {
setControlValue(controlName, defaultValue);
}
});
}
}

sectionsToRender() {
return sectionsToRender(
this.props.form_data.viz_type,
Expand Down Expand Up @@ -253,7 +275,6 @@ class ControlPanelsContainer extends React.Component {
const expandedCustomSections = this.sectionsToExpand(
displaySectionsToRender,
);

return (
<Styles>
{this.props.alert && (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ export default class CollectionControl extends React.Component {
this.onAdd = this.onAdd.bind(this);
}

componentDidUpdate(prevProps) {
if (prevProps.datasource.name !== this.props.datasource.name) {
this.props.onChange([]);
}
}

onChange(i, value) {
Object.assign(this.props.value[i], value);
this.props.onChange(this.props.value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ export default class AdhocMetricEditPopover extends React.PureComponent {
adhocMetricLabel: this.state.adhocMetric?.getDefaultLabel(),
});
}
if (prevProps.datasource !== this.props.datasource) {
this.props.onChange(null);
}
}

componentWillUnmount() {
Expand Down Expand Up @@ -272,7 +275,6 @@ export default class AdhocMetricEditPopover extends React.PureComponent {
datasourceType,
...popoverProps
} = this.props;

const { adhocMetric, savedMetric } = this.state;
const keywords = sqlKeywords.concat(
columns.map(column => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,17 @@ class AdhocMetricOption extends React.PureComponent {
onMoveLabel,
onDropLabel,
index,
datasource,
} = this.props;

return (
<AdhocMetricPopoverTrigger
adhocMetric={adhocMetric}
onMetricEdit={onMetricEdit}
columns={columns}
savedMetricsOptions={savedMetricsOptions}
savedMetric={savedMetric}
datasource={datasource}
datasourceType={datasourceType}
>
<OptionControlLabel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export type AdhocMetricPopoverTriggerProps = {
savedMetricsOptions: savedMetricType[];
savedMetric: savedMetricType;
datasourceType: string;
datasource: string;
children: ReactNode;
createNew?: boolean;
};
Expand Down Expand Up @@ -159,6 +160,7 @@ class AdhocMetricPopoverTrigger extends React.PureComponent<
columns={this.props.columns}
savedMetricsOptions={this.props.savedMetricsOptions}
savedMetric={this.props.savedMetric}
datasource={this.props.datasource}
datasourceType={this.props.datasourceType}
onResize={this.onPopoverResize}
onClose={this.closePopover}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const propTypes = {
savedMetricsOptions: PropTypes.arrayOf(savedMetricType),
multi: PropTypes.bool,
datasourceType: PropTypes.string,
datasource: PropTypes.string,
};

export default function MetricDefinitionValue({
Expand All @@ -51,6 +52,7 @@ export default function MetricDefinitionValue({
onMoveLabel,
onDropLabel,
index,
datasource,
}) {
const getSavedMetricByName = metricName =>
savedMetrics.find(metric => metric.metric_name === metricName);
Expand All @@ -77,6 +79,7 @@ export default function MetricDefinitionValue({
onDropLabel,
index,
savedMetric: savedMetric ?? {},
datasource,
};

return <AdhocMetricOption {...metricOptionProps} />;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ class MetricsControl extends React.PureComponent {
onMetricEdit={this.onMetricEdit}
onRemoveMetric={() => this.onRemoveMetric(index)}
columns={this.props.columns}
datasource={this.props.datasource}
savedMetrics={this.props.savedMetrics}
savedMetricsOptions={getOptionsForSavedMetrics(
this.props.savedMetrics,
Expand Down Expand Up @@ -292,6 +293,7 @@ class MetricsControl extends React.PureComponent {
this.props.value,
null,
)}
datasource={this.props.datasource}
savedMetric={{}}
datasourceType={this.props.datasourceType}
createNew
Expand Down Expand Up @@ -375,7 +377,6 @@ class MetricsControl extends React.PureComponent {

render() {
const { theme } = this.props;

return (
<div className="metrics-select">
<HeaderContainer>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,10 @@ const defaultProps = {
export default class SelectControl extends React.PureComponent {
constructor(props) {
super(props);
this.state = { options: this.getOptions(props) };
this.state = {
options: this.getOptions(props),
value: this.props.value,
};
this.onChange = this.onChange.bind(this);
this.createMetaSelectAllOption = this.createMetaSelectAllOption.bind(this);
this.select = null; // pointer to the react-select instance
Expand Down Expand Up @@ -240,7 +243,12 @@ export default class SelectControl extends React.PureComponent {
const isMulti = this.props.isMulti || this.props.multi;

let assistiveText;
if (isMulti && optionsRemaining && Array.isArray(value) && !!value.length) {
if (
isMulti &&
optionsRemaining &&
Array.isArray(this.state.value) &&
!!value.length
) {
assistiveText = optionRemaingText;
}

Expand All @@ -262,12 +270,12 @@ export default class SelectControl extends React.PureComponent {
onChange: this.onChange,
onFocus,
optionRenderer,
value,
options: this.state.options,
placeholder,
assistiveText,
promptTextCreator,
selectRef: this.getSelectRef,
value,
valueKey,
valueRenderer,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ interface TextControlProps {
value?: string | number;
controlId?: string;
renderTrigger?: boolean;
datasource?: string;
}

interface TextControlState {
Expand All @@ -50,6 +51,16 @@ export default class TextControl extends React.Component<
this.onChange(inputValue);
}, 500);

static getDerivedStateFromProps(
props: TextControlProps,
state: TextControlState,
) {
if (props.value !== state.value) {
return { value: props.value };
}
return null;
}

constructor(props: TextControlProps) {
super(props);

Expand All @@ -61,6 +72,10 @@ export default class TextControl extends React.Component<
};
}

defaultInput = () => {
this.setState({ value: '' });
};

onChange = (inputValue: string) => {
let parsedValue: string | number = inputValue;
// Validation & casting
Expand Down Expand Up @@ -103,7 +118,6 @@ export default class TextControl extends React.Component<
typeof rawValue !== 'undefined' && rawValue !== null
? rawValue.toString()
: '';

return (
<div>
<ControlHeader {...this.props} />
Expand Down

0 comments on commit 3bb14ab

Please sign in to comment.