From ad96d614bbcd01b4b66eb19df7a6fd38d5e548f3 Mon Sep 17 00:00:00 2001 From: Stuart Bennett Date: Thu, 19 Dec 2024 15:19:19 +0000 Subject: [PATCH] EES-5542 fix maps rendering with right boundary data --- .../datablocks/components/DataBlockPageTabs.tsx | 9 +++------ .../utils/getMapInitialBoundaryLevel.ts | 17 +++++++++++++++++ .../components/DataBlockTabs.tsx | 5 ++--- 3 files changed, 22 insertions(+), 9 deletions(-) create mode 100644 src/explore-education-statistics-common/src/modules/charts/components/utils/getMapInitialBoundaryLevel.ts diff --git a/src/explore-education-statistics-admin/src/pages/release/datablocks/components/DataBlockPageTabs.tsx b/src/explore-education-statistics-admin/src/pages/release/datablocks/components/DataBlockPageTabs.tsx index 4fcdba099ce..ac32652a9a1 100644 --- a/src/explore-education-statistics-admin/src/pages/release/datablocks/components/DataBlockPageTabs.tsx +++ b/src/explore-education-statistics-admin/src/pages/release/datablocks/components/DataBlockPageTabs.tsx @@ -9,11 +9,13 @@ import dataBlocksService, { CreateReleaseDataBlock, ReleaseDataBlock, } from '@admin/services/dataBlockService'; +import featuredTableService from '@admin/services/featuredTableService'; import LoadingSpinner from '@common/components/LoadingSpinner'; import Tabs from '@common/components/Tabs'; import TabsSection from '@common/components/TabsSection'; import WarningMessage from '@common/components/WarningMessage'; import useAsyncRetry from '@common/hooks/useAsyncRetry'; +import getMapInitialBoundaryLevel from '@common/modules/charts/components/utils/getMapInitialBoundaryLevel'; import isOrphanedDataSet from '@common/modules/charts/util/isOrphanedDataSet'; import { InitialTableToolState } from '@common/modules/table-tool/components/TableToolWizard'; import getInitialStepSubjectMeta from '@common/modules/table-tool/components/utils/getInitialStepSubjectMeta'; @@ -29,7 +31,6 @@ import minDelay from '@common/utils/minDelay'; import { produce } from 'immer'; import omit from 'lodash/omit'; import React, { useCallback, useState } from 'react'; -import featuredTableService from '@admin/services/featuredTableService'; export type SavedDataBlock = CreateReleaseDataBlock & { id?: string; @@ -78,14 +79,10 @@ const DataBlockPageTabs = ({ releaseId, }; - const boundaryLevel = - dataBlock.charts[0]?.type === 'map' - ? dataBlock.charts[0].boundaryLevel - : undefined; const tableData = await tableBuilderService.getTableData( query, releaseId, - boundaryLevel, + getMapInitialBoundaryLevel(dataBlock.charts[0]), ); const { initialStep, subjectMeta } = await getInitialStepSubjectMeta( diff --git a/src/explore-education-statistics-common/src/modules/charts/components/utils/getMapInitialBoundaryLevel.ts b/src/explore-education-statistics-common/src/modules/charts/components/utils/getMapInitialBoundaryLevel.ts new file mode 100644 index 00000000000..9042d781c76 --- /dev/null +++ b/src/explore-education-statistics-common/src/modules/charts/components/utils/getMapInitialBoundaryLevel.ts @@ -0,0 +1,17 @@ +import { Chart } from '@common/services/types/blocks'; +import isEqual from 'lodash/isEqual'; +import sortBy from 'lodash/sortBy'; + +export default function getMapInitialBoundaryLevel( + chart: Chart, +): number | undefined { + if (chart.type !== 'map') return undefined; + + // data set options are ordered alphabetically based on their label text + const firstDataSet = sortBy(chart.legend.items, 'label')[0].dataSet; + const firstDataSetConfig = chart.map?.dataSetConfigs.find(({ dataSet }) => { + return isEqual(dataSet, firstDataSet); + }); + + return firstDataSetConfig?.boundaryLevel ?? chart.boundaryLevel; +} diff --git a/src/explore-education-statistics-common/src/modules/find-statistics/components/DataBlockTabs.tsx b/src/explore-education-statistics-common/src/modules/find-statistics/components/DataBlockTabs.tsx index f09b6a8c623..9bdb2ce583e 100644 --- a/src/explore-education-statistics-common/src/modules/find-statistics/components/DataBlockTabs.tsx +++ b/src/explore-education-statistics-common/src/modules/find-statistics/components/DataBlockTabs.tsx @@ -6,6 +6,7 @@ import WarningMessage from '@common/components/WarningMessage'; import withLazyLoad from '@common/hocs/withLazyLoad'; import ChartRenderer from '@common/modules/charts/components/ChartRenderer'; import { GetInfographic } from '@common/modules/charts/components/InfographicBlock'; +import getMapInitialBoundaryLevel from '@common/modules/charts/components/utils/getMapInitialBoundaryLevel'; import { AxesConfiguration } from '@common/modules/charts/types/chart'; import TimePeriodDataTable from '@common/modules/table-tool/components/TimePeriodDataTable'; import getDefaultTableHeaderConfig from '@common/modules/table-tool/utils/getDefaultTableHeadersConfig'; @@ -43,9 +44,7 @@ const DataBlockTabs = ({ onToggle, }: DataBlockTabsProps) => { const [selectedBoundaryLevel, setSelectedBoundaryLevel] = useState( - dataBlock.charts[0]?.type === 'map' - ? dataBlock.charts[0].boundaryLevel - : undefined, + getMapInitialBoundaryLevel(dataBlock.charts[0]), ); const {