From 368c799933fd2e9081320fa1c48fbaa10b780b1c Mon Sep 17 00:00:00 2001 From: "Phyks (Lucas Verney)" Date: Thu, 17 Jan 2019 22:43:13 +0100 Subject: [PATCH] Fix report fetching and closest report emphasis --- src/components/Map.vue | 18 +++++++++++++----- src/store/actions.js | 4 ++-- src/tools/geometry.js | 5 ++++- src/tools/geometry.spec.js | 2 +- src/views/Map.vue | 8 +++----- 5 files changed, 23 insertions(+), 14 deletions(-) diff --git a/src/components/Map.vue b/src/components/Map.vue index a25d569..98adb00 100644 --- a/src/components/Map.vue +++ b/src/components/Map.vue @@ -254,9 +254,13 @@ export default { }, onMoveEnd() { const view = this.map.getView(); - if (this.onMapCenterManualUpdate && !this.isProgrammaticMove) { + if (this.onMapCenterUpdate) { const mapCenterLonLat = toLonLat(view.getCenter()); - this.onMapCenterManualUpdate([mapCenterLonLat[1], mapCenterLonLat[0]]); + const center = [mapCenterLonLat[1], mapCenterLonLat[0]]; + this.onMapCenterUpdate(center); + if (!this.isProgrammaticMove) { + this.$store.dispatch('setCurrentMapCenter', { center }); + } } // Show recenter button and call the callback if zoom is updated manually const zoom = view.getZoom(); @@ -486,7 +490,7 @@ export default { heading: Number, // in degrees, clockwise wrt north markers: Array, onPress: Function, - onMapCenterManualUpdate: Function, + onMapCenterUpdate: Function, onMapZoomManualUpdate: Function, polyline: Array, positionLatLng: Array, @@ -601,10 +605,14 @@ export default { // TODO: Take into account the history of positions for the direction if (closestReport.id !== -1) { // Only open the details if the box was not just closed - if (this.$store.state.reportDetails.previousId !== closestReport.id) { + // Don't dispatch useless actions if emphasized report is not updated + if ( + this.$store.state.reportDetails.previousId !== closestReport.id + && this.$store.state.reportDetails.id !== closestReport.id + ) { this.$store.dispatch('showReportDetails', { id: closestReport.id, userAsked: false }); } - } else { + } else if (isReportDetailsAlreadyShown) { this.$store.dispatch('hideReportDetails'); } } diff --git a/src/store/actions.js b/src/store/actions.js index 69c6f13..cdae29f 100644 --- a/src/store/actions.js +++ b/src/store/actions.js @@ -155,7 +155,7 @@ export function populateInitialStateFromStorage({ commit }) { }); } -export function fetchReports({ commit, state }) { +export function fetchReports({ commit }, { center }) { commit(IS_LOADING); return api.getActiveReports() .then((reports) => { @@ -167,7 +167,7 @@ export function fetchReports({ commit, state }) { } return pointToPointDistance( [report.attributes.lat, report.attributes.lng], - state.map.center, + center, ) < 10000; }, ); diff --git a/src/tools/geometry.js b/src/tools/geometry.js index 60e5e13..ecab57b 100644 --- a/src/tools/geometry.js +++ b/src/tools/geometry.js @@ -275,7 +275,10 @@ export function pointToGeometryDistance(latLng, geometry) { } if (geometry.type === 'Polygon') { - return pointToPolygonDistance(latLng, lngLatCoordinates.map(item => item.reverse())); + return pointToPolygonDistance( + latLng, + lngLatCoordinates[0].map(item => [].concat(item).reverse()), + ); } // Unsupported geometry diff --git a/src/tools/geometry.spec.js b/src/tools/geometry.spec.js index 9005b22..613625a 100644 --- a/src/tools/geometry.spec.js +++ b/src/tools/geometry.spec.js @@ -152,7 +152,7 @@ describe('Geometry tools', function() { }); it('should return correct distance for a polygon', function () { - var polygon = { type: 'Polygon', coordinates: [[2.3190774, 48.809982], [2.3320935, 48.8176872], [2.3323712, 48.8182127], [2.3143633, 48.8222148], [2.314133, 48.8222632], [2.3002323, 48.8115136], [2.3000166, 48.8113242], [2.3190774, 48.809982]] }; + var polygon = { type: 'Polygon', coordinates: [[[2.3190774, 48.809982], [2.3320935, 48.8176872], [2.3323712, 48.8182127], [2.3143633, 48.8222148], [2.314133, 48.8222632], [2.3002323, 48.8115136], [2.3000166, 48.8113242], [2.3190774, 48.809982]]] }; assert(Math.abs(geometry.pointToGeometryDistance([48.82787, 2.32686], polygon) - 900) / 900 < 1 / 100); }); diff --git a/src/views/Map.vue b/src/views/Map.vue index 0e31714..bc8073b 100644 --- a/src/views/Map.vue +++ b/src/views/Map.vue @@ -16,7 +16,7 @@ :hasGeolocationTracking="!hasCenterProvidedByRoute && hasGeolocationTracking" :heading="currentLocation.heading" :markers="reportsMarkers" - :onMapCenterManualUpdate="onMapCenterManualUpdate" + :onMapCenterUpdate="onMapCenterUpdate" :onMapZoomManualUpdate="onMapZoomManualUpdate" :onPress="showReportDialog" :polyline="positionHistory" @@ -260,7 +260,7 @@ export default { this.hasPromptedGeolocation = true; this.$store.dispatch('setLocationWatcherId', { id: watchID }); }, - onMapCenterManualUpdate(center) { + onMapCenterUpdate(center) { // Update reports by default let distanceFromPreviousPoint = constants.UPDATE_REPORTS_DISTANCE_THRESHOLD + 1; @@ -282,10 +282,8 @@ export default { store.dispatch('setLastReportFetchingLocation', { locationLatLng: center, }); - store.dispatch('fetchReports'); + store.dispatch('fetchReports', { center }); } - - this.$store.dispatch('setCurrentMapCenter', { center }); }, onMapZoomManualUpdate(zoom) { this.$store.dispatch('setCurrentMapZoom', { zoom });