Skip to content

Commit 2312220

Browse files
refacor(routing): keep reverseGeocoder relevant states together
1 parent 575c9f6 commit 2312220

1 file changed

Lines changed: 16 additions & 7 deletions

File tree

src/plugins/routing/store.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,17 @@ export const useRoutingStore = defineStore('plugins/routing', () => {
4646
let abortController: AbortController | null = null
4747
let draw: Draw | undefined
4848

49-
const reverseGeocodeCoordinateIndex = ref(-1)
49+
const pendingReverseGeocode = ref<{
50+
coordinate: Coordinate
51+
index: number
52+
} | null>(null)
5053

5154
const _currentlyFocusedInput = ref(-1)
5255
const route = ref<Coordinate[]>([[], []])
5356
const routeAddressTexts = ref<(string | null)[]>([null, null])
54-
const reverseGeocodeCoordinate = ref<Coordinate | null>(null)
57+
const reverseGeocodeCoordinate = computed(
58+
() => pendingReverseGeocode.value?.coordinate ?? null
59+
)
5560
const routingResponseData = ref<RoutingResponseData | null>(null)
5661
const selectedPreference = ref('recommended')
5762
const selectedRouteTypesToAvoid = ref<string[]>([])
@@ -163,14 +168,14 @@ export const useRoutingStore = defineStore('plugins/routing', () => {
163168
)
164169

165170
function addCoordinateToRoute(coordinate: Coordinate) {
166-
reverseGeocodeCoordinate.value = coordinate
171+
const index = currentlyFocusedInput.value
172+
pendingReverseGeocode.value = { coordinate, index }
167173
route.value = route.value.toSpliced(
168-
currentlyFocusedInput.value,
174+
index,
169175
1,
170176
coordinate
171177
)
172-
routeAddressTexts.value = routeAddressTexts.value.toSpliced(currentlyFocusedInput.value, 1, '')
173-
reverseGeocodeCoordinateIndex.value = currentlyFocusedInput.value
178+
routeAddressTexts.value = routeAddressTexts.value.toSpliced(index, 1, '')
174179
}
175180

176181
async function fetchRoute(signal: AbortSignal): Promise<RoutingResponseData> {
@@ -318,6 +323,7 @@ export const useRoutingStore = defineStore('plugins/routing', () => {
318323
function reset() {
319324
route.value = [[], []]
320325
routeAddressTexts.value = [null, null]
326+
pendingReverseGeocode.value = null
321327
currentlyFocusedInput.value = -1
322328
selectedPreference.value = 'recommended'
323329
selectedTravelMode.value = 'driving-car'
@@ -342,7 +348,10 @@ export const useRoutingStore = defineStore('plugins/routing', () => {
342348
}
343349

344350
function selectRouteResult(feature: PolarGeoJsonFeature) {
345-
routeAddressTexts.value[reverseGeocodeCoordinateIndex.value] = feature.title
351+
const pendingIndex = pendingReverseGeocode.value?.index
352+
if (pendingIndex !== undefined && pendingIndex > -1) {
353+
routeAddressTexts.value[pendingIndex] = feature.title
354+
}
346355
}
347356

348357
return {

0 commit comments

Comments
 (0)