-
Notifications
You must be signed in to change notification settings - Fork 916
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[D&D] Persists index field on agg type change if possible #2227
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'; | ||
|
||
|
@@ -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'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we need the cast because the agg service doesn't give us suitable types? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll be honest, that service has some seriously confusing typing going on that seems to confuse typescript a lot. Here the issue was that the line below where i try to |
||
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())); | ||
}} | ||
/> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
boo to lodash methods 😉
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I blame typescript. I gave into this after fighting with typescript to get the same behavior for this: