From 07aab4d7d009c496515432b3b98e25a03a50693e Mon Sep 17 00:00:00 2001 From: Jeremy Stucki Date: Mon, 5 Jul 2021 18:10:29 +0200 Subject: [PATCH] fix: Don't set on undefined data filters object Fixes #119 --- app/charts/shared/use-interactive-filters.tsx | 32 +++++++------------ app/components/chart-preview.tsx | 16 +++++++--- 2 files changed, 22 insertions(+), 26 deletions(-) diff --git a/app/charts/shared/use-interactive-filters.tsx b/app/charts/shared/use-interactive-filters.tsx index 911c88538..24b43f1fd 100644 --- a/app/charts/shared/use-interactive-filters.tsx +++ b/app/charts/shared/use-interactive-filters.tsx @@ -27,7 +27,7 @@ type InteractiveFiltersStateAction = } | { type: "SET_DATA_FILTER"; - value: FilterValueSingle; + value: { [x: string]: FilterValueSingle }; } | { type: "UPDATE_DATA_FILTER"; @@ -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) { @@ -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]; @@ -89,10 +81,8 @@ const InteractiveFiltersStateReducer = ( return draft; case "RESET_INTERACTIVE_CATEGORIES": - return { - ...draft, - categories: {}, - }; + draft.categories = {}; + return draft; default: throw new Error(); diff --git a/app/components/chart-preview.tsx b/app/components/chart-preview.tsx index dfd455edc..d72095acb 100644 --- a/app/components/chart-preview.tsx +++ b/app/components/chart-preview.tsx @@ -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 }); }