Skip to content
Snippets Groups Projects
Unverified Commit 16c9f4ea authored by Tom Robinson's avatar Tom Robinson
Browse files

Refactor AggregationWidget for named aggregations

parent d41222f4
No related branches found
No related tags found
No related merge requests found
...@@ -15,7 +15,7 @@ export function format(expr, { ...@@ -15,7 +15,7 @@ export function format(expr, {
aggregations = VALID_AGGREGATIONS aggregations = VALID_AGGREGATIONS
}, parens = false) { }, parens = false) {
const info = { tableMetadata, customFields, operators, aggregations }; const info = { tableMetadata, customFields, operators, aggregations };
if (expr == null) { if (expr == null || _.isEqual(expr, [])) {
return ""; return "";
} }
if (typeof expr === "number") { if (typeof expr === "number") {
......
...@@ -51,31 +51,26 @@ export default class AggregationWidget extends Component { ...@@ -51,31 +51,26 @@ export default class AggregationWidget extends Component {
const fieldId = AggregationClause.getField(aggregation); const fieldId = AggregationClause.getField(aggregation);
let selectedAggregation = getAggregator(AggregationClause.getOperator(aggregation)); let selectedAggregation = getAggregator(AggregationClause.getOperator(aggregation));
if (selectedAggregation && !_.findWhere(tableMetadata.aggregation_options, { short: selectedAggregation.short })) { // if this table doesn't support the selected aggregation, prompt the user to select a different one
// if this table doesn't support the selected aggregation, prompt the user to select a different one if (selectedAggregation && _.findWhere(tableMetadata.aggregation_options, { short: selectedAggregation.short })) {
selectedAggregation = null; return (
<span className="flex align-center">
{ selectedAggregation.name.replace(" of ...", "") }
{ fieldId &&
<span style={{paddingRight: "4px", paddingLeft: "4px"}} className="text-bold">of</span>
}
{ fieldId &&
<FieldName
className="View-section-aggregation-target SelectionModule py1"
tableMetadata={tableMetadata}
field={fieldId}
fieldOptions={Query.getFieldOptions(tableMetadata.fields, true)}
customFieldOptions={this.props.customFields}
/>
}
</span>
);
} }
return (
<span className="View-section-aggregation QueryOption py1 mx1 flex align-center">
{ selectedAggregation ?
selectedAggregation.name.replace(" of ...", "")
:
"Choose an aggregation"
}
{ selectedAggregation && fieldId &&
<span style={{paddingRight: "4px", paddingLeft: "4px"}} className="text-bold">of</span>
}
{ selectedAggregation && fieldId &&
<FieldName
className="View-section-aggregation-target SelectionModule py1"
tableMetadata={tableMetadata}
field={fieldId}
fieldOptions={Query.getFieldOptions(tableMetadata.fields, true)}
customFieldOptions={this.props.customFields}
/>
}
</span>
);
} }
renderMetricAggregation() { renderMetricAggregation() {
...@@ -83,16 +78,14 @@ export default class AggregationWidget extends Component { ...@@ -83,16 +78,14 @@ export default class AggregationWidget extends Component {
const metricId = AggregationClause.getMetric(aggregation); const metricId = AggregationClause.getMetric(aggregation);
let selectedMetric = _.findWhere(tableMetadata.metrics, { id: metricId }); let selectedMetric = _.findWhere(tableMetadata.metrics, { id: metricId });
return ( if (selectedMetric) {
<span className="View-section-aggregation QueryOption p1">{selectedMetric ? selectedMetric.name.replace(" of ...", "") : "Choose an aggregation"}</span> return selectedMetric.name.replace(" of ...", "")
); }
} }
renderCustomAggregation() { renderCustomAggregation() {
const { aggregation, tableMetadata, customFields } = this.props; const { aggregation, tableMetadata, customFields } = this.props;
return ( return format(aggregation, { tableMetadata, customFields });
<span className="View-section-aggregation QueryOption p1">{aggregation ? format(aggregation, { tableMetadata, customFields }) : "Choose an aggregation"}</span>
);
} }
renderPopover() { renderPopover() {
...@@ -121,20 +114,29 @@ export default class AggregationWidget extends Component { ...@@ -121,20 +114,29 @@ export default class AggregationWidget extends Component {
} }
render() { render() {
const { aggregation, addButton } = this.props; const { aggregation, addButton, name } = this.props;
if (aggregation && aggregation.length > 0) { if (aggregation && aggregation.length > 0) {
let aggregationName = AggregationClause.isCustom(aggregation) ?
this.renderCustomAggregation()
: AggregationClause.isMetric(aggregation) ?
this.renderMetricAggregation()
:
this.renderStandardAggregation()
return ( return (
<div className={cx("Query-section Query-section-aggregation", { "selected": this.state.isOpen })}> <div className={cx("Query-section Query-section-aggregation", { "selected": this.state.isOpen })}>
<div> <div>
<Clearable onClear={this.props.removeAggregation}> <Clearable onClear={this.props.removeAggregation}>
<div id="Query-section-aggregation" onClick={this.open} className="Query-section Query-section-aggregation cursor-pointer"> <div id="Query-section-aggregation" onClick={this.open} className="Query-section Query-section-aggregation cursor-pointer">
{ AggregationClause.isCustom(aggregation) ? <span className="View-section-aggregation QueryOption py1 mx1">
this.renderCustomAggregation() { aggregationName == null ?
: AggregationClause.isMetric(aggregation) ? "Choose an aggregation"
this.renderMetricAggregation() : name ?
: name
this.renderStandardAggregation() :
} aggregationName
}
</span>
</div> </div>
</Clearable> </Clearable>
{this.renderPopover()} {this.renderPopover()}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment