Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing map shapes in summary step #791

Merged
merged 1 commit into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 30 additions & 27 deletions src/components/Map/LeafletMap.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
leafletGestureHandlingText,
searchControlMessages,
} from './translations';
import {GeoJsonGeometry} from './types';

// Run some Leaflet-specific patches...
initialize();
Expand Down Expand Up @@ -92,18 +93,6 @@
onGeoJsonGeometrySet?.(newFeatureLayer.toGeoJSON().geometry);
};

useEffect(() => {
// Remove all shapes from the map.
// Only `geoJsonGeometry` should be shown on the map.
featureGroupRef.current?.clearLayers();
if (!geoJsonGeometry) {
return;
}
// Add the current `geoJsonGeometry` as shape
const layer = Leaflet.GeoJSON.geometryToLayer(geoJsonGeometry);
featureGroupRef.current?.addLayer(layer);
}, [geoJsonGeometry]);

return (
<>
<MapContainer
Expand Down Expand Up @@ -143,6 +132,7 @@
}}
/>
)}
<Geometry geoJsonGeometry={geoJsonGeometry} featureGroupRef={featureGroupRef} />
</FeatureGroup>
{coordinates && <MapView coordinates={coordinates} />}
{!disabled && (
Expand Down Expand Up @@ -171,21 +161,7 @@
};

LeaftletMap.propTypes = {
geoJsonGeometry: PropTypes.oneOfType([
PropTypes.shape({
type: PropTypes.oneOf(['Point']).isRequired,
coordinates: PropTypes.arrayOf(PropTypes.number).isRequired,
}),
PropTypes.shape({
type: PropTypes.oneOf(['LineString']).isRequired,
coordinates: PropTypes.arrayOf(PropTypes.arrayOf(PropTypes.number)).isRequired,
}),
PropTypes.shape({
type: PropTypes.oneOf(['Polygon']).isRequired,
coordinates: PropTypes.arrayOf(PropTypes.arrayOf(PropTypes.arrayOf(PropTypes.number)))
.isRequired,
}),
]),
geoJsonGeometry: GeoJsonGeometry,
onGeoJsonGeometrySet: PropTypes.func,
interactions: PropTypes.shape({
polyline: PropTypes.bool,
Expand All @@ -196,6 +172,33 @@
tileLayerUrl: PropTypes.string,
};

const Geometry = ({geoJsonGeometry, featureGroupRef}) => {
useEffect(() => {
if (!featureGroupRef.current) {
// If there is no feature group, nothing should be done...
return;

Check warning on line 179 in src/components/Map/LeafletMap.jsx

View check run for this annotation

Codecov / codecov/patch

src/components/Map/LeafletMap.jsx#L179

Added line #L179 was not covered by tests
}

// Remove all shapes from the map.
// Only the data from `geoJsonGeometry` should be shown on the map.
featureGroupRef.current.clearLayers();
if (!geoJsonGeometry) {
return;

Check warning on line 186 in src/components/Map/LeafletMap.jsx

View check run for this annotation

Codecov / codecov/patch

src/components/Map/LeafletMap.jsx#L186

Added line #L186 was not covered by tests
}

// Add the `geoJsonGeometry` data as shape.
const layer = Leaflet.GeoJSON.geometryToLayer(geoJsonGeometry);
featureGroupRef.current.addLayer(layer);
}, [featureGroupRef, geoJsonGeometry]);

return null;
};

Geometry.propTypes = {
geoJsonGeometry: GeoJsonGeometry,
featureGroupRef: PropTypes.object.isRequired,
};

// Set the map view if coordinates are provided
const MapView = ({coordinates = null}) => {
const map = useMap();
Expand Down
17 changes: 17 additions & 0 deletions src/components/Map/types.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import PropTypes from 'prop-types';

export const GeoJsonGeometry = PropTypes.oneOfType([
PropTypes.shape({
type: PropTypes.oneOf(['Point']).isRequired,
coordinates: PropTypes.arrayOf(PropTypes.number).isRequired,
}),
PropTypes.shape({
type: PropTypes.oneOf(['LineString']).isRequired,
coordinates: PropTypes.arrayOf(PropTypes.arrayOf(PropTypes.number)).isRequired,
}),
PropTypes.shape({
type: PropTypes.oneOf(['Polygon']).isRequired,
coordinates: PropTypes.arrayOf(PropTypes.arrayOf(PropTypes.arrayOf(PropTypes.number)))
.isRequired,
}),
]);