Skip to content

Commit 4a06f5a

Browse files
authored
Persists index field on agg type change if possible (#2227)
Signed-off-by: Ashwin Pc <ashwinpc@amazon.com>
1 parent 140c56f commit 4a06f5a

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

src/plugins/wizard/public/application/components/data_tab/secondary_panel.tsx

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
*/
55

66
import React, { useCallback, useMemo, useState } from 'react';
7-
import { cloneDeep } from 'lodash';
7+
import { cloneDeep, get } from 'lodash';
88
import { useDebounce } from 'react-use';
99
import { useTypedDispatch, useTypedSelector } from '../../utils/state_management';
1010
import { DefaultEditorAggParams } from '../../../../../vis_default_editor/public';
1111
import { Title } from './title';
1212
import { useIndexPatterns, useVisualizationType } from '../../utils/use';
1313
import { useOpenSearchDashboards } from '../../../../../opensearch_dashboards_react/public';
1414
import { WizardServices } from '../../../types';
15-
import { IAggType } from '../../../../../data/public';
15+
import { AggParam, IAggType, IFieldParamType } from '../../../../../data/public';
1616
import { saveDraftAgg, editDraftAgg } from '../../utils/state_management/visualization_slice';
1717
import { setValidity } from '../../utils/state_management/metadata_slice';
1818

@@ -109,6 +109,20 @@ export function SecondaryPanel() {
109109
}}
110110
onAggTypeChange={function (aggId: string, aggType: IAggType): void {
111111
aggConfig.type = aggType;
112+
113+
// Persist field if the new agg type supports the existing field
114+
const fieldParam = (aggType.params as AggParam[]).find(({ type }) => type === 'field');
115+
if (fieldParam) {
116+
const availableFields = (fieldParam as IFieldParamType).getAvailableFields(aggConfig);
117+
const indexField = availableFields.find(
118+
({ name }) => name === get(draftAgg, 'params.field')
119+
);
120+
121+
if (indexField) {
122+
aggConfig.params.field = indexField;
123+
}
124+
}
125+
112126
dispatch(editDraftAgg(aggConfig.serialize()));
113127
}}
114128
/>

0 commit comments

Comments
 (0)