Skip to content

Commit

Permalink
feat(native-filter): Hide native filters (#14784)
Browse files Browse the repository at this point in the history
* fix:fix get permission function

* feat: support showFilters

* fix: fix CR notes

* fix: fix lint issues
  • Loading branch information
simcha90 committed May 31, 2021
1 parent 50c5dcb commit e43112c
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 33 deletions.
3 changes: 2 additions & 1 deletion superset-frontend/src/components/Menu/Menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { Row, Col, Grid } from 'antd';
import Icon from 'src/components/Icon';
import RightMenu from './MenuRight';
import { Languages } from './LanguagePicker';
import { URL_PARAMS } from '../../constants';

interface BrandProps {
path: string;
Expand Down Expand Up @@ -158,7 +159,7 @@ export function Menu({
return () => window.removeEventListener('resize', windowResize);
}, []);

const standalone = getUrlParam('standalone', 'boolean');
const standalone = getUrlParam(URL_PARAMS.standalone);
if (standalone) return <></>;

const renderSubMenu = ({
Expand Down
16 changes: 13 additions & 3 deletions superset-frontend/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,19 @@ export const BOOL_TRUE_DISPLAY = 'True';
export const BOOL_FALSE_DISPLAY = 'False';

export const URL_PARAMS = {
standalone: 'standalone',
preselectFilters: 'preselect_filters',
};
standalone: {
name: 'standalone',
type: 'number',
},
preselectFilters: {
name: 'preselect_filters',
type: 'object',
},
showFilters: {
name: 'show_filters',
type: 'boolean',
},
} as const;

/**
* Faster debounce delay for inputs without expensive operation.
Expand Down
9 changes: 5 additions & 4 deletions superset-frontend/src/dashboard/actions/hydrate.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import {
import { chart } from 'src/chart/chartReducer';
import { initSliceEntities } from 'src/dashboard/reducers/sliceEntities';
import { getInitialState as getInitialNativeFilterState } from 'src/dashboard/reducers/nativeFilters';
import { getParam } from 'src/modules/utils';
import { applyDefaultFormData } from 'src/explore/store';
import { buildActiveFilters } from 'src/dashboard/util/activeDashboardFilters';
import findPermission, {
Expand All @@ -54,6 +53,8 @@ import getFilterConfigsFromFormdata from 'src/dashboard/util/getFilterConfigsFro
import getLocationHash from 'src/dashboard/util/getLocationHash';
import newComponentFactory from 'src/dashboard/util/newComponentFactory';
import { TIME_RANGE } from 'src/visualizations/FilterBox/FilterBox';
import { URL_PARAMS } from 'src/constants';
import { getUrlParam } from 'src/utils/urlUtils';
import { FeatureFlag, isFeatureEnabled } from '../../featureFlags';
import extractUrlParams from '../util/extractUrlParams';

Expand All @@ -77,9 +78,9 @@ export const hydrateDashboard = (dashboardData, chartData, datasourcesData) => (
});
try {
// allow request parameter overwrite dashboard metadata
preselectFilters = JSON.parse(
getParam('preselect_filters') || metadata.default_filters,
);
preselectFilters =
getUrlParam(URL_PARAMS.preselectFilters) ||
JSON.parse(metadata.default_filters);
} catch (e) {
//
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,9 @@ const DashboardBuilder: FC<DashboardBuilderProps> = () => {
isFeatureEnabled(FeatureFlag.DASHBOARD_NATIVE_FILTERS) &&
(canEdit || (!canEdit && filterValues.length !== 0));

const [dashboardFiltersOpen, setDashboardFiltersOpen] = useState(true);
const [dashboardFiltersOpen, setDashboardFiltersOpen] = useState(
getUrlParam(URL_PARAMS.showFilters) ?? true,
);

const toggleDashboardFiltersOpen = (visible?: boolean) => {
setDashboardFiltersOpen(visible ?? !dashboardFiltersOpen);
Expand Down Expand Up @@ -152,7 +154,7 @@ const DashboardBuilder: FC<DashboardBuilderProps> = () => {
: undefined;

const hideDashboardHeader =
getUrlParam(URL_PARAMS.standalone, 'number') ===
getUrlParam(URL_PARAMS.standalone) ===
DashboardStandaloneMode.HIDE_NAV_AND_TITLE;

const barTopOffset =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ class HeaderActionsDropdown extends React.PureComponent {
window.location.pathname,
getActiveFilters(),
window.location.hash,
getUrlParam(URL_PARAMS.standalone, 'number'),
getUrlParam(URL_PARAMS.standalone),
);
window.location.replace(url);
break;
Expand Down
4 changes: 2 additions & 2 deletions superset-frontend/src/dashboard/util/getDashboardUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ export default function getDashboardUrl(
// convert flattened { [id_column]: values } object
// to nested filter object
newSearchParams.set(
URL_PARAMS.preselectFilters,
URL_PARAMS.preselectFilters.name,
JSON.stringify(serializeActiveFilterValues(filters)),
);

if (standalone) {
newSearchParams.set(URL_PARAMS.standalone, standalone.toString());
newSearchParams.set(URL_PARAMS.standalone.name, standalone.toString());
}

const hashSection = hash ? `#${hash}` : '';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export default class EmbedCodeButton extends React.Component {
generateEmbedHTML() {
const srcLink = `${window.location.origin + getURIDirectory()}?r=${
this.state.shortUrlId
}&${URL_PARAMS.standalone}=1&height=${this.state.height}`;
}&${URL_PARAMS.standalone.name}=1&height=${this.state.height}`;
return (
'<iframe\n' +
` width="${this.state.width}"\n` +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ function ExploreViewContainer(props) {
const payload = { ...props.form_data };
const longUrl = getExploreLongUrl(
props.form_data,
props.standalone ? URL_PARAMS.standalone : null,
props.standalone ? URL_PARAMS.standalone.name : null,
false,
);
try {
Expand Down
4 changes: 2 additions & 2 deletions superset-frontend/src/explore/exploreUtils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export function getExploreLongUrl(
search[key] = extraSearch[key];
});
search.form_data = safeStringify(formData);
if (endpointType === URL_PARAMS.standalone) {
if (endpointType === URL_PARAMS.standalone.name) {
search.standalone = DashboardStandaloneMode.HIDE_NAV;
}
const url = uri.directory(directory).search(search).toString();
Expand Down Expand Up @@ -176,7 +176,7 @@ export function getExploreUrl({
if (endpointType === 'csv') {
search.csv = 'true';
}
if (endpointType === URL_PARAMS.standalone) {
if (endpointType === URL_PARAMS.standalone.name) {
search.standalone = '1';
}
if (endpointType === 'query') {
Expand Down
10 changes: 0 additions & 10 deletions superset-frontend/src/modules/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,6 @@ export function getDatasourceParameter(datasourceId, datasourceType) {
return `${datasourceId}__${datasourceType}`;
}

export function getParam(name) {
/* eslint no-useless-escape: 0 */
const formattedName = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
const regex = new RegExp(`[\\?&]${formattedName}=([^&#]*)`);
const results = regex.exec(window.location.search);
return results === null
? ''
: decodeURIComponent(results[1].replace(/\+/g, ' '));
}

export function mainMetric(savedMetrics) {
// Using 'count' as default metric if it exists, otherwise using whatever one shows up first
let metric;
Expand Down
20 changes: 14 additions & 6 deletions superset-frontend/src/utils/urlUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@
*/
import { SupersetClient } from '@superset-ui/core';
import { getClientErrorObject } from './getClientErrorObject';
import { URL_PARAMS } from '../constants';

export type UrlParamType = 'string' | 'number' | 'boolean';
export function getUrlParam(paramName: string, type: 'string'): string;
export function getUrlParam(paramName: string, type: 'number'): number;
export function getUrlParam(paramName: string, type: 'boolean'): boolean;
export function getUrlParam(paramName: string, type: UrlParamType): unknown {
const urlParam = new URLSearchParams(window.location.search).get(paramName);
export type UrlParamType = 'string' | 'number' | 'boolean' | 'object';
export type UrlParam = typeof URL_PARAMS[keyof typeof URL_PARAMS];
export function getUrlParam(param: UrlParam & { type: 'string' }): string;
export function getUrlParam(param: UrlParam & { type: 'number' }): number;
export function getUrlParam(param: UrlParam & { type: 'boolean' }): boolean;
export function getUrlParam(param: UrlParam & { type: 'object' }): object;
export function getUrlParam({ name, type }: UrlParam): unknown {
const urlParam = new URLSearchParams(window.location.search).get(name);
switch (type) {
case 'number':
if (!urlParam) {
Expand All @@ -40,6 +43,11 @@ export function getUrlParam(paramName: string, type: UrlParamType): unknown {
return Number(urlParam);
}
return null;
case 'object':
if (!urlParam) {
return null;
}
return JSON.parse(urlParam);
case 'boolean':
if (!urlParam) {
return null;
Expand Down

0 comments on commit e43112c

Please sign in to comment.