Skip to content

Commit

Permalink
create useVerboseMap in hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
Lily Kuang committed Apr 25, 2023
1 parent 8e9d265 commit 0afc61f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import {
Behavior,
Column,
ContextMenuFilters,
Metric,
css,
ensureIsArray,
getChartMetadataRegistry,
Expand All @@ -45,6 +44,7 @@ import {
cachedSupersetGet,
supersetGetCache,
} from 'src/utils/cachedSupersetGet';
import { useVerboseMap } from 'src/hooks/apiResources/datasets';
import { MenuItemTooltip } from '../DisabledMenuItemTooltip';
import DrillByModal from './DrillByModal';
import { getSubmenuYOffset } from '../utils';
Expand Down Expand Up @@ -116,6 +116,7 @@ export const DrillByMenuItems = ({
?.behaviors.find(behavior => behavior === Behavior.DRILL_BY),
[formData.viz_type],
);
const verboseMap = useVerboseMap(dataset);

useEffect(() => {
if (handlesDimensionContextMenu && hasDrillBy) {
Expand All @@ -124,16 +125,7 @@ export const DrillByMenuItems = ({
endpoint: `/api/v1/dataset/${datasetId}`,
})
.then(({ json: { result } }) => {
const verbose_map = {};
ensureIsArray(result.columns).forEach((column: Column) => {
verbose_map[column.column_name] =
column.verbose_name || column.column_name;
});
ensureIsArray(result.metrics).forEach((metric: Metric) => {
verbose_map[metric.metric_name] =
metric.verbose_name || metric.metric_name;
});
setDataset({ ...result, verbose_map });
setDataset(result);
setColumns(
ensureIsArray(result.columns)
.filter(column => column.groupby)
Expand Down Expand Up @@ -280,7 +272,7 @@ export const DrillByMenuItems = ({
drillByConfig={drillByConfig}
formData={formData}
onHideModal={closeModal}
dataset={dataset!}
dataset={{ ...dataset!, verbose_map: verboseMap }}
/>
)}
</>
Expand Down
4 changes: 3 additions & 1 deletion superset-frontend/src/components/Chart/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
import { Column } from '@superset-ui/core';
import { Column, Metric } from '@superset-ui/core';

export enum DrillByType {
Chart,
Expand All @@ -41,4 +41,6 @@ export type Dataset = {
last_name: string;
}[];
columns?: Column[];
metrics?: Metric[];
verbose_map?: Record<string, string>;
};
32 changes: 32 additions & 0 deletions superset-frontend/src/hooks/apiResources/datasets.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/* eslint-disable no-underscore-dangle */
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import { Column, Metric, ensureIsArray } from '@superset-ui/core';
import { Dataset } from 'src/components/Chart/types';

export const useVerboseMap = (dataset?: Dataset) => {
const verbose_map = {};
ensureIsArray(dataset?.columns).forEach((column: Column) => {
verbose_map[column.column_name] = column.verbose_name || column.column_name;
});
ensureIsArray(dataset?.metrics).forEach((metric: Metric) => {
verbose_map[metric.metric_name] = metric.verbose_name || metric.metric_name;
});
return verbose_map;
};

0 comments on commit 0afc61f

Please sign in to comment.