Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retain filters when switching to map #741

Merged
merged 4 commits into from
Sep 27, 2022
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ You can also check the [release page](https://github.com/visualize-admin/visuali

Nothing yet.

## [3.9.3] - 2022-09-27

- Map: Retain filters when switching from another chart type to map

## [3.9.2] - 2022-09-20

- Map: fix a layer offset problem on Windows Edge.
Expand Down
17 changes: 14 additions & 3 deletions app/charts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ascending, group } from "d3";
import produce from "immer";
import get from "lodash/get";
import groupBy from "lodash/groupBy";
import sortBy from "lodash/sortBy";

import { DEFAULT_SYMBOL_LAYER_COLORS } from "@/charts/map/constants";
import {
Expand Down Expand Up @@ -34,6 +35,8 @@ import {
getCategoricalDimensions,
getGeoDimensions,
getTimeDimensions,
isGeoCoordinatesDimension,
isGeoShapesDimension,
} from "../domain/data";
import { DimensionMetadataFragment } from "../graphql/query-hooks";
import { DataCubeMetadata } from "../graphql/types";
Expand Down Expand Up @@ -166,7 +169,9 @@ export const getInitialConfig = ({
fields: {
x: {
componentIri: findPreferredDimension(
dimensions,
sortBy(dimensions, (x) =>
isGeoCoordinatesDimension(x) || isGeoShapesDimension(x) ? 1 : -1
),
"TemporalDimension"
).iri,
sorting: DEFAULT_SORTING,
Expand Down Expand Up @@ -798,8 +803,14 @@ const chartConfigsAdjusters: ChartConfigsAdjusters = {
map: {
filters: ({ oldValue, newChartConfig }) => {
return produce(newChartConfig, (draft) => {
if (!oldValue) {
draft.filters = oldValue;
// Filters have been reset by the initial config of the map.
// We need to set them back to their old value, taking care not
// to override the filters that have been set by the initial config
// of the map.
for (const [iri, value] of Object.entries(oldValue)) {
if (draft.filters[iri] === undefined) {
draft.filters[iri] = value;
}
}
});
},
Expand Down
11 changes: 11 additions & 0 deletions app/configurator/configurator-state.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,17 @@ describe("retainChartConfigWhenSwitchingChartType", () => {
newFieldGetterPath: "fields.areaLayer.componentIri",
equal: false,
},
{
oldFieldGetterPath: [
"filters",
"https://environment.ld.admin.ch/foen/COVID19VaccPersons_v2/type",
],
newFieldGetterPath: [
"filters",
"https://environment.ld.admin.ch/foen/COVID19VaccPersons_v2/type",
],
equal: true,
},
],
},
},
Expand Down
8 changes: 5 additions & 3 deletions app/configurator/configurator-state.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import get from "lodash/get";
import mapValues from "lodash/mapValues";
import pickBy from "lodash/pickBy";
import setWith from "lodash/setWith";
import sortBy from "lodash/sortBy";
import { useRouter } from "next/router";
import {
createContext,
Expand Down Expand Up @@ -352,8 +353,8 @@ export const deriveFiltersFromFields = produce(
const isField = (iri: string) => fieldDimensionIris.has(iri);

// Apply hierarchical dimensions first
const sortedDimensions = [...dimensions].sort(
(a, b) => (a.hierarchy ? -1 : 1) - (b.hierarchy ? -1 : 1)
const sortedDimensions = sortBy(dimensions, (d) =>
d.hierarchy ? -1 : 1
);
sortedDimensions.forEach((dimension) =>
applyNonTableDimensionToFilters({
Expand Down Expand Up @@ -854,8 +855,9 @@ const reducer: Reducer<ConfiguratorState, ConfiguratorStateAction> = (
// setWith(draft, action.value.path, action.value.value, Object);
const { chartType, dataSetMetadata } = action.value;

const previousConfig = current(draft.chartConfig);
draft.chartConfig = getChartConfigAdjustedToChartType({
chartConfig: current(draft.chartConfig),
chartConfig: previousConfig,
newChartType: chartType,
dimensions: dataSetMetadata.dimensions,
measures: dataSetMetadata.measures,
Expand Down
10 changes: 1 addition & 9 deletions cypress/integration/filters.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,7 @@ describe("Filters", () => {
cy.findByText("1. production region");
cy.findByText("2. stand structure");
cy.findByText("3. evaluation type");
cy.findByText("4. Inventory");
cy.findByText("5. unit of evaluation");
cy.findByText("4. unit of evaluation");
});

selectors.edition
.findChartFiltersList(cy)
.should(
"contain.text",
"production region: Switzerland, stand structure: total, evaluation type: Zustand, Inventory: NFI1, unit of evaluation: accessible forest without shrub forest NFI1/NFI2/NFI3/NFI4"
);
});
});