Skip to content

Commit

Permalink
keep current quick function
Browse files Browse the repository at this point in the history
  • Loading branch information
flash1293 committed May 31, 2021
1 parent 3637ee6 commit ed47705
Showing 1 changed file with 24 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
EuiTab,
EuiCallOut,
} from '@elastic/eui';
import useUnmount from 'react-use/lib/useUnmount';
import { IndexPatternDimensionEditorProps } from './dimension_panel';
import { OperationSupportMatrix } from './operation_support';
import { IndexPatternColumn } from '../indexpattern';
Expand All @@ -40,7 +41,7 @@ import { mergeLayer } from '../state_helpers';
import { FieldSelect } from './field_select';
import { hasField, fieldIsInvalid } from '../utils';
import { BucketNestingEditor } from './bucket_nesting_editor';
import { IndexPattern, IndexPatternLayer } from '../types';
import { IndexPattern, IndexPatternLayer, IndexPatternPrivateState } from '../types';
import { trackUiEvent } from '../../lens_ui_telemetry';
import { FormatSelector } from './format_selector';
import { ReferenceEditor } from './reference_editor';
Expand Down Expand Up @@ -84,7 +85,7 @@ const LabelInput = ({ value, onChange }: { value: string; onChange: (value: stri

export function DimensionEditor(props: DimensionEditorProps) {
const {
selectedColumn,
selectedColumn: upstreamSelectedColumn,
operationSupportMatrix,
state,
columnId,
Expand All @@ -106,19 +107,39 @@ export function DimensionEditor(props: DimensionEditorProps) {
};
const { fieldByOperation, operationWithoutField } = operationSupportMatrix;

const [previousQuickFunctionState, setPreviousQuickFunctionState] = useState<
IndexPatternPrivateState | undefined
>(undefined);

const [temporaryQuickFunction, setQuickFunction] = useState(false);

const selectedColumn =
temporaryQuickFunction && previousQuickFunctionState
? previousQuickFunctionState.layers[layerId].columns[columnId]
: upstreamSelectedColumn;

const selectedOperationDefinition =
selectedColumn && operationDefinitionMap[selectedColumn.operationType];

const [changedFormula, setChangedFormula] = useState(
Boolean(selectedOperationDefinition?.type === 'formula')
);

useUnmount(() => {
if (temporaryQuickFunction && previousQuickFunctionState) {
setState(previousQuickFunctionState, {
shouldClose: true,
});
}
});

const setStateWrapper = (
setter: IndexPatternLayer | ((prevLayer: IndexPatternLayer) => IndexPatternLayer),
shouldClose?: boolean
) => {
if (selectedOperationDefinition?.type === 'formula' && !temporaryQuickFunction) {
setChangedFormula(true);
setPreviousQuickFunctionState(undefined);
} else {
setChangedFormula(false);
}
Expand Down Expand Up @@ -152,8 +173,6 @@ export function DimensionEditor(props: DimensionEditorProps) {

const ParamEditor = selectedOperationDefinition?.paramEditor;

const [temporaryQuickFunction, setQuickFunction] = useState(false);

const possibleOperations = useMemo(() => {
return Object.values(operationDefinitionMap)
.filter(({ hidden }) => !hidden)
Expand Down Expand Up @@ -615,6 +634,7 @@ export function DimensionEditor(props: DimensionEditorProps) {
onClick={() => {
if (selectedColumn?.operationType !== 'formula') {
setQuickFunction(false);
setPreviousQuickFunctionState(state);
const newLayer = insertOrReplaceColumn({
layer: props.state.layers[props.layerId],
indexPattern: currentIndexPattern,
Expand Down

0 comments on commit ed47705

Please sign in to comment.