Clear extra tags in some case, automagically match input

This commit is contained in:
Robin van der Linde 2024-04-29 01:36:27 +02:00
parent 8234616168
commit 117bf77dbd
Signed by untrusted user who does not match committer: Robin-van-der-Linde
GPG key ID: 53956B3252478F0D
3 changed files with 56 additions and 5 deletions

View file

@ -194,6 +194,26 @@
} }
] ]
}, },
{
"id": "post_offic_brand",
"condition": "amenity=post_office",
"question": {
"en": "To which brand does this post office belong?"
},
"render": {
"en": "This is a {brand} post office"
},
"freeform": {
"type": "nsi",
"key": "brand",
"placeholder": {
"en": "Brand of the post office"
},
"helperArgs": [
"amenity=post_office"
]
}
},
{ {
"id": "partner-brand", "id": "partner-brand",
"render": { "render": {
@ -639,4 +659,4 @@
] ]
} }
] ]
} }

View file

@ -399,12 +399,14 @@
"key": "brand", "key": "brand",
"type": "nsi", "type": "nsi",
"helperArgs": [ "helperArgs": [
"amenity=public_bookcase", "amenity=public_bookcase"
"nobrand="
], ],
"placeholder": { "placeholder": {
"en": "Name of the network" "en": "Name of the network"
} },
"addExtraTags": [
"nobrand="
]
}, },
"mappings": [ "mappings": [
{ {

View file

@ -23,7 +23,10 @@
let maintag = helperArgs[0].toString() let maintag = helperArgs[0].toString()
let tag = key let tag = key
let addExtraTags = helperArgs[1].split(";") let addExtraTags: string[] = []
if (helperArgs[1]) {
addExtraTags = helperArgs[1].split(";")
}
const path = `${tag}s/${maintag.split("=")[0]}/${maintag.split("=")[1]}` const path = `${tag}s/${maintag.split("=")[0]}/${maintag.split("=")[1]}`
@ -141,6 +144,32 @@
// Finally, set the extra tags // Finally, set the extra tags
extraTags.setData(tags) extraTags.setData(tags)
} }
value.addCallback((value) => {
// If the value changes by the user typing we might need to update the selected item or make sure we clear any old keys
// First, check if the value is already selected, in that case we don't need to do anything
if (selectedItem && selectedItem.tags[tag] === value) {
return
}
// If the value is not selected, we check if there is an item with the same value and select it
const item = items.find((item) => item.tags[tag] === value)
if (item) {
select(item)
} else {
// If there is no item with the value, we need to clear the extra tags based on the last selected item by looping over the tags from the last selected item
if (selectedItem) {
const tags = Object.entries(selectedItem.tags).reduce((acc, [key, value]) => {
if (key !== tag && key !== maintag.split("=")[0]) {
acc[key] = ""
}
return acc
}, {} as Record<string, string>)
extraTags.setData(tags)
}
}
})
</script> </script>
<div> <div>