Skip to content

Commit

Permalink
Persists index field on agg type change if possible (opensearch-proje…
Browse files Browse the repository at this point in the history
…ct#2227)

Signed-off-by: Ashwin Pc <[email protected]>
Signed-off-by: Sergey V. Osipov <[email protected]>
  • Loading branch information
ashwin-pc authored and sipopo committed Dec 16, 2022
1 parent a746d4a commit b55b9bd
Showing 1 changed file with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
*/

import React, { useCallback, useMemo, useState } from 'react';
import { cloneDeep } from 'lodash';
import { cloneDeep, get } from 'lodash';
import { useDebounce } from 'react-use';
import { useTypedDispatch, useTypedSelector } from '../../utils/state_management';
import { DefaultEditorAggParams } from '../../../../../vis_default_editor/public';
import { Title } from './title';
import { useIndexPatterns, useVisualizationType } from '../../utils/use';
import { useOpenSearchDashboards } from '../../../../../opensearch_dashboards_react/public';
import { WizardServices } from '../../../types';
import { IAggType } from '../../../../../data/public';
import { AggParam, IAggType, IFieldParamType } from '../../../../../data/public';
import { saveDraftAgg, editDraftAgg } from '../../utils/state_management/visualization_slice';
import { setValidity } from '../../utils/state_management/metadata_slice';

Expand Down Expand Up @@ -109,6 +109,20 @@ export function SecondaryPanel() {
}}
onAggTypeChange={function (aggId: string, aggType: IAggType): void {
aggConfig.type = aggType;

// Persist field if the new agg type supports the existing field
const fieldParam = (aggType.params as AggParam[]).find(({ type }) => type === 'field');
if (fieldParam) {
const availableFields = (fieldParam as IFieldParamType).getAvailableFields(aggConfig);
const indexField = availableFields.find(
({ name }) => name === get(draftAgg, 'params.field')
);

if (indexField) {
aggConfig.params.field = indexField;
}
}

dispatch(editDraftAgg(aggConfig.serialize()));
}}
/>
Expand Down

0 comments on commit b55b9bd

Please sign in to comment.