Skip to content

Commit

Permalink
feat: reset form location on map location change
Browse files Browse the repository at this point in the history
  • Loading branch information
friedjoff committed Sep 10, 2020
1 parent 288880d commit b2c5df9
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/components/MapLocation.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import mapPositionIcon from '../icons/mapPosition.svg';
import { MapContext } from '../spatial/components/Map';
import Mapbox from '../spatial/components/layer/Mapbox';
import Vector from '../spatial/components/layer/Vector';
import { setMapLocation } from '../store/actions';
import { setFormLocation, setMapLocation } from '../store/actions';

const getKey = (sl) =>
(
Expand Down Expand Up @@ -43,20 +43,30 @@ const vectorSource = new VectorSource({
features: [iconFeature],
});

const initialFormLocation = {
forestEcoregion: null,
altitudinalZone: null,
silverFirArea: null,
targetAltitudinalZone: null,
};

function MapLocation() {
const map = useContext(MapContext);
const dispatch = useDispatch();
const mapLocation = useSelector((state) => state.mapLocation);

useEffect(() => {
const handleCoords = ({ coordinate }) => {
const handleCoords = ({ coordinate }, resetFormLocation = true) => {
iconFeature.getGeometry().setCoordinates(coordinate);
const pixel = map.getPixelFromCoordinate(coordinate);
const features = map.getFeaturesAtPixel(pixel) || [];
const location = features
.filter((feature) => feature.properties && feature.properties.code)
.reduce(featuresToLocation, {});
dispatch(setMapLocation({ ...location, coordinate: to2056(coordinate) }));
if (resetFormLocation) {
dispatch(setFormLocation(initialFormLocation));
}
};
const waitForLoad = () => {
const mapboxLayer = map
Expand All @@ -65,7 +75,7 @@ function MapLocation() {
.find((layer) => layer instanceof Mapbox.Layer);
if (mapboxLayer && mapLocation && mapLocation.coordinate) {
const coordinate = to3857(mapLocation.coordinate);
mapboxLayer.on('loadend', () => handleCoords({ coordinate }));
mapboxLayer.on('loadend', () => handleCoords({ coordinate }, false));
map.getLayers().un('propertychange', waitForLoad);
}
};
Expand Down

0 comments on commit b2c5df9

Please sign in to comment.