Skip to content
This repository has been archived by the owner on Dec 10, 2021. It is now read-only.

Commit

Permalink
Refactor duplicated code
Browse files Browse the repository at this point in the history
  • Loading branch information
kgabryje committed May 18, 2021
1 parent 2e40039 commit 05401c4
Showing 1 changed file with 25 additions and 23 deletions.
48 changes: 25 additions & 23 deletions plugins/plugin-chart-table/src/controlPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import {
ControlPanelState,
ExtraControlProps,
TIME_COLUMN_OPTION,
ControlState,
} from '@superset-ui/chart-controls';

import i18n from './i18n';
Expand Down Expand Up @@ -69,6 +70,13 @@ function isQueryMode(mode: QueryMode) {
const isAggMode = isQueryMode(QueryMode.aggregate);
const isRawMode = isQueryMode(QueryMode.raw);

const validateAggControlValues = (controls: ControlStateMapping, values: any[]) => {
const areControlsEmpty = values.every(val => ensureIsArray(val).length === 0);
return areControlsEmpty && isAggMode({ controls })
? [t('Group By, Metrics or Percentage Metrics must have a value')]
: [];
};

const queryMode: ControlConfig<'RadioButtonControl'> = {
type: 'RadioButtonControl',
label: t('Query mode'),
Expand Down Expand Up @@ -117,13 +125,11 @@ const percent_metrics: typeof sharedControls.metrics = {
savedMetrics: datasource?.metrics || [],
datasourceType: datasource?.type,
queryMode: getQueryMode(controls),
externalValidationErrors:
isAggMode({ controls }) &&
ensureIsArray(controls.groupby?.value).length === 0 &&
ensureIsArray(controls.metrics?.value).length === 0 &&
ensureIsArray(controlState.value).length === 0
? [t('Group By, Metrics or Percentage Metrics must have a value')]
: [],
externalValidationErrors: validateAggControlValues(controls, [
controls.groupby?.value,
controls.metrics?.value,
controlState.value,
]),
}),
rerender: ['groupby', 'metrics'],
default: [],
Expand Down Expand Up @@ -155,7 +161,7 @@ const config: ControlPanelConfig = {
visibility: isAggMode,
mapStateToProps: (
{ datasource, controls }: ControlPanelState,
controlState: ExtraControlProps,
controlState: ControlState,
) => {
const newState: ExtraControlProps = {};
if (datasource) {
Expand All @@ -167,13 +173,11 @@ const config: ControlPanelConfig = {
? Object.fromEntries(options.map(option => [option.column_name, option]))
: options;
}
newState.externalValidationErrors =
isAggMode({ controls }) &&
ensureIsArray(controls.metrics?.value).length === 0 &&
ensureIsArray(controls.percent_metrics?.value).length === 0 &&
ensureIsArray(controlState.value).length === 0
? [t('Group By, Metrics or Percentage Metrics must have a value')]
: [];
newState.externalValidationErrors = validateAggControlValues(controls, [
controls.metrics?.value,
controls.percent_metrics?.value,
controlState.value,
]);
return newState;
},
rerender: ['metrics', 'percent_metrics'],
Expand All @@ -188,20 +192,18 @@ const config: ControlPanelConfig = {
visibility: isAggMode,
mapStateToProps: (
{ controls, datasource, form_data }: ControlPanelState,
controlState: ExtraControlProps,
controlState: ControlState,
) => ({
columns: datasource?.columns.filter(c => c.filterable) || [],
savedMetrics: datasource?.metrics || [],
// current active adhoc metrics
selectedMetrics: form_data.metrics || (form_data.metric ? [form_data.metric] : []),
datasource,
externalValidationErrors:
isAggMode({ controls }) &&
ensureIsArray(controls.groupby?.value).length === 0 &&
ensureIsArray(controls.percent_metrics?.value).length === 0 &&
ensureIsArray(controlState.value).length === 0
? [t('Group By, Metrics or Percentage Metrics must have a value')]
: [],
externalValidationErrors: validateAggControlValues(controls, [
controls.groupby?.value,
controls.percent_metrics?.value,
controlState.value,
]),
}),
rerender: ['groupby', 'percent_metrics'],
},
Expand Down

0 comments on commit 05401c4

Please sign in to comment.