Skip to content

Commit

Permalink
Fix LegendWidget update logic for multiple legends case (#889)
Browse files Browse the repository at this point in the history
  • Loading branch information
juandjara authored Jul 22, 2024
1 parent a8c90ef commit 48a293a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Not released

- fix LegendWidget update logic for multiple legends case [#889](https://github.com/CartoDB/carto-react/pull/889)
- onStateChange callback for all widgets [#886](https://github.com/CartoDB/carto-react/pull/886)

## 3.0.0
Expand Down
11 changes: 6 additions & 5 deletions packages/react-redux/src/slices/cartoSlice.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,15 @@ export const createCartoSlice = (initialState) => {
const layer = state.layers[action.payload.id];
if (layer) {
const newLayer = { ...layer, ...action.payload.layerAttributes };
const hasLegendUpdate = layer.legend && newLayer.legend;
// Merge legend object if it's not an array (for the case of multiple legend objects per layer)
if (hasLegendUpdate && !Array.isArray(newLayer.legend)) {
newLayer.legend = { ...layer.legend, ...newLayer.legend };
}

// TODO: Study if we should use a deepmerge fn
state.layers[action.payload.id] = {
...newLayer,
...(layer.legend &&
newLayer.legend && {
legend: { ...layer.legend, ...newLayer.legend }
})
...newLayer
};
}
},
Expand Down
11 changes: 6 additions & 5 deletions packages/react-widgets/src/widgets/LegendWidget.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState } from 'react';
import React, { useMemo, useState } from 'react';
import { updateLayer } from '@carto/react-redux/';
import { LegendWidgetUI } from '@carto/react-ui';
import { useDispatch, useSelector } from 'react-redux';
Expand All @@ -17,12 +17,13 @@ import { useMediaQuery } from '@mui/material';
*/
function LegendWidget({ customLegendTypes, initialCollapsed, layerOrder = [], title }) {
const dispatch = useDispatch();
const layers = useSelector((state) =>
const reduxLayers = useSelector((state) => state.carto.layers);
const layers = useMemo(() => {
sortLayers(
Object.values(state.carto.layers).filter((layer) => !!layer.legend),
Object.values(reduxLayers).filter((layer) => !!layer.legend),
layerOrder
).filter((l) => !!l.legend)
);
).filter((l) => !!l.legend);
}, [reduxLayers, layerOrder]);

const [collapsed, setCollapsed] = useState(initialCollapsed);
const isMobile = useMediaQuery((theme) => theme.breakpoints.down('sm'));
Expand Down

0 comments on commit 48a293a

Please sign in to comment.