Skip to content

Commit

Permalink
EES-5542 moved geoJson fetching on dataset selection into parent comp…
Browse files Browse the repository at this point in the history
…onents with callbacks
  • Loading branch information
bennettstuart committed Dec 12, 2024
1 parent 3043ec4 commit 62dc8da
Show file tree
Hide file tree
Showing 14 changed files with 156 additions and 213 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,6 @@ const DataBlockPageReadOnlyTabs = ({ releaseId, dataBlock }: Props) => {
return (
<ChartRenderer
{...chart}
releaseId={releaseId}
dataBlockParentId={dataBlock.dataBlockParentId}
key={key}
id={`dataBlockTabs-chart-${index}`}
axes={axes}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ interface Props {
export default function ChartBoundaryLevelsConfiguration({
buttons,
// TODO: EES-5402 - Remove when all boundary level changes are done
hasDataSetBoundaryLevels = false,
hasDataSetBoundaryLevels = true,
map,
meta,
options,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ interface Props {
data: TableDataResult[];
meta: FullTableMeta;
releaseId: string;
dataBlockParentId: DataBlock['dataBlockParentId'];
initialChart?: Chart;
tableTitle: string;
onChartSave: (chart: Chart, file?: File) => Promise<void>;
Expand All @@ -112,7 +111,6 @@ export default function ChartBuilder({
data,
meta,
releaseId,
dataBlockParentId,
initialChart,
tableTitle,
onChartSave,
Expand Down Expand Up @@ -200,8 +198,8 @@ export default function ChartBuilder({
},
boundaryLevel: options.boundaryLevel ?? 0,
type: 'map',
releaseId,
dataBlockParentId,
onBoundaryLevelChange: boundaryLevel =>
onTableQueryUpdate({ boundaryLevel }),
};
default:
return undefined;
Expand All @@ -215,8 +213,7 @@ export default function ChartBuilder({
meta,
options,
map,
releaseId,
dataBlockParentId,
onTableQueryUpdate,
]);

const handleSubmit = useCallback(async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ import React, { memo, useMemo } from 'react';

export type ChartRendererProps = {
source?: string;
releaseId?: string;
dataBlockParentId?: string;
} & (
| ({
type: 'map';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
}
}

.mapLoading {
opacity: 0.5;
}

.tooltip {
white-space: normal;
width: 100%;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ import { LegendConfiguration } from '@common/modules/charts/types/legend';
import getMapDataSetCategoryConfigs, {
MapDataSetCategoryConfig,
} from '@common/modules/charts/util/getMapDataSetCategoryConfigs';
import tableBuilderService, {
GeoJsonFeatureProperties,
} from '@common/services/tableBuilderService';
import { GeoJsonFeatureProperties } from '@common/services/tableBuilderService';
import { Dictionary } from '@common/types';
import naturalOrderBy from '@common/utils/array/naturalOrderBy';
import classNames from 'classnames';
Expand All @@ -32,9 +30,8 @@ import { Layer, Path, Polyline } from 'leaflet';
import keyBy from 'lodash/keyBy';
import React, { useCallback, useEffect, useMemo, useState } from 'react';
import { MapContainer } from 'react-leaflet';
import { useQuery, useQueryClient } from '@tanstack/react-query';
import { LocationFilter } from '@common/modules/table-tool/types/filters';
import { mapFullTableMetaLocations } from '@common/modules/table-tool/utils/mapFullTableMeta';
import useToggle from '@common/hooks/useToggle';
import LoadingSpinner from '@common/components/LoadingSpinner';

export interface MapFeatureProperties extends GeoJsonFeatureProperties {
colour: string;
Expand Down Expand Up @@ -64,8 +61,7 @@ export interface MapBlockProps extends ChartProps {
map?: MapConfig;
position?: { lat: number; lng: number };
boundaryLevel: number;
releaseId: string;
dataBlockParentId: string;
onBoundaryLevelChange: (boundaryLevel: number) => void;
}

export const mapBlockDefinition: ChartDefinition = {
Expand Down Expand Up @@ -130,43 +126,10 @@ export default function MapBlock({
axes,
title,
boundaryLevel,
releaseId,
dataBlockParentId,
onBoundaryLevelChange,
}: MapBlockProps) {
const [selectedBoundaryLevel, setSelectedBoundaryLevel] =
useState<number>(boundaryLevel);

const queryClient = useQueryClient();
// add existing geoJson to cache, avoiding double fetching
queryClient.setQueryData(
['mapLocationGeoJson', releaseId, dataBlockParentId, boundaryLevel],
meta.locations,
);

const { data: locations, isFetching: isFetchingGeoJson } = useQuery<
LocationFilter[]
>({
queryKey: [
'mapLocationGeoJson',
releaseId,
dataBlockParentId,
selectedBoundaryLevel,
],
queryFn: async () => {
return mapFullTableMetaLocations(
await tableBuilderService.getLocationGeoJson(
releaseId,
dataBlockParentId,
selectedBoundaryLevel,
),
);
},
staleTime: Infinity,
cacheTime: Infinity,
keepPreviousData: true,
refetchOnWindowFocus: false,
refetchOnMount: false,
});
const [isBoundaryLevelChanging, setIsBoundaryLevelChanging] =
useToggle(false);

const axisMajor = useMemo<AxisConfiguration>(
() => ({
Expand All @@ -178,13 +141,8 @@ export default function MapBlock({
);

const dataSetCategories = useMemo<MapDataSetCategory[]>(() => {
return createMapDataSetCategories(
axisMajor,
data,
meta,
locations ?? meta.locations,
);
}, [axisMajor, data, meta, locations]);
return createMapDataSetCategories(axisMajor, data, meta);
}, [axisMajor, data, meta]);

const dataSetCategoryConfigs = useMemo<Dictionary<MapDataSetCategoryConfig>>(
() =>
Expand Down Expand Up @@ -258,16 +216,31 @@ export default function MapBlock({
(value: string) => {
setSelectedFeature(features?.features.find(feat => feat.id === value));
},
[setSelectedFeature, features],
[features],
);

const handleDataSetChange = useCallback(
(value: string) => {
async (value: string) => {
setSelectedDataSetKey(value);
setSelectedBoundaryLevel(
dataSetCategoryConfigs[value].boundaryLevel ?? boundaryLevel,
);
if (
dataSetCategoryConfigs[value].boundaryLevel ??
boundaryLevel !== selectedDataSetConfig.boundaryLevel ??
boundaryLevel
) {
setIsBoundaryLevelChanging.on();
await onBoundaryLevelChange(
dataSetCategoryConfigs[value].boundaryLevel ?? boundaryLevel,
);
setIsBoundaryLevelChanging.off();
}
},
[dataSetCategoryConfigs, boundaryLevel],
[
dataSetCategoryConfigs,
boundaryLevel,
onBoundaryLevelChange,
setIsBoundaryLevelChanging,
selectedDataSetConfig,
],
);

if (
Expand Down Expand Up @@ -298,11 +271,21 @@ export default function MapBlock({
width: (width && `${width}px`) || '100%',
height: `${height || 600}px`,
}}
className={classNames(styles.map, 'dfe-print-break-avoid')}
className={classNames(styles.map, 'dfe-print-break-avoid', {
[styles.mapLoading]: isBoundaryLevelChanging,
})}
center={position}
minZoom={5}
zoom={5}
>
<LoadingSpinner
className="govuk-!-margin-top-8"
loading={isBoundaryLevelChanging}
text="Loading map"
size="lg"
hideText
alert
/>
<MapGeoJSON
dataSetCategoryConfigs={dataSetCategoryConfigs}
features={features}
Expand All @@ -311,7 +294,6 @@ export default function MapBlock({
selectedDataSetKey={selectedDataSetKey}
selectedFeature={selectedFeature}
onSelectFeature={setSelectedFeature}
isLoading={isFetchingGeoJson}
/>
</MapContainer>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import { FeatureCollection } from 'geojson';
import Leaflet, { Layer, PathOptions } from 'leaflet';
import React, { useEffect, useRef, useState } from 'react';
import { GeoJSON, useMap } from 'react-leaflet';
import LoadingSpinner from '@common/components/LoadingSpinner';

interface Props {
dataSetCategoryConfigs: Dictionary<MapDataSetCategoryConfig>;
Expand All @@ -23,7 +22,6 @@ interface Props {
height: number;
width?: number;
onSelectFeature: (feature: MapFeature) => void;
isLoading: boolean;
}

export default function MapGeoJSON({
Expand All @@ -34,7 +32,6 @@ export default function MapGeoJSON({
selectedDataSetKey,
dataSetCategoryConfigs,
onSelectFeature,
isLoading = false,
}: Props) {
const map = useMap();
const container = useRef<HTMLDivElement>(null);
Expand Down Expand Up @@ -168,21 +165,15 @@ export default function MapGeoJSON({
return {
color: '#cfdce3',
fillColor: '#003078',
fillOpacity: isLoading ? 0.05 : 0.1,
fillOpacity: 0.1,
stroke: false,
weight: 1,
};
}}
ref={ukRef}
/>
)}
<LoadingSpinner
className="govuk-!-margin-top-8"
loading={isLoading}
text="fetching geometry for data selection"
size="lg"
hideText
/>

{features && (
<GeoJSON
ref={geometryRef}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ describe('ChartRenderer', () => {
const testFullTableMeta = mapFullTable(testChartTableData);
const testMapChartRenderer: ChartRendererProps = {
type: 'map',
releaseId: 'releaseId',
dataBlockParentId: 'dataBlockParentId',
meta: testFullTableMeta.subjectMeta,
data: testFullTableMeta.results,
alt: '',
Expand All @@ -55,6 +53,7 @@ describe('ChartRenderer', () => {
},
boundaryLevel: 1,
map: { dataSetConfigs: [] },
onBoundaryLevelChange: () => {},
};

test('renders auto-generated boundary level footnote successfully', async () => {
Expand Down
Loading

0 comments on commit 62dc8da

Please sign in to comment.