Skip to content

Commit

Permalink
fix: Properly handle joinByDimension when setting range filters
Browse files Browse the repository at this point in the history
  • Loading branch information
bprusinowski committed Dec 1, 2023
1 parent a3a9cb6 commit 75b7712
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 24 deletions.
5 changes: 2 additions & 3 deletions app/configurator/components/filters.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -903,14 +903,13 @@ export const TimeFilter = (props: TimeFilterProps) => {
dispatch({
type: "CHART_CONFIG_FILTER_SET_RANGE",
value: {
cubeIri: dimension.cubeIri,
dimensionIri: dimension.iri,
dimension,
from,
to,
},
});
},
[dispatch, dimension.cubeIri, dimension.iri]
[dispatch, dimension]
);

const temporalDimension =
Expand Down
51 changes: 30 additions & 21 deletions app/configurator/configurator-state.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,7 @@ export type ConfiguratorStateAction =
| {
type: "CHART_CONFIG_FILTER_SET_RANGE";
value: {
cubeIri: string;
dimensionIri: string;
dimension: Dimension;
from: string;
to: string;
};
Expand Down Expand Up @@ -1197,28 +1196,38 @@ const reducer: Reducer<ConfiguratorState, ConfiguratorStateAction> = (

case "CHART_CONFIG_FILTER_SET_RANGE":
if (draft.state === "CONFIGURING_CHART") {
const { cubeIri, dimensionIri, from, to } = action.value;
const { dimension, from, to } = action.value;
const chartConfig = getChartConfig(draft);
const cube = chartConfig.cubes.find((cube) => cube.iri === cubeIri);

if (cube) {
cube.filters[dimensionIri] = {
type: "range",
from,
to,
};

if (chartConfig.interactiveFiltersConfig) {
chartConfig.interactiveFiltersConfig.timeRange = {
componentIri: dimensionIri,
active: chartConfig.interactiveFiltersConfig.timeRange.active,
presets: {
type: "range",
from,
to,
},
const adjustFilter = (cubeIri: string, dimensionIri: string) => {
const cube = chartConfig.cubes.find((cube) => cube.iri === cubeIri);

if (cube) {
cube.filters[dimensionIri] = {
type: "range",
from,
to,
};
}
};

if (dimension.isJoinByDimension) {
for (const { cubeIri, dimensionIri } of dimension.originalIris) {
adjustFilter(cubeIri, dimensionIri);
}
} else {
adjustFilter(dimension.cubeIri, dimension.iri);
}

if (chartConfig.interactiveFiltersConfig) {
chartConfig.interactiveFiltersConfig.timeRange = {
componentIri: dimension.iri,
active: chartConfig.interactiveFiltersConfig.timeRange.active,
presets: {
type: "range",
from,
to,
},
};
}
}

Expand Down
41 changes: 41 additions & 0 deletions app/graphql/hook-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { ascending } from "d3";
import uniqBy from "lodash/uniqBy";
import { OperationResult } from "urql";

import { Filters } from "@/configurator";
import { FIELD_VALUE_NONE } from "@/configurator/constants";
import { Dimension, Observation, ObservationValue } from "@/domain/data";
import {
DataCubeComponentsQuery,
Expand All @@ -11,6 +13,45 @@ import {
Exact,
} from "@/graphql/query-hooks";

/** Use in both components and observations query, to omit literal `joinBy` filter,
* and use the value of the `joinBy` property of the cube filter instead.
*
* This is required to be able to properly filter the LINDAS cubes, as we use
* `joinBy` dimension on the client side.
*/
export const getJoinedCubeFilters = <
T extends { filters?: Filters | null; joinBy?: string | null }
>(
cubeFilters: T[]
) => {
const joinedCubeFilters: T[] = [];

for (const cubeFilter of cubeFilters) {
if (cubeFilter.filters && cubeFilter.joinBy) {
const { joinBy: joinByFilter, ...filters } = cubeFilter.filters;

if (
joinByFilter &&
joinByFilter.type === "single" &&
joinByFilter.value !== FIELD_VALUE_NONE
) {
joinedCubeFilters.push({
...cubeFilter,
filters: {
[cubeFilter.joinBy]: { type: "single", value: joinByFilter.value },
...filters,
},
});

continue;
}
}
joinedCubeFilters.push(cubeFilter);
}

return joinedCubeFilters;
};

/** Use to exclude joinBy dimensions when fetching dimensions, and create
* a new joinBy dimension with values from all joinBy dimensions.
*/
Expand Down

0 comments on commit 75b7712

Please sign in to comment.