From 2dacb7b4b383882083032b056b70dae9c03a517e Mon Sep 17 00:00:00 2001 From: binh-dam-ibigroup <56846598+binh-dam-ibigroup@users.noreply.github.com> Date: Tue, 29 Aug 2023 16:11:57 -0400 Subject: [PATCH 1/4] fix(actions/location): Display alert if location was denied. Also, don't fetch location on loading the desktop view. --- i18n/en-US.yml | 5 +++++ lib/actions/location.tsx | 10 ++++++++++ lib/components/app/responsive-webapp.js | 6 ++---- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/i18n/en-US.yml b/i18n/en-US.yml index 613156575..e518a7672 100644 --- a/i18n/en-US.yml +++ b/i18n/en-US.yml @@ -34,6 +34,11 @@ 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 map: diff --git a/lib/actions/location.tsx b/lib/actions/location.tsx index a336efb18..64e9e8921 100644 --- a/lib/actions/location.tsx +++ b/lib/actions/location.tsx @@ -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' @@ -47,6 +48,15 @@ export function getCurrentPosition( // On error (error) => { console.log('error getting current position', error) + // On desktop, after user clicks "Use location" from the location fields, + // show an alert and explain if location is blocked. + if (!isMobile() && error.code === 1) { + window.alert( + intl.formatMessage({ + id: 'actions.location.deniedAccessAlert' + }) + ) + } // FIXME, analyze error code to produce better error message. // See https://developer.mozilla.org/en-US/docs/Web/API/GeolocationPositionError dispatch(receivedPositionError({ error })) diff --git a/lib/components/app/responsive-webapp.js b/lib/components/app/responsive-webapp.js index e135c16b1..0ae74b8a7 100644 --- a/lib/components/app/responsive-webapp.js +++ b/lib/components/app/responsive-webapp.js @@ -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 From 11e8a613640ee0b0a343fe2617832b4e466e9c44 Mon Sep 17 00:00:00 2001 From: binh-dam-ibigroup <56846598+binh-dam-ibigroup@users.noreply.github.com> Date: Tue, 29 Aug 2023 16:54:36 -0400 Subject: [PATCH 2/4] improvement(actions/location): Add i18n to user-denied error messages. --- i18n/en-US.yml | 1 + lib/actions/location.tsx | 16 +++++++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/i18n/en-US.yml b/i18n/en-US.yml index e518a7672..8c438529b 100644 --- a/i18n/en-US.yml +++ b/i18n/en-US.yml @@ -41,6 +41,7 @@ actions: 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: diff --git a/lib/actions/location.tsx b/lib/actions/location.tsx index 64e9e8921..40ffeaa93 100644 --- a/lib/actions/location.tsx +++ b/lib/actions/location.tsx @@ -57,9 +57,19 @@ export function getCurrentPosition( }) ) } - // FIXME, analyze error code to produce better error message. - // See https://developer.mozilla.org/en-US/docs/Web/API/GeolocationPositionError - dispatch(receivedPositionError({ error })) + 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 } From 1b84270e8789820a5ded4a077a568b7304dc690e Mon Sep 17 00:00:00 2001 From: binh-dam-ibigroup <56846598+binh-dam-ibigroup@users.noreply.github.com> Date: Tue, 29 Aug 2023 17:03:38 -0400 Subject: [PATCH 3/4] chore(i18n): Add missing French. --- i18n/fr.yml | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/i18n/fr.yml b/i18n/fr.yml index 742db4275..5c41e72fc 100644 --- a/i18n/fr.yml +++ b/i18n/fr.yml @@ -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: @@ -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... From 933d34a0c8fc1838653a27433651b5e21b92c218 Mon Sep 17 00:00:00 2001 From: binh-dam-ibigroup <56846598+binh-dam-ibigroup@users.noreply.github.com> Date: Wed, 30 Aug 2023 10:37:38 -0400 Subject: [PATCH 4/4] refactor(actions/location): Apply PR review feedback. --- lib/actions/location.tsx | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/actions/location.tsx b/lib/actions/location.tsx index 40ffeaa93..c62e9b740 100644 --- a/lib/actions/location.tsx +++ b/lib/actions/location.tsx @@ -50,6 +50,7 @@ export function getCurrentPosition( console.log('error getting current position', 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( intl.formatMessage({