akkoma-fe/src/components/list_new/list_new.js

80 lines
2 KiB
JavaScript
Raw Normal View History

2022-06-15 22:52:24 +00:00
import { mapState, mapGetters } from 'vuex'
import BasicUserCard from '../basic_user_card/basic_user_card.vue'
import UserAvatar from '../user_avatar/user_avatar.vue'
2022-07-05 13:28:23 +00:00
import ListUserSearch from '../list_user_search/list_user_search.vue'
2022-06-15 22:52:24 +00:00
import { library } from '@fortawesome/fontawesome-svg-core'
import {
faSearch,
faChevronLeft
} from '@fortawesome/free-solid-svg-icons'
library.add(
faSearch,
faChevronLeft
)
const ListNew = {
components: {
BasicUserCard,
2022-07-05 13:28:23 +00:00
UserAvatar,
ListUserSearch
2022-06-15 22:52:24 +00:00
},
data () {
return {
title: '',
userIds: [],
2022-07-05 13:28:23 +00:00
selectedUserIds: []
2022-06-15 22:52:24 +00:00
}
},
computed: {
users () {
return this.userIds.map(userId => this.findUser(userId))
},
2022-06-16 06:41:07 +00:00
selectedUsers () {
return this.selectedUserIds.map(userId => this.findUser(userId))
},
2022-06-15 22:52:24 +00:00
...mapState({
currentUser: state => state.users.currentUser
2022-06-15 22:52:24 +00:00
}),
...mapGetters(['findUser'])
},
methods: {
goBack () {
this.$emit('cancel')
},
onInput () {
this.search(this.query)
},
2022-06-16 05:25:09 +00:00
selectUser (user) {
2022-06-15 22:52:24 +00:00
if (this.selectedUserIds.includes(user.id)) {
this.removeUser(user.id)
} else {
this.addUser(user)
}
},
2022-06-16 05:25:09 +00:00
isSelected (user) {
return this.selectedUserIds.includes(user.id)
},
2022-06-15 22:52:24 +00:00
addUser (user) {
this.selectedUserIds.push(user.id)
},
removeUser (userId) {
this.selectedUserIds = this.selectedUserIds.filter(id => id !== userId)
},
2022-07-05 13:28:23 +00:00
onResults (results) {
this.userIds = results
2022-06-15 22:52:24 +00:00
},
createList () {
// the API has two different endpoints for "creating a list with a name"
// and "updating the accounts on the list".
this.$store.dispatch('createList', { title: this.title })
.then((list) => {
this.$store.dispatch('setListAccounts', { id: list.id, accountIds: this.selectedUserIds })
this.$router.push({ name: 'list-timeline', params: { id: list.id } })
2022-06-15 22:52:24 +00:00
})
}
}
}
export default ListNew