Skip to content

Commit

Permalink
fix: do not make geolocation call if offline
Browse files Browse the repository at this point in the history
closes #69
  • Loading branch information
juancarlosfarah committed May 6, 2019
1 parent 4a634d0 commit 611f988
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 21 deletions.
45 changes: 24 additions & 21 deletions src/actions/user/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,30 @@ import {
} from '../../config/messages';

const getGeolocation = async () => async dispatch => {
try {
const geolocation = await getCurrentPosition();
const payload = {
coords: {
accuracy: geolocation.coords.accuracy,
altitude: geolocation.coords.altitude,
altitudeAccuracy: geolocation.coords.altitudeAccuracy,
heading: geolocation.coords.heading,
latitude: geolocation.coords.latitude,
longitude: geolocation.coords.longitude,
speed: geolocation.coords.speed,
},
timestamp: geolocation.timestamp,
};
dispatch({
type: GET_GEOLOCATION_SUCCEEDED,
payload,
});
} catch (e) {
console.error(e);
toastr.error(ERROR_MESSAGE_HEADER, ERROR_GETTING_GEOLOCATION);
// only fetch location if online
if (window.navigator.onLine) {
try {
const geolocation = await getCurrentPosition();
const payload = {
coords: {
accuracy: geolocation.coords.accuracy,
altitude: geolocation.coords.altitude,
altitudeAccuracy: geolocation.coords.altitudeAccuracy,
heading: geolocation.coords.heading,
latitude: geolocation.coords.latitude,
longitude: geolocation.coords.longitude,
speed: geolocation.coords.speed,
},
timestamp: geolocation.timestamp,
};
dispatch({
type: GET_GEOLOCATION_SUCCEEDED,
payload,
});
} catch (e) {
console.error(e);
toastr.error(ERROR_MESSAGE_HEADER, ERROR_GETTING_GEOLOCATION);
}
}
};

Expand Down
7 changes: 7 additions & 0 deletions src/components/SpacesNearby.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ class SpacesNearby extends Component {
this.getSpacesNearby();
}

componentDidUpdate({ geolocation: prevGeolocation }) {
const { geolocation } = this.props;
if (!geolocation.equals(prevGeolocation)) {
this.getSpacesNearby();
}
}

getSpacesNearby = () => {
const { dispatchGetSpacesNearby, geolocation } = this.props;
if (!geolocation.isEmpty()) {
Expand Down

0 comments on commit 611f988

Please sign in to comment.