yandere_fe/src/components/lists_edit/lists_edit.js

146 lines
4.2 KiB
JavaScript
Raw Normal View History

2022-08-06 07:26:43 -07:00
import { mapState, mapGetters } from 'vuex'
import BasicUserCard from '../basic_user_card/basic_user_card.vue'
import ListsUserSearch from '../lists_user_search/lists_user_search.vue'
2022-08-16 14:48:10 -07:00
import PanelLoading from 'src/components/panel_loading/panel_loading.vue'
2022-08-06 07:26:43 -07:00
import UserAvatar from '../user_avatar/user_avatar.vue'
2022-08-16 14:48:10 -07:00
import TabSwitcher from 'src/components/tab_switcher/tab_switcher.jsx'
2022-08-06 07:26:43 -07:00
import { library } from '@fortawesome/fontawesome-svg-core'
import {
faSearch,
faChevronLeft
} from '@fortawesome/free-solid-svg-icons'
library.add(
faSearch,
faChevronLeft
)
const ListsNew = {
components: {
BasicUserCard,
UserAvatar,
2022-08-16 14:48:10 -07:00
ListsUserSearch,
TabSwitcher,
PanelLoading
2022-08-06 07:26:43 -07:00
},
data () {
return {
title: '',
2022-08-16 14:48:10 -07:00
titleDraft: '',
membersUserIds: [],
removedUserIds: new Set([]), // users we added for members, to undo
searchUserIds: [],
addedUserIds: new Set([]), // users we added from search, to undo
searchLoading: false,
reallyDelete: false
2022-08-06 07:26:43 -07:00
}
},
created () {
2022-08-17 10:21:10 -07:00
if (!this.id) return
this.$store.dispatch('fetchList', { listId: this.id })
2022-08-16 14:48:10 -07:00
.then(() => {
this.title = this.findListTitle(this.id)
this.titleDraft = this.title
})
this.$store.dispatch('fetchListAccounts', { listId: this.id })
2022-08-06 07:26:43 -07:00
.then(() => {
2022-08-16 14:48:10 -07:00
this.membersUserIds = this.findListAccounts(this.id)
this.membersUserIds.forEach(userId => {
2022-08-06 07:26:43 -07:00
this.$store.dispatch('fetchUserIfMissing', userId)
})
})
},
computed: {
id () {
return this.$route.params.id
},
2022-08-16 14:48:10 -07:00
membersUsers () {
return [...this.membersUserIds, ...this.addedUserIds]
.map(userId => this.findUser(userId)).filter(user => user)
2022-08-06 07:26:43 -07:00
},
2022-08-16 14:48:10 -07:00
searchUsers () {
return this.searchUserIds.map(userId => this.findUser(userId)).filter(user => user)
2022-08-06 07:26:43 -07:00
},
...mapState({
currentUser: state => state.users.currentUser
}),
...mapGetters(['findUser', 'findListTitle', 'findListAccounts'])
},
methods: {
onInput () {
this.search(this.query)
},
2022-08-16 14:48:10 -07:00
toggleRemoveMember (user) {
if (this.removedUserIds.has(user.id)) {
2022-08-17 10:21:10 -07:00
this.id && this.addUser(user)
2022-08-16 14:48:10 -07:00
this.removedUserIds.delete(user.id)
} else {
2022-08-17 10:21:10 -07:00
this.id && this.removeUser(user.id)
2022-08-16 14:48:10 -07:00
this.removedUserIds.add(user.id)
}
},
toggleAddFromSearch (user) {
if (this.addedUserIds.has(user.id)) {
2022-08-17 10:21:10 -07:00
this.id && this.removeUser(user.id)
2022-08-16 14:48:10 -07:00
this.addedUserIds.delete(user.id)
2022-08-06 07:26:43 -07:00
} else {
2022-08-17 10:21:10 -07:00
this.id && this.addUser(user)
2022-08-16 14:48:10 -07:00
this.addedUserIds.add(user.id)
2022-08-06 07:26:43 -07:00
}
},
2022-08-16 14:48:10 -07:00
isRemoved (user) {
return this.removedUserIds.has(user.id)
},
isAdded (user) {
return this.addedUserIds.has(user.id)
2022-08-06 07:26:43 -07:00
},
addUser (user) {
this.$store.dispatch('addListAccount', { accountId: this.user.id, listId: this.id })
2022-08-06 07:26:43 -07:00
},
removeUser (userId) {
this.$store.dispatch('removeListAccount', { accountId: this.user.id, listId: this.id })
2022-08-06 07:26:43 -07:00
},
2022-08-16 14:48:10 -07:00
onSearchLoading (results) {
this.searchLoading = true
2022-08-06 07:26:43 -07:00
},
2022-08-16 14:48:10 -07:00
onSearchLoadingDone (results) {
this.searchLoading = false
},
onSearchResults (results) {
this.searchLoading = false
this.searchUserIds = results
},
updateListTitle () {
this.$store.dispatch('setList', { listId: this.id, title: this.titleDraft })
.then(() => {
this.title = this.findListTitle(this.id)
})
2022-08-06 07:26:43 -07:00
},
2022-08-17 10:21:10 -07:00
createList () {
this.$store.dispatch('createList', { title: this.titleDraft })
.then((list) => {
return this
.$store
.dispatch('setListAccounts', { listId: list.id, accountIds: [...this.addedUserIds] })
.then(() => list.id)
})
.then((listId) => {
this.$router.push({ name: 'lists-timeline', params: { id: listId } })
})
.catch((e) => {
this.$store.dispatch('pushGlobalNotice', {
messageKey: 'lists.error',
messageArgs: [e.message],
level: 'error'
})
})
},
2022-08-06 07:26:43 -07:00
deleteList () {
this.$store.dispatch('deleteList', { listId: this.id })
2022-08-06 07:26:43 -07:00
this.$router.push({ name: 'lists' })
}
}
}
export default ListsNew