Skip to content
This repository has been archived by the owner on Dec 10, 2021. It is now read-only.

feat(core): add support for empty results and refresh indicator #1121

Merged
merged 3 commits into from
May 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const defaultProps = {
FallbackComponent: DefaultFallbackComponent,
height: 400 as string | number,
width: '100%' as string | number,
enableNoResults: true,
};

export type FallbackPropsWithDimension = FallbackProps & Partial<Dimension>;
Expand All @@ -30,6 +31,8 @@ export type Props = Omit<SuperChartCoreProps, 'chartProps'> &
disableErrorBoundary?: boolean;
/** debounceTime to check for container resize */
debounceTime?: number;
/** enable "No Results" message if empty result set */
enableNoResults?: boolean;
/** Component to render when there are unexpected errors */
FallbackComponent?: React.ComponentType<FallbackPropsWithDimension>;
/** Event listener for unexpected errors from chart */
Expand Down Expand Up @@ -112,6 +115,7 @@ export default class SuperChart extends React.PureComponent<Props, {}> {
onErrorBoundary,
Wrapper,
queriesData,
enableNoResults,
...rest
} = this.props as PropsWithDefault;

Expand All @@ -125,8 +129,9 @@ export default class SuperChart extends React.PureComponent<Props, {}> {
let chart;
// Render the no results component if the query data is null or empty
const noResultQueries =
!queriesData ||
queriesData.every(({ data }) => !data || (Array.isArray(data) && data.length === 0));
enableNoResults &&
(!queriesData ||
queriesData.every(({ data }) => !data || (Array.isArray(data) && data.length === 0)));
if (noResultQueries) {
chart = <NoResultsComponent id={id} className={className} height={height} width={width} />;
} else {
Expand Down
6 changes: 6 additions & 0 deletions packages/superset-ui-core/src/chart/models/ChartMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface ChartMetadataConfig {
credits?: string[];
description?: string;
datasourceCount?: number;
enableNoResults?: boolean;
show?: boolean;
supportedAnnotationTypes?: string[];
thumbnail: string;
Expand Down Expand Up @@ -40,6 +41,8 @@ export default class ChartMetadata {

datasourceCount: number;

enableNoResults: boolean;

constructor(config: ChartMetadataConfig) {
const {
name,
Expand All @@ -52,6 +55,7 @@ export default class ChartMetadata {
useLegacyApi = false,
behaviors = [],
datasourceCount = 1,
enableNoResults = true,
} = config;

this.name = name;
Expand All @@ -73,6 +77,7 @@ export default class ChartMetadata {
this.useLegacyApi = useLegacyApi;
this.behaviors = behaviors;
this.datasourceCount = datasourceCount;
this.enableNoResults = enableNoResults;
}

canBeAnnotationType(type: string): boolean {
Expand All @@ -91,6 +96,7 @@ export default class ChartMetadata {
useLegacyApi: this.useLegacyApi,
behaviors: this.behaviors,
datasourceCount: this.datasourceCount,
enableNoResults: this.enableNoResults,
});
}
}
12 changes: 12 additions & 0 deletions packages/superset-ui-core/src/chart/models/ChartProps.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/** Type checking is disabled for this file due to reselect only supporting
* TS declarations for selectors with up to 12 arguments. */
// @ts-nocheck
import { createSelector } from 'reselect';
import {
AppSection,
Expand Down Expand Up @@ -66,6 +69,8 @@ export interface ChartPropsConfig {
behaviors?: Behavior[];
/** Application section of the chart on the screen (in what components/screen it placed) */
appSection?: AppSection;
/** is the chart refreshing its contents */
isRefreshing?: boolean;
}

const DEFAULT_WIDTH = 800;
Expand Down Expand Up @@ -102,6 +107,8 @@ export default class ChartProps<FormData extends RawFormData = RawFormData> {

appSection?: AppSection;

isRefreshing?: boolean;

constructor(config: ChartPropsConfig & { formData?: FormData } = {}) {
const {
annotationData = {},
Expand All @@ -116,6 +123,7 @@ export default class ChartProps<FormData extends RawFormData = RawFormData> {
width = DEFAULT_WIDTH,
height = DEFAULT_HEIGHT,
appSection,
isRefreshing,
} = config;
this.width = width;
this.height = height;
Expand All @@ -131,6 +139,7 @@ export default class ChartProps<FormData extends RawFormData = RawFormData> {
this.filterState = filterState;
this.behaviors = behaviors;
this.appSection = appSection;
this.isRefreshing = isRefreshing;
}
}

Expand All @@ -149,6 +158,7 @@ ChartProps.createSelector = function create(): ChartPropsSelector {
input => input.filterState,
input => input.behaviors,
input => input.appSection,
input => input.isRefreshing,
(
annotationData,
datasource,
Expand All @@ -162,6 +172,7 @@ ChartProps.createSelector = function create(): ChartPropsSelector {
filterState,
behaviors,
appSection,
isRefreshing,
) =>
new ChartProps({
annotationData,
Expand All @@ -176,6 +187,7 @@ ChartProps.createSelector = function create(): ChartPropsSelector {
width,
behaviors,
appSection,
isRefreshing,
}),
);
};
33 changes: 32 additions & 1 deletion packages/superset-ui-core/test/chart/models/ChartProps.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ChartProps } from '@superset-ui/core/src';
import { Behavior, ChartProps } from '@superset-ui/core/src';

const RAW_FORM_DATA = {
some_field: 1,
Expand All @@ -10,6 +10,7 @@ const RAW_DATASOURCE = {

const QUERY_DATA = { data: {} };
const QUERIES_DATA = [QUERY_DATA];
const BEHAVIORS = [Behavior.NATIVE_FILTER, Behavior.INTERACTIVE_CHART];

describe('ChartProps', () => {
it('exists', () => {
Expand Down Expand Up @@ -51,16 +52,46 @@ describe('ChartProps', () => {
datasource: RAW_DATASOURCE,
formData: RAW_FORM_DATA,
queriesData: QUERIES_DATA,
behaviors: BEHAVIORS,
isRefreshing: false,
});
const props2 = selector({
width: 800,
height: 600,
datasource: RAW_DATASOURCE,
formData: RAW_FORM_DATA,
queriesData: QUERIES_DATA,
behaviors: BEHAVIORS,
isRefreshing: false,
});
expect(props1).toBe(props2);
});
it('selector returns a new chartProps if the 13th field changes', () => {
/** this test is here to test for selectors that exceed 12 arguments (
* isRefreshing is the 13th argument, which is missing TS declarations).
* See: https://github.com/reduxjs/reselect/issues/378
*/

const props1 = selector({
width: 800,
height: 600,
datasource: RAW_DATASOURCE,
formData: RAW_FORM_DATA,
queriesData: QUERIES_DATA,
behaviors: BEHAVIORS,
isRefreshing: false,
});
const props2 = selector({
width: 800,
height: 600,
datasource: RAW_DATASOURCE,
formData: RAW_FORM_DATA,
queriesData: QUERIES_DATA,
behaviors: BEHAVIORS,
isRefreshing: true,
});
expect(props1).not.toBe(props2);
});
it('selector returns a new chartProps if some input fields change', () => {
const props1 = selector({
width: 800,
Expand Down