refactoring where selection adding an event

This commit is contained in:
lesion
2022-06-01 14:07:55 +02:00
parent bcea3b119c
commit a7704526f9

View File

@@ -5,13 +5,13 @@ v-row
:rules="[$validators.required('common.where')]" :rules="[$validators.required('common.where')]"
:label="$t('common.where')" :label="$t('common.where')"
:hint="$t('event.where_description')" :hint="$t('event.where_description')"
:search-input.sync="placeName"
:prepend-icon='mdiMapMarker' :prepend-icon='mdiMapMarker'
persistent-hint
:value="value.name"
:items="filteredPlaces"
no-filter no-filter
item-text='name' :value='value.name'
hide-no-data
@input.native='search'
persistent-hint
:items="places"
@change='selectPlace') @change='selectPlace')
template(v-slot:item="{ item, attrs, on }") template(v-slot:item="{ item, attrs, on }")
v-list-item(v-bind='attrs' v-on='on') v-list-item(v-bind='attrs' v-on='on')
@@ -32,24 +32,24 @@ v-row
</template> </template>
<script> <script>
import { mapState } from 'vuex'
import { mdiMap, mdiMapMarker, mdiPlus } from '@mdi/js' import { mdiMap, mdiMapMarker, mdiPlus } from '@mdi/js'
import debounce from 'lodash/debounce'
export default { export default {
name: 'WhereInput', name: 'WhereInput',
props: { props: {
value: { type: Object, default: () => {} } value: { type: Object, default: () => ({}) }
}, },
data () { data () {
return { return {
mdiMap, mdiMapMarker, mdiPlus, mdiMap, mdiMapMarker, mdiPlus,
place: { }, place: { },
placeName: '', placeName: '',
places: [],
disableAddress: true disableAddress: true
} }
}, },
computed: { computed: {
...mapState(['places']),
filteredPlaces () { filteredPlaces () {
if (!this.placeName) { return this.places } if (!this.placeName) { return this.places }
const placeName = this.placeName.trim().toLowerCase() const placeName = this.placeName.trim().toLowerCase()
@@ -70,6 +70,15 @@ export default {
} }
}, },
methods: { methods: {
search: debounce(async function(ev) {
const search = ev.target.value.trim().toLowerCase()
this.places = await this.$axios.$get(`place?search=${search}`)
if (!search) { return this.places }
const matches = this.places.find(p => search === p.name.toLocaleLowerCase())
if (!matches) {
this.places.unshift({ create: true, name: ev.target.value.trim() })
}
}, 100),
selectPlace (p) { selectPlace (p) {
if (!p) { return } if (!p) { return }
if (typeof p === 'object' && !p.create) { if (typeof p === 'object' && !p.create) {
@@ -79,9 +88,9 @@ export default {
this.disableAddress = true this.disableAddress = true
} else { // this is a new place } else { // this is a new place
this.place.name = p.name || p this.place.name = p.name || p
const tmpPlace = this.place.name.trim().toLowerCase() const tmpPlace = this.place.name.trim().toLocaleLowerCase()
// search for a place with the same name // search for a place with the same name
const place = this.places.find(p => p.name.toLowerCase() === tmpPlace) const place = this.places.find(p => !p.create && p.name.trim().toLocaleLowerCase() === tmpPlace)
if (place) { if (place) {
this.place.name = place.name this.place.name = place.name
this.place.id = place.id this.place.id = place.id