vikunja-frontend/src/components/tasks/partials/editAssignees.vue

125 lines
2.9 KiB
Vue
Raw Normal View History

2019-11-24 14:16:24 +01:00
<template>
<multiselect
:loading="listUserService.loading"
placeholder="Type to assign a user..."
:disabled="disabled"
:multiple="true"
@search="findUser"
:search-results="foundUsers"
@select="addAssignee"
label="username"
select-placeholder="Assign this user"
v-model="assignees"
>
<template v-slot:tag="props">
<span class="assignee">
<user :avatar-size="32" :show-username="false" :user="props.item"/>
<a @click="removeAssignee(props.item)" class="remove-assignee" v-if="!disabled">
<icon icon="times"/>
</a>
</span>
2019-11-24 14:16:24 +01:00
</template>
</multiselect>
</template>
<script>
import differenceWith from 'lodash/differenceWith'
2019-11-24 14:16:24 +01:00
import UserModel from '../../../models/user'
import ListUserService from '../../../services/listUsers'
import TaskAssigneeService from '../../../services/taskAssignee'
import User from '../../misc/user'
import Multiselect from '@/components/input/multiselect'
2019-11-24 14:16:24 +01:00
export default {
name: 'editAssignees',
components: {
User,
Multiselect,
},
props: {
taskId: {
type: Number,
required: true,
2019-11-24 14:16:24 +01:00
},
listId: {
type: Number,
required: true,
2019-11-24 14:16:24 +01:00
},
disabled: {
default: false,
2019-11-24 14:16:24 +01:00
},
value: {
type: Array,
},
},
data() {
return {
newAssignee: UserModel,
listUserService: ListUserService,
foundUsers: [],
assignees: [],
taskAssigneeService: TaskAssigneeService,
}
},
created() {
this.assignees = this.value
this.listUserService = new ListUserService()
this.newAssignee = new UserModel()
this.taskAssigneeService = new TaskAssigneeService()
},
watch: {
value(newVal) {
this.assignees = newVal
},
},
methods: {
addAssignee(user) {
this.$store.dispatch('tasks/addAssignee', {user: user, taskId: this.taskId})
.then(() => {
this.$emit('input', this.assignees)
this.success({message: 'The user has been assigned successfully.'}, this)
})
.catch(e => {
this.error(e, this)
})
2019-11-24 14:16:24 +01:00
},
removeAssignee(user) {
this.$store.dispatch('tasks/removeAssignee', {user: user, taskId: this.taskId})
.then(() => {
// Remove the assignee from the list
for (const a in this.assignees) {
if (this.assignees[a].id === user.id) {
this.assignees.splice(a, 1)
2019-11-24 14:16:24 +01:00
}
}
this.success({message: 'The user has been unassinged successfully.'}, this)
})
.catch(e => {
this.error(e, this)
})
},
findUser(query) {
if (query === '') {
this.clearAllFoundUsers()
return
}
2019-11-24 14:16:24 +01:00
this.listUserService.getAll({listId: this.listId}, {s: query})
.then(response => {
// Filter the results to not include users who are already assigned
this.$set(this, 'foundUsers', differenceWith(response, this.assignees, (first, second) => {
return first.id === second.id
}))
})
.catch(e => {
this.error(e, this)
})
},
clearAllFoundUsers() {
this.$set(this, 'foundUsers', [])
2019-11-24 14:16:24 +01:00
},
},
}
2019-11-24 14:16:24 +01:00
</script>