mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-05 18:34:09 +00:00
fix(planner): drop search and day-panel answers that arrive too late
The desktop day panel stays mounted across a day switch, so a forecast or the stays still loading for the previous day overwrote the new day's once they arrived. The location search in the transport form showed results for text the user had already changed, or refilled the list after a pick. Both now drop an answer for a request that no longer matches.
This commit is contained in:
@@ -57,20 +57,27 @@ export default function LocationSelect({ value, onChange, placeholder, style, pl
|
||||
setResults([])
|
||||
return
|
||||
}
|
||||
// Clearing the timer does nothing to a request already out: an answer for
|
||||
// text the user has since changed, or that lands after a pick, is dropped.
|
||||
let stale = false
|
||||
debounceRef.current = setTimeout(async () => {
|
||||
setLoading(true)
|
||||
try {
|
||||
const data = await mapsApi.search(trimmed, placeLang, locationBias)
|
||||
if (stale) return
|
||||
setResults(data.places || [])
|
||||
setHighlight(-1)
|
||||
} catch {
|
||||
setResults([])
|
||||
if (!stale) setResults([])
|
||||
} finally {
|
||||
setLoading(false)
|
||||
if (!stale) setLoading(false)
|
||||
}
|
||||
}, 320)
|
||||
return () => { if (debounceRef.current) clearTimeout(debounceRef.current) }
|
||||
}, [query, value, locale])
|
||||
return () => {
|
||||
stale = true
|
||||
if (debounceRef.current) clearTimeout(debounceRef.current)
|
||||
}
|
||||
}, [query, value, locale, placeLang])
|
||||
|
||||
const pick = (r: any) => {
|
||||
const lat = Number(r.lat)
|
||||
|
||||
Reference in New Issue
Block a user