-
Notifications
You must be signed in to change notification settings - Fork 14.6k
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
feat(native-filters): add optional sort metric to select filter #14346
Merged
villebro
merged 10 commits into
apache:master
from
preset-io:villebro/native-select-sort-metric
Apr 27, 2021
Merged
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d48c541
feat(native-filters): add optional sort metric to select filter
villebro ec708cb
use verbose name when defined
villebro 9ef267a
fixes
villebro 51c95a8
lint
villebro df8e23e
Merge branch 'master' into villebro/native-select-sort-metric
villebro f338c5a
disable flaky test
villebro dca110e
disable flaky test
villebro a07f1ca
Merge branch 'master' into villebro/native-select-sort-metric
villebro 46803e2
disable flaky test
villebro 55eb4c7
Merge branch 'master' into villebro/native-select-sort-metric
villebro File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -21,14 +21,20 @@ import { | |||||
t, | ||||||
getChartMetadataRegistry, | ||||||
Behavior, | ||||||
JsonResponse, | ||||||
SupersetApiError, | ||||||
} from '@superset-ui/core'; | ||||||
import { FormInstance } from 'antd/lib/form'; | ||||||
import React, { useCallback } from 'react'; | ||||||
import React, { useCallback, useEffect, useState } from 'react'; | ||||||
import { Metric } from '@superset-ui/chart-controls'; | ||||||
import { Checkbox, Form, Input, Typography } from 'src/common/components'; | ||||||
import { Select } from 'src/components/Select'; | ||||||
import SupersetResourceSelect from 'src/components/SupersetResourceSelect'; | ||||||
import SupersetResourceSelect, { | ||||||
cachedSupersetGet, | ||||||
} from 'src/components/SupersetResourceSelect'; | ||||||
import { addDangerToast } from 'src/messageToasts/actions'; | ||||||
import { ClientErrorObject } from 'src/utils/getClientErrorObject'; | ||||||
import SelectControl from 'src/explore/components/controls/SelectControl'; | ||||||
import { ColumnSelect } from './ColumnSelect'; | ||||||
import { NativeFiltersForm } from '../types'; | ||||||
import { | ||||||
|
@@ -98,6 +104,7 @@ export const FiltersConfigForm: React.FC<FiltersConfigFormProps> = ({ | |||||
form, | ||||||
parentFilters, | ||||||
}) => { | ||||||
const [metrics, setMetrics] = useState<Metric[]>([]); | ||||||
const forceUpdate = useForceUpdate(); | ||||||
const formFilter = (form.getFieldValue('filters') || {})[filterId]; | ||||||
|
||||||
|
@@ -115,16 +122,33 @@ export const FiltersConfigForm: React.FC<FiltersConfigFormProps> = ({ | |||||
const hasColumn = | ||||||
hasDataset && !FILTERS_WITHOUT_COLUMN.includes(formFilter?.filterType); | ||||||
|
||||||
const datasetId = formFilter?.dataset?.value; | ||||||
|
||||||
useEffect(() => { | ||||||
if (datasetId) { | ||||||
cachedSupersetGet({ | ||||||
endpoint: `/api/v1/dataset/${datasetId}`, | ||||||
}) | ||||||
.then((response: JsonResponse) => { | ||||||
setMetrics(response.json?.result?.metrics); | ||||||
}) | ||||||
.catch((response: SupersetApiError) => { | ||||||
addDangerToast(response.message); | ||||||
}); | ||||||
} | ||||||
}, [datasetId]); | ||||||
|
||||||
const hasMetrics = hasColumn && !!metrics.length; | ||||||
|
||||||
const hasFilledDataset = | ||||||
!hasDataset || | ||||||
(formFilter?.dataset?.value && (formFilter?.column || !hasColumn)); | ||||||
!hasDataset || (datasetId && (formFilter?.column || !hasColumn)); | ||||||
|
||||||
useBackendFormUpdate(form, filterId, filterToEdit, hasDataset, hasColumn); | ||||||
|
||||||
const initDatasetId = filterToEdit?.targets[0]?.datasetId; | ||||||
const initColumn = filterToEdit?.targets[0]?.column?.name; | ||||||
const newFormData = getFormData({ | ||||||
datasetId: formFilter?.dataset?.value, | ||||||
datasetId, | ||||||
groupby: hasColumn ? formFilter?.column : undefined, | ||||||
defaultValue: formFilter?.defaultValue, | ||||||
...formFilter, | ||||||
|
@@ -203,7 +227,6 @@ export const FiltersConfigForm: React.FC<FiltersConfigFormProps> = ({ | |||||
onError={onDatasetSelectError} | ||||||
onChange={e => { | ||||||
// We need reset column when dataset changed | ||||||
const datasetId = formFilter?.dataset?.value; | ||||||
if (datasetId && e?.value !== datasetId) { | ||||||
setNativeFilterFieldValues(form, filterId, { | ||||||
column: null, | ||||||
|
@@ -226,7 +249,7 @@ export const FiltersConfigForm: React.FC<FiltersConfigFormProps> = ({ | |||||
<ColumnSelect | ||||||
form={form} | ||||||
filterId={filterId} | ||||||
datasetId={formFilter?.dataset?.value} | ||||||
datasetId={datasetId} | ||||||
onChange={forceUpdate} | ||||||
/> | ||||||
</StyledFormItem> | ||||||
|
@@ -297,6 +320,32 @@ export const FiltersConfigForm: React.FC<FiltersConfigFormProps> = ({ | |||||
form={form} | ||||||
forceUpdate={forceUpdate} | ||||||
/> | ||||||
{hasMetrics && ( | ||||||
<StyledFormItem | ||||||
// don't show the column select unless we have a dataset | ||||||
// style={{ display: datasetId == null ? undefined : 'none' }} | ||||||
name={['filters', filterId, 'sortMetric']} | ||||||
initialValue={filterToEdit?.sortMetric} | ||||||
label={<StyledLabel>{t('Sort Metric')}</StyledLabel>} | ||||||
data-test="field-input" | ||||||
> | ||||||
<SelectControl | ||||||
form={form} | ||||||
filterId={filterId} | ||||||
name="sortMetric" | ||||||
options={metrics.map((metric: Metric) => ({ | ||||||
value: metric.metric_name, | ||||||
label: metric.metric_name, | ||||||
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. one nit:
Suggested change
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. Thanks @zhaoyongjie ; thanks for catching this! |
||||||
}))} | ||||||
onChange={(value: string | null): void => { | ||||||
setNativeFilterFieldValues(form, filterId, { | ||||||
sortMetric: value, | ||||||
}); | ||||||
forceUpdate(); | ||||||
}} | ||||||
/> | ||||||
</StyledFormItem> | ||||||
)} | ||||||
<FilterScope | ||||||
updateFormValues={(values: any) => | ||||||
setNativeFilterFieldValues(form, filterId, values) | ||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
If #14313 is merged first, that won't be necessary - metrics can be accessed in
formFilter.dataset?.metrics
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.
Oh nice, let's get that one in first then 👍