frontend: create-post: fix city name lookup

Signed-off-by: Nicolas Froger <nicolas@kektus.xyz>
This commit is contained in:
Nicolas Froger 2024-07-26 01:20:00 +02:00
commit 9ab5e684f3
No known key found for this signature in database

View file

@ -167,7 +167,18 @@ function getCityAndCountry() {
fetch(`https://nominatim.openstreetmap.org/reverse?lat=${lat}&lon=${lon}&format=jsonv2`)
.then((resp) => resp.json()).then((resp) => {
form.setFieldValue('city', resp.address.village)
const cityFieldOrder = ['city', 'town', 'borough', 'village', 'suburb', 'municipality', 'county', 'state']
let found = false
for (const field of cityFieldOrder) {
if (Object.hasOwn(resp.address, field)) {
found = true
form.setFieldValue('city', resp.address[field])
break
}
}
if (!found) {
form.setFieldValue('city', 'endroit perdu')
}
form.setFieldValue('country', resp.address.country)
})