From 41f52ef6ba9e6fb24cbc3f64ee14cdc15e1d0881 Mon Sep 17 00:00:00 2001 From: Ahmedhossamdev Date: Thu, 14 Nov 2024 18:54:06 +0200 Subject: [PATCH] fix: prevent multiple location selections by locking the selection process --- src/components/trip-planner/TripPlan.svelte | 31 +++++++++++++-------- 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/src/components/trip-planner/TripPlan.svelte b/src/components/trip-planner/TripPlan.svelte index af75512..455f09e 100644 --- a/src/components/trip-planner/TripPlan.svelte +++ b/src/components/trip-planner/TripPlan.svelte @@ -17,6 +17,7 @@ let fromMarker; let toMarker; let loading = false; + let lockSelectLocation = false; const dispatch = createEventDispatcher(); @@ -74,18 +75,25 @@ } async function selectLocation(suggestion, isFrom) { - if (isFrom) { - const response = await geocodeLocation(suggestion.text); - selectedFrom = response.location.geometry.location; - fromMarker = mapProvider.addPinMarker(selectedFrom, 'From'); - fromPlace = suggestion.text; - fromResults = []; - } else { + if (lockSelectLocation) return; + lockSelectLocation = true; + try { const response = await geocodeLocation(suggestion.text); - selectedTo = response.location.geometry.location; - toMarker = mapProvider.addPinMarker(selectedTo, 'To'); - toPlace = suggestion.text; - toResults = []; + if (isFrom) { + selectedFrom = response.location.geometry.location; + fromMarker = mapProvider.addPinMarker(selectedFrom, 'From'); + fromPlace = suggestion.text; + fromResults = []; + } else { + selectedTo = response.location.geometry.location; + toMarker = mapProvider.addPinMarker(selectedTo, 'To'); + toPlace = suggestion.text; + toResults = []; + } + } catch (error) { + console.error('Error selecting location:', error); + } finally { + lockSelectLocation = false; } } @@ -147,7 +155,6 @@ } } - // clear input fields when the tab is switched onMount(() => { if (browser) { window.addEventListener('tabSwitched', () => {