Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refine desktop location fetch #989

Merged
merged 5 commits into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions i18n/en-US.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,14 @@ actions:
setPaymentError: "Error setting payment info:"
setRequestStatusError: "Error setting request status:"
location:
deniedAccessAlert: >
Access to your location is blocked.

To use your current location, enable location permissions from your
browser, and reload the page.
geolocationNotSupportedError: Geolocation not supported by your browser
unknownPositionError: Unknown error getting position
userDeniedPermission: User denied permission
map:
currentLocation: (Current Location)
user:
Expand Down
8 changes: 7 additions & 1 deletion i18n/fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,14 @@ actions:
setPaymentError: "Erreur sur les coordonnées de paiement :"
setRequestStatusError: "Erreur sur l'état de la requête :"
location:
deniedAccessAlert: >
L'accès à votre position est refusé.

Pour utiliser votre emplacement actuel, permettez-en l'accès depuis votre
navigateur, et ouvrez de nouveau cette page.
geolocationNotSupportedError: La géolocalisation n'est pas prise en charge par votre navigateur.
unknownPositionError: Erreur inconnue lors de la détection de votre emplacement.
userDeniedPermission: Refusé par l'utilisateur
map:
currentLocation: (Emplacement actuel)
user:
Expand Down Expand Up @@ -178,7 +184,7 @@ components:
AdvancedOptions:
bannedRoutes: Choisissez les lignes à éviter…
bikeTolerance: Tolérance au vélo
preferredRoutes: Choisissez les lignes preferées
preferredRoutes: Choisissez les lignes préferées
walkTolerance: Tolérance à la marche
AfterSignInScreen:
mainTitle: Redirection...
Expand Down
27 changes: 24 additions & 3 deletions lib/actions/location.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { createAction } from 'redux-actions'
import { Dispatch } from 'redux'
import { IntlShape } from 'react-intl'
import { isMobile } from '@opentripplanner/core-utils/lib/ui'

import { setLocationToCurrent } from './map'

Expand Down Expand Up @@ -47,9 +48,29 @@ export function getCurrentPosition(
// On error
(error) => {
console.log('error getting current position', error)
// FIXME, analyze error code to produce better error message.
// See https://developer.mozilla.org/en-US/docs/Web/API/GeolocationPositionError
dispatch(receivedPositionError({ error }))
// On desktop, after user clicks "Use location" from the location fields,
// show an alert and explain if location is blocked.
// TODO: Consider moving the handling of unavailable location to the location-field component.
if (!isMobile() && error.code === 1) {
window.alert(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bit clunky, but I think good enough for now! Could we possibly add a TODO here to move this code into location-field and handle this more elgantly

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added the TODO in 933d34a.

intl.formatMessage({
id: 'actions.location.deniedAccessAlert'
})
)
}
const newError = { ...error }
if (error.code === 1) {
// i18n for user-denied location message (error.code = 1 on secure origins).
if (
window.location.protocol === 'https:' ||
window.location.host.startsWith('localhost:')
) {
newError.message = intl.formatMessage({
id: 'actions.location.userDeniedPermission'
})
}
}
dispatch(receivedPositionError({ error: newError }))
},
// Options
{ enableHighAccuracy: true }
Expand Down
6 changes: 2 additions & 4 deletions lib/components/app/responsive-webapp.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,9 @@ class ResponsiveWebapp extends Component {
}
}

// Test location availability on load,
// so it is reported correctly by the location fields.
getCurrentPosition(intl)

if (isMobile()) {
// Test location availability on load
getCurrentPosition(intl)
// Also, watch for changes in position on mobile
navigator.geolocation.watchPosition(
// On success
Expand Down
Loading