diff --git a/imports/startup/client/geolocation.js b/imports/startup/client/geolocation.js index 5d3a97a..a43d784 100644 --- a/imports/startup/client/geolocation.js +++ b/imports/startup/client/geolocation.js @@ -1,18 +1,20 @@ import { Meteor } from 'meteor/meteor'; +import { ReactiveVar } from 'meteor/reactive-var'; + const geolocation = new ReactiveVar(); -Meteor.startup(function() { - Meteor.call("geo", function (error, response) { - if (error) { - console.warn(error); - } else { - var pos = [ - response.location.latitude, - response.location.longitude - ]; - geolocation.set(pos); - } - }); +Meteor.startup(() => { + Meteor.call('geo', (error, response) => { + if (error) { + console.warn(error); + } else { + const pos = [ + response.location.latitude, + response.location.longitude + ]; + geolocation.set(pos); + } + }); }); export default geolocation; diff --git a/imports/ui/components/DistanceSlider/DistanceSlider.js b/imports/ui/components/DistanceSlider/DistanceSlider.js index 44a60e2..efc541a 100644 --- a/imports/ui/components/DistanceSlider/DistanceSlider.js +++ b/imports/ui/components/DistanceSlider/DistanceSlider.js @@ -30,6 +30,7 @@ class DistanceSlider extends React.Component { }; this.onSliderChange = this.onSliderChange.bind(this); this.onAfterChange = this.onAfterChange.bind(this); + this.props.onChange(10); } onSliderChange(value) { diff --git a/imports/ui/components/SelectionMap/SelectionMap.js b/imports/ui/components/SelectionMap/SelectionMap.js index 1651a5b..7764b16 100644 --- a/imports/ui/components/SelectionMap/SelectionMap.js +++ b/imports/ui/components/SelectionMap/SelectionMap.js @@ -1,8 +1,13 @@ +/* global setTimeout */ +/* eslint-disable react/jsx-indent-props */ +/* eslint-disable import/no-absolute-path */ import React, { Component } from 'react'; import PropTypes from 'prop-types'; import { Map, TileLayer, Marker, CircleMarker, Circle } from 'react-leaflet'; import Leaflet from 'leaflet'; import { translate } from 'react-i18next'; +import { withTracker } from 'meteor/react-meteor-data'; +import update from 'immutability-helper'; import geolocation from '/imports/startup/client/geolocation'; import 'leaflet-graphicscale/dist/Leaflet.GraphicScale.min.css'; import 'leaflet-graphicscale/dist/Leaflet.GraphicScale.min.js'; @@ -25,11 +30,11 @@ class SelectionMap extends Component { constructor(props) { super(props); this.state = { - center: geolocation.get(), - marker: geolocation.get(), + center: props.center, + marker: props.center, zoom: 11, - draggable: true, - modified: false + distance: props.distance, + draggable: true }; this.getMap = this.getMap.bind(this); @@ -41,7 +46,20 @@ class SelectionMap extends Component { } componentDidMount() { - this.addScale(); + if (this.isValidState()) { + this.addScale(); + } + } + + componentWillReceiveProps(nextProps) { + const nextCenter = nextProps.center[0] ? nextProps.center : this.state.center; + const nextMarker = nextProps.center[0] ? nextProps.center : this.state.marker; + this.setState({ + center: nextCenter, + marker: nextMarker, + distance: nextProps.distance || this.state.distance + }); + this.fit(); } componentDidUpdate() { @@ -58,19 +76,18 @@ class SelectionMap extends Component { updatePosition() { const { lat, lng } = this.marker.leafletElement.getLatLng(); + // console.log(`New marker lat ${lat} and lng ${lng}`); const currentDistance = this.state.distance; - this.setState({ - marker: [lat, lng], - modified: true - }); - console.log(currentDistance); + this.setState(update(this.state, { $merge: { marker: [lat, lng] } })); this.props.onSelection({ lat, lng, currentDistance }); this.fit(); } fit() { // console.log("fit!"); - this.getMap().fitBounds(this.distanceCircle.leafletElement.getBounds(), [70, 70]); + if (this.selectionMap && this.distanceCircle) { + this.getMap().fitBounds(this.distanceCircle.leafletElement.getBounds(), [70, 70]); + } } addScale() { @@ -91,69 +108,80 @@ class SelectionMap extends Component { ); } + isValidState() { + return this.state.center && this.state.center[0] && this.state.distance; + } + render() { - this.state.center = !this.state.modified && this.props.lat? [this.props.lat, this.props.lng]: this.state.center; - this.state.marker = !this.state.modified && this.props.lat? [this.props.lat, this.props.lng]: this.state.marker; - this.state.distance = !this.state.modified? this.props.distance: this.state.distance; - this.state.modified = false; + // console.log('render map called'); return (