Skip to content

Commit

Permalink
add FilterValue type
Browse files Browse the repository at this point in the history
  • Loading branch information
villebro committed Mar 9, 2021
1 parent 3e70fbf commit 578820f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { NativeFiltersState } from 'src/dashboard/reducers/types';
import { DataMaskStateWithId } from 'src/dataMask/types';
import { Layout } from '../../types';
import { getTreeCheckedItems } from '../nativeFilters/FiltersConfigModal/FiltersConfigForm/FilterScope/utils';
import { FilterValue } from '../nativeFilters/types';

export enum IndicatorStatus {
Unset = 'UNSET',
Expand Down Expand Up @@ -52,7 +53,7 @@ const selectIndicatorValue = (
columnKey: string,
filter: Filter,
datasource: Datasource,
): string[] => {
): FilterValue => {
const values = filter.columns[columnKey];
const arrValues = Array.isArray(values) ? values : [values];

Expand Down Expand Up @@ -132,7 +133,7 @@ const getRejectedColumns = (chart: any): Set<string> =>
export type Indicator = {
column?: string;
name: string;
value: string[];
value: FilterValue;
status: IndicatorStatus;
path: string[];
};
Expand Down Expand Up @@ -185,20 +186,22 @@ export const selectNativeIndicatorsForChart = (
const rejectedColumns = getRejectedColumns(chart);

const getStatus = (
value: string[],
value: FilterValue,
isAffectedByScope: boolean,
column?: string,
): IndicatorStatus => {
// a filter is only considered unset if it's value is null
const hasValue = value !== null;
if (!isAffectedByScope) {
return IndicatorStatus.Unset;
}
if (!column && value.length > 0) {
if (!column && hasValue) {
// Filter without datasource
return IndicatorStatus.Applied;
}
if (column && rejectedColumns.has(column))
return IndicatorStatus.Incompatible;
if (column && appliedColumns.has(column) && value.length > 0) {
if (column && appliedColumns.has(column) && hasValue) {
return IndicatorStatus.Applied;
}
return IndicatorStatus.Unset;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@ export interface Target {
// clarityColumns?: Column[];
}

export type FilterValue = string | number | (string | number)[] | null;

export interface Filter {
cascadeParentIds: string[];
defaultValue: any;
currentValue?: any;
defaultValue: FilterValue;
currentValue?: FilterValue;
isInstant: boolean;
id: string; // randomly generated at filter creation
name: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,21 @@ export default function PluginFilterSelect(props: PluginFilterSelectProps) {

useEffect(() => {
handleChange(currentValue ?? []);
}, [JSON.stringify(currentValue), multiSelect, enableEmptyFilter, inverseSelection]);
}, [
JSON.stringify(currentValue),
multiSelect,
enableEmptyFilter,
inverseSelection,
]);

useEffect(() => {
handleChange(defaultValue ?? []);
}, [JSON.stringify(defaultValue), multiSelect, enableEmptyFilter, inverseSelection]);
}, [
JSON.stringify(defaultValue),
multiSelect,
enableEmptyFilter,
inverseSelection,
]);

const placeholderText =
(data || []).length === 0
Expand Down

0 comments on commit 578820f

Please sign in to comment.