Skip to content

Commit

Permalink
fix: crash when filtering a map that is being viewed as chart (#2246)
Browse files Browse the repository at this point in the history
* fix: mapViews were already extracted prior to getFilteredVisualization

* fix: do not remove id if we are using the original plugin
  • Loading branch information
jenniferarnesen authored Mar 15, 2023
1 parent 3e5ee53 commit d1cf7b8
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 40 deletions.
11 changes: 2 additions & 9 deletions src/components/Item/VisualizationItem/Visualization/MapPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import NoVisualizationMessage from './NoVisualizationMessage.js'

const mapViewIsEELayer = (mapView) => mapView.layer.includes('earthEngine')

const MapPlugin = ({ visualization, style, ...pluginProps }) => {
const MapPlugin = ({ visualization, ...pluginProps }) => {
const { isDisconnected: offline } = useDhis2ConnectionStatus()

if (offline && visualization.mapViews?.find(mapViewIsEELayer)) {
Expand All @@ -20,17 +20,10 @@ const MapPlugin = ({ visualization, style, ...pluginProps }) => {
)
}

return (
<IframePlugin
visualization={visualization}
style={style}
{...pluginProps}
/>
)
return <IframePlugin visualization={visualization} {...pluginProps} />
}

MapPlugin.propTypes = {
style: PropTypes.object,
visualization: PropTypes.object,
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,16 @@ const Visualization = ({
[availableHeight, availableWidth]
)

const visualizationConfig = useMemo(
() => getVisualizationConfig(visualization, originalType, activeType),
[visualization, activeType, originalType]
)
const visualizationConfig = useMemo(() => {
if (originalType === EVENT_VISUALIZATION) {
return visualization
}

const filteredVisualization = useMemo(
() =>
getFilteredVisualization(
visualizationConfig,
itemFilters,
originalType
),
[visualizationConfig, itemFilters, originalType]
)
return getFilteredVisualization(
getVisualizationConfig(visualization, originalType, activeType),
itemFilters
)
}, [visualization, activeType, originalType, itemFilters])

const filterVersion = useMemo(() => uniqueId(), [])

Expand Down Expand Up @@ -104,7 +100,7 @@ const Visualization = ({
case VISUALIZATION: {
return (
<IframePlugin
visualization={filteredVisualization}
visualization={visualizationConfig}
{...iFramePluginProps}
/>
)
Expand Down Expand Up @@ -136,22 +132,20 @@ const Visualization = ({
</>
)
}

case MAP: {
return (
<MapPlugin
visualization={filteredVisualization}
visualization={visualizationConfig}
{...iFramePluginProps}
/>
)
}

default: {
return pluginIsAvailable(activeType || item.type, d2) ? (
<LegacyPlugin
item={item}
activeType={activeType}
visualization={filteredVisualization}
visualization={visualizationConfig}
filterVersion={filterVersion}
style={style}
{...rest}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ describe('getVisualizationConfig', () => {
const actualResult = getVisualizationConfig(visualization, MAP, MAP)
const expectedResult = {
...visualization,
id: undefined,
}

expect(actualResult).toEqual(expectedResult)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { MAP } from '../../../../modules/itemTypes.js'

const mapViewIsThematicOrEvent = (mapView) =>
mapView.layer.includes('thematic') || mapView.layer.includes('event')

const getFilteredMap = (visualization, filters) => {
// apply filters only to thematic and event layers
const mapViews = visualization.mapViews.map((mapView) => {
if (mapViewIsThematicOrEvent(mapView)) {
return getFilteredVisualization(mapView, filters)
return getFilteredNonMap(mapView, filters)
}

return mapView
Expand All @@ -19,15 +17,7 @@ const getFilteredMap = (visualization, filters) => {
}
}

const getFilteredVisualization = (visualization, filters, originalType) => {
if (!Object.keys(filters).length) {
return visualization
}

if (originalType === MAP) {
return getFilteredMap(visualization, filters)
}

const getFilteredNonMap = (visualization, filters) => {
// deep clone objects in filters to avoid changing the visualization in the Redux store
const visRows = visualization.rows.map((obj) => ({ ...obj }))
const visColumns = visualization.columns.map((obj) => ({ ...obj }))
Expand Down Expand Up @@ -64,4 +54,16 @@ const getFilteredVisualization = (visualization, filters, originalType) => {
}
}

const getFilteredVisualization = (visualization, filters) => {
if (!Object.keys(filters).length) {
return visualization
}

if (visualization.mapViews) {
return getFilteredMap(visualization, filters)
}

return getFilteredNonMap(visualization, filters)
}

export default getFilteredVisualization
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ const getWithoutId = (object) => ({
})

const getVisualizationConfig = (visualization, originalType, activeType) => {
if (originalType === activeType) {
return visualization
}

if (originalType === MAP && originalType !== activeType) {
const thematicMapViews = getThematicMapViews(visualization)

Expand Down

0 comments on commit d1cf7b8

Please sign in to comment.