From 471f533117edf94bf14388ba145429da43e64b44 Mon Sep 17 00:00:00 2001 From: Robin Townsend Date: Fri, 25 Feb 2022 13:01:35 -0500 Subject: [PATCH] Clean up error listener when location picker closes --- .../views/location/LocationPicker.tsx | 30 ++++++++++--------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/src/components/views/location/LocationPicker.tsx b/src/components/views/location/LocationPicker.tsx index 6683016e0ac..b6be4e10c89 100644 --- a/src/components/views/location/LocationPicker.tsx +++ b/src/components/views/location/LocationPicker.tsx @@ -111,20 +111,7 @@ class LocationPicker extends React.Component { this.geolocate.trigger(); }); - this.geolocate.on('error', (e: GeolocationPositionError) => { - this.props.onFinished(); - logger.error("Could not fetch location", e); - Modal.createTrackedDialog( - 'Could not fetch location', - '', - ErrorDialog, - { - title: _t("Could not fetch location"), - description: positionFailureMessage(e.code), - }, - ); - }); - + this.geolocate.on('error', this.onGeolocateError); this.geolocate.on('geolocate', this.onGeolocate); } catch (e) { logger.error("Failed to render map", e); @@ -133,6 +120,7 @@ class LocationPicker extends React.Component { } componentWillUnmount() { + this.geolocate?.off('error', this.onGeolocateError); this.geolocate?.off('geolocate', this.onGeolocate); this.context.off(ClientEvent.ClientWellKnown, this.updateStyleUrl); } @@ -154,6 +142,20 @@ class LocationPicker extends React.Component { ); }; + private onGeolocateError = (e: GeolocationPositionError) => { + this.props.onFinished(); + logger.error("Could not fetch location", e); + Modal.createTrackedDialog( + 'Could not fetch location', + '', + ErrorDialog, + { + title: _t("Could not fetch location"), + description: positionFailureMessage(e.code), + }, + ); + }; + private onOk = () => { const position = this.state.position;