Skip to content

Commit f2afabf

Browse files
Merge pull request #791 from open-formulieren/bug/5038-map-shapes-not-shown-in-summary-view
Missing map shapes in summary step
2 parents 57bd820 + 7d1521a commit f2afabf

File tree

2 files changed

+47
-27
lines changed

2 files changed

+47
-27
lines changed

src/components/Map/LeafletMap.jsx

+30-27
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
leafletGestureHandlingText,
2020
searchControlMessages,
2121
} from './translations';
22+
import {GeoJsonGeometry} from './types';
2223

2324
// Run some Leaflet-specific patches...
2425
initialize();
@@ -92,18 +93,6 @@ const LeaftletMap = ({
9293
onGeoJsonGeometrySet?.(newFeatureLayer.toGeoJSON().geometry);
9394
};
9495

95-
useEffect(() => {
96-
// Remove all shapes from the map.
97-
// Only `geoJsonGeometry` should be shown on the map.
98-
featureGroupRef.current?.clearLayers();
99-
if (!geoJsonGeometry) {
100-
return;
101-
}
102-
// Add the current `geoJsonGeometry` as shape
103-
const layer = Leaflet.GeoJSON.geometryToLayer(geoJsonGeometry);
104-
featureGroupRef.current?.addLayer(layer);
105-
}, [geoJsonGeometry]);
106-
10796
return (
10897
<>
10998
<MapContainer
@@ -143,6 +132,7 @@ const LeaftletMap = ({
143132
}}
144133
/>
145134
)}
135+
<Geometry geoJsonGeometry={geoJsonGeometry} featureGroupRef={featureGroupRef} />
146136
</FeatureGroup>
147137
{coordinates && <MapView coordinates={coordinates} />}
148138
{!disabled && (
@@ -171,21 +161,7 @@ const LeaftletMap = ({
171161
};
172162

173163
LeaftletMap.propTypes = {
174-
geoJsonGeometry: PropTypes.oneOfType([
175-
PropTypes.shape({
176-
type: PropTypes.oneOf(['Point']).isRequired,
177-
coordinates: PropTypes.arrayOf(PropTypes.number).isRequired,
178-
}),
179-
PropTypes.shape({
180-
type: PropTypes.oneOf(['LineString']).isRequired,
181-
coordinates: PropTypes.arrayOf(PropTypes.arrayOf(PropTypes.number)).isRequired,
182-
}),
183-
PropTypes.shape({
184-
type: PropTypes.oneOf(['Polygon']).isRequired,
185-
coordinates: PropTypes.arrayOf(PropTypes.arrayOf(PropTypes.arrayOf(PropTypes.number)))
186-
.isRequired,
187-
}),
188-
]),
164+
geoJsonGeometry: GeoJsonGeometry,
189165
onGeoJsonGeometrySet: PropTypes.func,
190166
interactions: PropTypes.shape({
191167
polyline: PropTypes.bool,
@@ -196,6 +172,33 @@ LeaftletMap.propTypes = {
196172
tileLayerUrl: PropTypes.string,
197173
};
198174

175+
const Geometry = ({geoJsonGeometry, featureGroupRef}) => {
176+
useEffect(() => {
177+
if (!featureGroupRef.current) {
178+
// If there is no feature group, nothing should be done...
179+
return;
180+
}
181+
182+
// Remove all shapes from the map.
183+
// Only the data from `geoJsonGeometry` should be shown on the map.
184+
featureGroupRef.current.clearLayers();
185+
if (!geoJsonGeometry) {
186+
return;
187+
}
188+
189+
// Add the `geoJsonGeometry` data as shape.
190+
const layer = Leaflet.GeoJSON.geometryToLayer(geoJsonGeometry);
191+
featureGroupRef.current.addLayer(layer);
192+
}, [featureGroupRef, geoJsonGeometry]);
193+
194+
return null;
195+
};
196+
197+
Geometry.propTypes = {
198+
geoJsonGeometry: GeoJsonGeometry,
199+
featureGroupRef: PropTypes.object.isRequired,
200+
};
201+
199202
// Set the map view if coordinates are provided
200203
const MapView = ({coordinates = null}) => {
201204
const map = useMap();

src/components/Map/types.jsx

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import PropTypes from 'prop-types';
2+
3+
export const GeoJsonGeometry = PropTypes.oneOfType([
4+
PropTypes.shape({
5+
type: PropTypes.oneOf(['Point']).isRequired,
6+
coordinates: PropTypes.arrayOf(PropTypes.number).isRequired,
7+
}),
8+
PropTypes.shape({
9+
type: PropTypes.oneOf(['LineString']).isRequired,
10+
coordinates: PropTypes.arrayOf(PropTypes.arrayOf(PropTypes.number)).isRequired,
11+
}),
12+
PropTypes.shape({
13+
type: PropTypes.oneOf(['Polygon']).isRequired,
14+
coordinates: PropTypes.arrayOf(PropTypes.arrayOf(PropTypes.arrayOf(PropTypes.number)))
15+
.isRequired,
16+
}),
17+
]);

0 commit comments

Comments
 (0)