Fetch groups on the fly

Signed-off-by: Julius Härtl <jus@bitgrid.net>
This commit is contained in:
Julius Härtl 2019-10-11 16:52:29 +02:00 committed by Arthur Schiwon
parent 06d43bdd77
commit 2adcc75413
No known key found for this signature in database
GPG Key ID: 7424F1874854DF23
1 changed files with 59 additions and 29 deletions

View File

@ -22,61 +22,91 @@
<template> <template>
<div> <div>
<Multiselect v-model="newValue" <Multiselect :value="currentValue"
:class="{'icon-loading-small': groups.length === 0}" :loading="status.isLoading && groups.length === 0"
:options="groups" :options="groups"
:multiple="false" :multiple="false"
label="displayname" label="displayname"
track-by="id" track-by="id"
@input="setValue" /> @search-change="searchAsync"
@input="(value) => $emit('input', value.id)" />
</div> </div>
</template> </template>
<script> <script>
import { Multiselect } from 'nextcloud-vue/dist/Components/Multiselect' import { Multiselect } from 'nextcloud-vue/dist/Components/Multiselect'
import valueMixin from '../../mixins/valueMixin'
import axios from '@nextcloud/axios' import axios from '@nextcloud/axios'
const groups = []
const status = {
isLoading: false
}
export default { export default {
name: 'RequestUserGroup', name: 'RequestUserGroup',
components: { components: {
Multiselect Multiselect
}, },
mixins: [ props: {
valueMixin value: {
], type: String,
data() { default: ''
return { },
groups: [] check: {
type: Object,
default: () => { return {} }
} }
}, },
beforeMount() { data() {
axios.get(OC.linkToOCS('cloud', 2) + 'groups').then((response) => { return {
this.groups = response.data.ocs.data.groups.reduce((obj, item) => { groups: groups,
obj.push({ status: status
id: item, }
displayname: item },
}) computed: {
return obj currentValue() {
}, []) return this.groups.find(group => group.id === this.value) || null
this.updateInternalValue(this.value) }
}, (error) => { },
console.error('Error while loading group list', error.response) async mounted() {
}) if (this.groups.length === 0) {
await this.searchAsync('')
}
if (this.currentValue === null) {
await this.searchAsync(this.value)
}
}, },
methods: { methods: {
updateInternalValue() { searchAsync(searchQuery) {
this.newValue = this.groups.find(group => group.id === this.value) || null if (this.status.isLoading) {
return
}
this.status.isLoading = true
return axios.get(OC.linkToOCS('cloud', 2) + 'groups?limit=20&search=' + encodeURI(searchQuery)).then((response) => {
response.data.ocs.data.groups.reduce((obj, item) => {
obj.push({
id: item,
displayname: item
})
return obj
}, []).forEach((group) => this.addGroup(group))
this.status.isLoading = false
}, (error) => {
console.error('Error while loading group list', error.response)
})
}, },
setValue(value) { addGroup(group) {
if (value !== null) { const index = this.groups.findIndex((item) => item.id === group.id)
this.$emit('input', this.newValue.id) if (index === -1) {
this.groups.push(group)
} }
} }
} }
} }
</script> </script>
<style scoped> <style scoped>
.multiselect, input[type='text'] { .multiselect {
width: 100%; width: 100%;
} }
</style> </style>