Fix a bug in automatic refetch of reports after a given distance

This commit is contained in:
Lucas Verney 2018-08-24 18:13:41 +02:00
parent 60f041f5a6
commit d24c326b01
5 changed files with 31 additions and 4 deletions

View File

@ -23,6 +23,7 @@
</template>
<script>
// TODO: Rotate the map according to user heading.
// TODO: Map going outside of container + on resize ?
import 'ol/ol.css';
import Feature from 'ol/Feature';

View File

@ -16,6 +16,7 @@ import {
SET_CURRENT_MAP_CENTER,
SET_CURRENT_MAP_ZOOM,
SET_CURRENT_POSITION,
SET_LAST_REPORT_FETCHING_LOCATION,
SET_LOCATION_ERROR,
SET_LOCATION_WATCHER_ID,
SET_SETTING,
@ -36,6 +37,10 @@ export function fetchReports({ commit }) {
});
}
export function setLastReportFetchingLocation({ commit }, { locationLatLng }) {
return commit(SET_LAST_REPORT_FETCHING_LOCATION, { locationLatLng });
}
export function downvote({ commit }, { id }) {
// Hide details
commit(SHOW_REPORT_DETAILS, { id: null, userAsked: null });

View File

@ -9,6 +9,7 @@ export const REMOVE_UNSENT_REPORT = 'REMOVE_UNSENT_REPORT';
export const SET_CURRENT_MAP_CENTER = 'SET_CURRENT_MAP_CENTER';
export const SET_CURRENT_MAP_ZOOM = 'SET_CURRENT_MAP_ZOOM';
export const SET_CURRENT_POSITION = 'SET_CURRENT_POSITION';
export const SET_LAST_REPORT_FETCHING_LOCATION = 'SET_LAST_REPORT_FETCHING_LOCATION';
export const SET_LOCATION_ERROR = 'SET_LOCATION_ERROR';
export const SET_LOCATION_WATCHER_ID = 'SET_LOCATION_WATCHER_ID';
export const SET_SETTING = 'SET_SETTING';

View File

@ -91,6 +91,7 @@ export const initialState = {
gpx: [],
watcherID: null,
},
lastReportFetchingLocation: [null, null],
map: {
center: [null, null],
zoom: null,
@ -164,6 +165,19 @@ export const mutations = {
},
[types.SET_CURRENT_POSITION](state, { currentLocation }) {
state.location.gpx.push(currentLocation);
if (
!state.lastReportFetchingLocation
|| !state.lastReportFetchingLocation[0]
|| !state.lastReportFetchingLocation[1]
) {
state.lastReportFetchingLocation = [
currentLocation.latitude,
currentLocation.longitude,
];
}
},
[types.SET_LAST_REPORT_FETCHING_LOCATION](state, { locationLatLng }) {
state.lastReportFetchingLocation = locationLatLng;
},
[types.SET_LOCATION_ERROR](state, { error }) {
Vue.set(state.location, 'error', error);

View File

@ -61,14 +61,20 @@ function handlePositionError(error) {
}
function setPosition(position) {
const lastLocation = store.getters.getLastLocation;
if (lastLocation !== null) {
// TODO: Should not be lastLocation
const lastFetchingLocation = store.state.lastReportFetchingLocation;
if (
lastFetchingLocation
&& lastFetchingLocation[0] !== null
&& lastFetchingLocation[1] !== null
) {
const distanceFromPreviousPoint = distance(
[lastLocation.latitude, lastLocation.longitude],
[lastFetchingLocation[0], lastFetchingLocation[1]],
[position.coords.latitude, position.coords.longitude],
);
if (distanceFromPreviousPoint > constants.UPDATE_REPORTS_DISTANCE_THRESHOLD) {
store.dispatch('setLastReportFetchingLocation', {
locationLatLng: [position.coords.latitude, position.coords.longitude],
});
store.dispatch('fetchReports');
}
}