Skip to content

Commit

Permalink
fix: Don't set on undefined data filters object
Browse files Browse the repository at this point in the history
Fixes #119
  • Loading branch information
jstcki committed Jul 5, 2021
1 parent 1579c52 commit 07aab4d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 26 deletions.
32 changes: 11 additions & 21 deletions app/charts/shared/use-interactive-filters.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type InteractiveFiltersStateAction =
}
| {
type: "SET_DATA_FILTER";
value: FilterValueSingle;
value: { [x: string]: FilterValueSingle };
}
| {
type: "UPDATE_DATA_FILTER";
Expand All @@ -50,10 +50,8 @@ const InteractiveFiltersStateReducer = (
) => {
switch (action.type) {
case "ADD_INTERACTIVE_FILTER":
return {
...draft,
categories: { ...draft.categories, [action.value]: true },
};
draft.categories = { ...draft.categories, [action.value]: true };
return draft;
case "REMOVE_INTERACTIVE_FILTER":
const { categories } = draft;
if (categories) {
Expand All @@ -62,20 +60,14 @@ const InteractiveFiltersStateReducer = (
}
return draft;
case "ADD_TIME_FILTER":
return {
...draft,
time: { from: action.value[0], to: action.value[1] },
};
draft.time = { from: action.value[0], to: action.value[1] };
return draft;
case "RESET_DATA_FILTER":
return {
...draft,
dataFilters: undefined,
};
draft.dataFilters = {};
return draft;
case "SET_DATA_FILTER":
return {
...draft,
dataFilters: action.value,
};
draft.dataFilters = action.value;
return draft;
case "UPDATE_DATA_FILTER":
if (action.value.dimensionValueIri === FIELD_VALUE_NONE) {
delete draft.dataFilters[action.value.dimensionIri];
Expand All @@ -89,10 +81,8 @@ const InteractiveFiltersStateReducer = (
return draft;

case "RESET_INTERACTIVE_CATEGORIES":
return {
...draft,
categories: {},
};
draft.categories = {};
return draft;

default:
throw new Error();
Expand Down
16 changes: 11 additions & 5 deletions app/components/chart-preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,19 @@ const ChartWithInteractiveFilters = ({
if (componentIris) {
// If dimension is already in use as interactive filter, use it,
// otherwise, default to editor config filter dimension value.
const newInteractiveDataFilters = componentIris.reduce((obj, iri) => {
const newInteractiveDataFilters = componentIris.reduce<{
[key: string]: FilterValueSingle;
}>((obj, iri) => {
const configFilter = chartConfig.filters[iri];

if (Object.keys(IFstate.dataFilters).includes(iri)) {
return { ...obj, [iri]: IFstate.dataFilters[iri] };
} else {
return { ...obj, [iri]: chartConfig.filters[iri] };
obj[iri] = IFstate.dataFilters[iri];
} else if (configFilter?.type === "single") {
obj[iri] = configFilter;
}
}, {} as FilterValueSingle);

return obj;
}, {});

dispatch({ type: "SET_DATA_FILTER", value: newInteractiveDataFilters });
}
Expand Down

0 comments on commit 07aab4d

Please sign in to comment.