Skip to content

Commit

Permalink
Merge pull request #807 from visualize-admin/fix/missing-loading-stat…
Browse files Browse the repository at this point in the history
…e-configurator

fix: Crashing of charts when selecting component "too fast"
  • Loading branch information
bprusinowski authored Oct 27, 2022
2 parents de1e77e + 782a331 commit c282772
Show file tree
Hide file tree
Showing 4 changed files with 200 additions and 60 deletions.
31 changes: 28 additions & 3 deletions app/configurator/components/field.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { t } from "@lingui/macro";
import { CircularProgress, Theme } from "@mui/material";
import { makeStyles } from "@mui/styles";
import { extent, timeFormat, TimeLocaleObject, timeParse } from "d3";
import get from "lodash/get";
import { ChangeEvent, ReactNode, useCallback, useMemo, useState } from "react";
Expand Down Expand Up @@ -53,6 +55,15 @@ import { IconName } from "@/icons";
import { useLocale } from "@/locales/use-locale";
import { getPalette } from "@/palettes";

const useStyles = makeStyles<Theme>((theme) => ({
loadingIndicator: {
color: theme.palette.grey[700],
display: "inline-block",
marginTop: theme.spacing(1),
marginLeft: theme.spacing(2),
},
}));

export const ControlTabField = ({
component,
value,
Expand Down Expand Up @@ -625,7 +636,8 @@ export const ChartFieldField = ({
optional?: boolean;
disabled?: boolean;
}) => {
const fieldProps = useChartFieldField({ field });
const classes = useStyles();
const { fetching, ...fieldProps } = useChartFieldField({ field });

const noneLabel = t({
id: "controls.dimension.none",
Expand All @@ -641,8 +653,21 @@ export const ChartFieldField = ({
<Select
key={`select-${field}-dimension`}
id={field}
label={optional ? `${label} (${optionalLabel})` : label}
disabled={disabled}
label={
<>
{optional ? (
<span>
{label} ({optionalLabel})
</span>
) : (
<span>{label}</span>
)}
{fetching ? (
<CircularProgress size={12} className={classes.loadingIndicator} />
) : null}
</>
}
disabled={disabled || fetching}
options={
optional
? [
Expand Down
41 changes: 29 additions & 12 deletions app/configurator/config-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import React, {
InputHTMLAttributes,
useCallback,
useContext,
useEffect,
useMemo,
useRef,
useState,
} from "react";
import { useClient } from "urql";

Expand Down Expand Up @@ -65,16 +68,28 @@ export const useChartFieldField = ({
field,
}: {
field: string;
}): SelectProps => {
}): SelectProps & {
fetching: boolean;
} => {
const unmountedRef = useRef(false);
const [fetching, setFetching] = useState(false);
const [state, dispatch] = useConfiguratorState();
const client = useClient();
const locale = useLocale();

useEffect(() => {
return () => {
unmountedRef.current = true;
};
}, []);

const onChange = useEvent(async (e: SelectChangeEvent<unknown>) => {
if (!state.dataSet) {
return;
}

if (e.target.value !== FIELD_VALUE_NONE) {
setFetching(true);
const dimensionIri = e.target.value as string;
const { data: hierarchyData } = await client
.query<DimensionHierarchyQuery, DimensionHierarchyQueryVariables>(
Expand All @@ -90,19 +105,20 @@ export const useChartFieldField = ({
.toPromise();
const tree = hierarchyData?.dataCubeByIri?.dimensionByIri
?.hierarchy as HierarchyValue[];

// If the dimension has a hierarchy, we select leaves
const leaves = getLeaves(tree);

dispatch({
type: "CHART_FIELD_CHANGED",
value: {
locale,
field,
componentIri: dimensionIri,
selectedValues: leaves,
},
});
if (!unmountedRef.current) {
dispatch({
type: "CHART_FIELD_CHANGED",
value: {
locale,
field,
componentIri: dimensionIri,
selectedValues: leaves,
},
});
setFetching(false);
}
} else {
dispatch({
type: "CHART_FIELD_DELETED",
Expand All @@ -124,6 +140,7 @@ export const useChartFieldField = ({
name: field,
value,
onChange,
fetching,
};
};

Expand Down
92 changes: 80 additions & 12 deletions app/configurator/configurator-state.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
deriveFiltersFromFields,
getFiltersByMappingStatus,
getLocalStorageKey,
handleChartFieldChanged,
handleChartOptionChanged,
initChartStateFromChart,
initChartStateFromCube,
Expand All @@ -40,6 +41,39 @@ jest.mock("@/utils/chart-config/exchange", () => ({
fetchChartConfig: jest.fn(),
}));

jest.mock("@/graphql/client", () => {
return {
client: {
readQuery: () => {
return {
data: {
dataCubeByIri: {
dimensions: [
{
__typename: "GeoShapesDimension",
iri: "newAreaLayerColorIri",
values: [{ value: "orange", label: "orange" }],
},
{
__typename: "GeoCoordinatesDimension",
iri: "symbolLayerIri",
values: [{ value: "x", label: "y" }],
},
],
measures: [
{
__typename: "NumericalMeasure",
iri: "measure",
},
],
},
},
};
},
},
};
});

afterEach(() => {
jest.restoreAllMocks();
});
Expand Down Expand Up @@ -690,20 +724,53 @@ describe("colorMapping", () => {
});
});

describe("handleChartOptionChanged", () => {
jest.mock("@/graphql/client", () => ({
readQuery: () => {
return {
dimensions: [
{
iri: "newAreaLayerColorIri",
values: [{ value: "orange", label: "orange" }],
describe("handleChartFieldChanged", () => {
it("should not reset symbol layer when it's being updated", () => {
const state = {
state: "CONFIGURING_CHART",
dataSet: "mapDataset",
dataSource: {
type: "sparql",
url: "fakeUrl",
},
chartConfig: {
chartType: "map",
fields: {
symbolLayer: {
componentIri: "symbolLayerIri",
color: {
type: "categorical",
componentIri: "symbolLayerColorIri",
palette: "oranges",
colorMapping: {
red: "green",
green: "blue",
blue: "red",
},
},
},
],
};
},
}));
},
filters: {},
},
};

handleChartFieldChanged(
state as unknown as ConfiguratorStateConfiguringChart,
{
type: "CHART_FIELD_CHANGED",
value: {
locale: "en",
field: "symbolLayer",
componentIri: "symbolLayerIri",
},
}
);

expect(state.chartConfig.fields.symbolLayer.color).toBeDefined();
});
});

describe("handleChartOptionChanged", () => {
it("should reset previous color filters", () => {
const state = {
state: "CONFIGURING_CHART",
Expand All @@ -713,6 +780,7 @@ describe("handleChartOptionChanged", () => {
url: "fakeUrl",
},
chartConfig: {
chartType: "map",
fields: {
areaLayer: {
componentIri: "areaLayerIri",
Expand Down
Loading

1 comment on commit c282772

@vercel
Copy link

@vercel vercel bot commented on c282772 Oct 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

visualization-tool – ./

visualization-tool-alpha.vercel.app
visualization-tool-git-main-ixt1.vercel.app
visualization-tool-ixt1.vercel.app

Please sign in to comment.