Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/apache/superset into pexd…
Browse files Browse the repository at this point in the history
…ax/snowflake
  • Loading branch information
hughhhh committed Sep 29, 2021
2 parents b3a23b3 + 331de0c commit 9fa95ba
Show file tree
Hide file tree
Showing 12 changed files with 144 additions and 142 deletions.
35 changes: 30 additions & 5 deletions superset-frontend/src/SqlLab/components/ResultSet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,16 @@ import { RadioChangeEvent } from 'antd/lib/radio';
import Button from 'src/components/Button';
import shortid from 'shortid';
import rison from 'rison';
import { styled, t, makeApi } from '@superset-ui/core';
import {
styled,
t,
makeApi,
SupersetClient,
JsonResponse,
} from '@superset-ui/core';
import { debounce } from 'lodash';
import ErrorMessageWithStackTrace from 'src/components/ErrorMessage/ErrorMessageWithStackTrace';
import { SaveDatasetModal } from 'src/SqlLab/components/SaveDatasetModal';
import { put as updateDatset } from 'src/api/dataset';
import { UserWithPermissionsAndRoles } from 'src/types/bootstrapTypes';
import Loading from '../../components/Loading';
import ExploreCtasResultsButton from './ExploreCtasResultsButton';
Expand Down Expand Up @@ -133,6 +138,27 @@ const ResultSetErrorMessage = styled.div`
padding-top: ${({ theme }) => 4 * theme.gridUnit}px;
`;

const updateDataset = async (
datasetId: number,
sql: string,
columns: Array<Record<string, any>>,
overrideColumns: boolean,
) => {
const endpoint = `api/v1/dataset/${datasetId}?override_columns=${overrideColumns}`;
const headers = { 'Content-Type': 'application/json' };
const body = JSON.stringify({
sql,
columns,
});

const data: JsonResponse = await SupersetClient.put({
endpoint,
headers,
body,
});
return data.json.result;
};

export default class ResultSet extends React.PureComponent<
ResultSetProps,
ResultSetState
Expand Down Expand Up @@ -236,12 +262,11 @@ export default class ResultSet extends React.PureComponent<
};

handleOverwriteDataset = async () => {
const { sql, results, dbId } = this.props.query;
const { sql, results } = this.props.query;
const { datasetToOverwrite } = this.state;

await updateDatset(
await updateDataset(
datasetToOverwrite.datasetId,
dbId,
sql,
results.selected_columns.map(d => ({ column_name: d.name })),
true,
Expand Down
63 changes: 0 additions & 63 deletions superset-frontend/src/api/dataset.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ describe('FilterScope', () => {
restoreFilter: jest.fn(),
parentFilters: [],
save,
removedFilters: {},
};

const MockModal = ({ scope }: { scope?: object }) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import React, {
useState,
} from 'react';
import { useSelector } from 'react-redux';
import { isEqual } from 'lodash';
import { isEqual, isEmpty } from 'lodash';
import { FormItem } from 'src/components/Form';
import { Input } from 'src/common/components';
import { Select } from 'src/components';
Expand All @@ -70,7 +70,7 @@ import {
} from 'src/dashboard/types';
import Loading from 'src/components/Loading';
import { ColumnSelect } from './ColumnSelect';
import { NativeFiltersForm } from '../types';
import { NativeFiltersForm, FilterRemoval } from '../types';
import {
FILTER_SUPPORTED_TYPES,
hasTemporalColumns,
Expand Down Expand Up @@ -264,7 +264,7 @@ const FilterPanels = {
export interface FiltersConfigFormProps {
filterId: string;
filterToEdit?: Filter;
removed?: boolean;
removedFilters: Record<string, FilterRemoval>;
restoreFilter: (filterId: string) => void;
form: FormInstance<NativeFiltersForm>;
parentFilters: { id: string; title: string }[];
Expand Down Expand Up @@ -300,21 +300,22 @@ const FiltersConfigForm = (
{
filterId,
filterToEdit,
removed,
removedFilters,
restoreFilter,
form,
parentFilters,
}: FiltersConfigFormProps,
ref: React.RefObject<any>,
) => {
const isRemoved = !!removedFilters[filterId];
const [error, setError] = useState<string>('');
const [metrics, setMetrics] = useState<Metric[]>([]);
const [activeTabKey, setActiveTabKey] = useState<string>(
FilterTabs.configuration.key,
);
const [activeFilterPanelKey, setActiveFilterPanelKey] = useState<
string | string[]
>(FilterPanels.basic.key);
string | string[] | undefined
>();
const [undoFormValues, setUndoFormValues] = useState<Record<
string,
any
Expand All @@ -325,6 +326,24 @@ const FiltersConfigForm = (
const formValues = form.getFieldValue('filters')?.[filterId];
const formFilter = formValues || undoFormValues || defaultFormFilter;

const parentFilterOptions = useMemo(
() =>
parentFilters.map(filter => ({
value: filter.id,
label: filter.title,
})),
[parentFilters],
);

const parentId =
formFilter?.parentFilter?.value || filterToEdit?.cascadeParentIds?.[0];

const parentFilter = parentFilterOptions.find(
({ value }) => value === parentId,
);

const hasParentFilter = !!parentFilter;

const nativeFilterItems = getChartMetadataRegistry().items;
const nativeFilterVizTypes = Object.entries(nativeFilterItems)
// @ts-ignore
Expand Down Expand Up @@ -374,7 +393,7 @@ const FiltersConfigForm = (
filterType: formFilter?.filterType,
filterToEdit,
formFilter,
removed,
removed: isRemoved,
})
: {};
const hasColumn = !!mainControlItems.groupby;
Expand Down Expand Up @@ -506,19 +525,6 @@ const FiltersConfigForm = (
[filterId, form, formChanged],
);

const parentFilterOptions = parentFilters.map(filter => ({
value: filter.id,
label: filter.title,
}));

const parentFilter = parentFilterOptions.find(
({ value }) =>
value === formFilter?.parentFilter?.value ||
value === filterToEdit?.cascadeParentIds?.[0],
);

const hasParentFilter = !!parentFilter;

const hasPreFilter =
!!formFilter?.adhoc_filters ||
!!formFilter?.time_range ||
Expand Down Expand Up @@ -583,28 +589,32 @@ const FiltersConfigForm = (
return Promise.reject(new Error(t('Pre-filter is required')));
};

let hasCheckedAdvancedControl = hasParentFilter || hasPreFilter || hasSorting;
if (!hasCheckedAdvancedControl) {
hasCheckedAdvancedControl = Object.keys(controlItems)
.filter(key => !BASIC_CONTROL_ITEMS.includes(key))
.some(key => controlItems[key].checked);
}

const ParentSelect = ({
value,
...rest
}: {
value?: { value: string | number };
}) => (
<Select
ariaLabel={t('Parent filter')}
placeholder={t('None')}
options={parentFilterOptions}
allowClear
value={value?.value}
{...rest}
/>
);
}) => {
const parentId = value?.value;
const isParentRemoved = parentId && removedFilters[parentId];
let options = parentFilterOptions;
if (isParentRemoved) {
options = [
{ label: t('(deleted)'), value: parentId as string },
...parentFilterOptions,
];
}
return (
<Select
ariaLabel={t('Parent filter')}
placeholder={t('None')}
options={options}
allowClear
value={parentId}
{...rest}
/>
);
};

useEffect(() => {
if (datasetId) {
Expand Down Expand Up @@ -647,12 +657,28 @@ const FiltersConfigForm = (
]);

useEffect(() => {
const activeFilterPanelKey = [FilterPanels.basic.key];
if (hasCheckedAdvancedControl) {
activeFilterPanelKey.push(FilterPanels.advanced.key);
// Run only once when the control items are available
if (!activeFilterPanelKey && !isEmpty(controlItems)) {
const hasCheckedAdvancedControl =
hasParentFilter ||
hasPreFilter ||
hasSorting ||
Object.keys(controlItems)
.filter(key => !BASIC_CONTROL_ITEMS.includes(key))
.some(key => controlItems[key].checked);
setActiveFilterPanelKey(
hasCheckedAdvancedControl
? [FilterPanels.basic.key, FilterPanels.advanced.key]
: FilterPanels.basic.key,
);
}
setActiveFilterPanelKey(activeFilterPanelKey);
}, [hasCheckedAdvancedControl]);
}, [
activeFilterPanelKey,
hasParentFilter,
hasPreFilter,
hasSorting,
controlItems,
]);

const initiallyExcludedCharts = useMemo(() => {
const excluded: number[] = [];
Expand All @@ -678,20 +704,20 @@ const FiltersConfigForm = (

useEffect(() => {
// just removed, saving current form items for eventual undo
if (removed) {
if (isRemoved) {
setUndoFormValues(formValues);
}
}, [removed]);
}, [isRemoved]);

useEffect(() => {
// the filter was just restored after undo
if (undoFormValues && !removed) {
if (undoFormValues && !isRemoved) {
setNativeFilterFieldValues(form, filterId, undoFormValues);
setUndoFormValues(null);
}
}, [formValues, filterId, form, removed, undoFormValues]);
}, [formValues, filterId, form, isRemoved, undoFormValues]);

if (removed) {
if (isRemoved) {
return <RemovedFilter onClick={() => restoreFilter(filterId)} />;
}

Expand All @@ -718,13 +744,13 @@ const FiltersConfigForm = (
name={['filters', filterId, 'name']}
label={<StyledLabel>{t('Filter name')}</StyledLabel>}
initialValue={filterToEdit?.name}
rules={[{ required: !removed, message: t('Name is required') }]}
rules={[{ required: !isRemoved, message: t('Name is required') }]}
>
<Input {...getFiltersConfigModalTestId('name-input')} />
</StyledFormItem>
<StyledFormItem
name={['filters', filterId, 'filterType']}
rules={[{ required: !removed, message: t('Name is required') }]}
rules={[{ required: !isRemoved, message: t('Name is required') }]}
initialValue={filterToEdit?.filterType || 'filter_select'}
label={<StyledLabel>{t('Filter Type')}</StyledLabel>}
{...getFiltersConfigModalTestId('filter-type')}
Expand Down Expand Up @@ -782,7 +808,7 @@ const FiltersConfigForm = (
: undefined
}
rules={[
{ required: !removed, message: t('Dataset is required') },
{ required: !isRemoved, message: t('Dataset is required') },
]}
{...getFiltersConfigModalTestId('datasource-input')}
>
Expand Down Expand Up @@ -837,7 +863,7 @@ const FiltersConfigForm = (
formChanged();
}}
>
{!removed && (
{!isRemoved && (
<StyledRowSubFormItem
name={['filters', filterId, 'defaultDataMask']}
initialValue={initialDefaultValue}
Expand Down
Loading

0 comments on commit 9fa95ba

Please sign in to comment.