pleroma-fe/src/components/chat_new/chat_new.js

80 lines
1.9 KiB
JavaScript
Raw Normal View History

2026-01-06 16:23:17 +02:00
import { mapGetters, mapState } from 'vuex'
2026-01-08 17:26:52 +02:00
2020-05-07 16:10:53 +03:00
import BasicUserCard from '../basic_user_card/basic_user_card.vue'
import UserAvatar from '../user_avatar/user_avatar.vue'
2026-01-08 17:26:52 +02:00
import { library } from '@fortawesome/fontawesome-svg-core'
import { faChevronLeft, faSearch } from '@fortawesome/free-solid-svg-icons'
2026-01-06 16:22:52 +02:00
library.add(faSearch, faChevronLeft)
2020-05-07 16:10:53 +03:00
const chatNew = {
components: {
BasicUserCard,
2026-01-06 16:22:52 +02:00
UserAvatar,
2020-05-07 16:10:53 +03:00
},
2026-01-06 16:22:52 +02:00
data() {
2020-05-07 16:10:53 +03:00
return {
suggestions: [],
userIds: [],
loading: false,
2026-01-06 16:22:52 +02:00
query: '',
2020-05-07 16:10:53 +03:00
}
},
2026-01-06 16:22:52 +02:00
async created() {
2020-05-07 16:10:53 +03:00
const { chats } = await this.backendInteractor.chats()
2026-01-06 16:22:52 +02:00
chats.forEach((chat) => this.suggestions.push(chat.account))
2020-05-07 16:10:53 +03:00
},
computed: {
2026-01-06 16:22:52 +02:00
users() {
return this.userIds.map((userId) => this.findUser(userId))
2020-05-07 16:10:53 +03:00
},
2026-01-06 16:22:52 +02:00
availableUsers() {
2020-05-07 16:10:53 +03:00
if (this.query.length !== 0) {
return this.users
} else {
return this.suggestions
}
},
...mapState({
2026-01-06 16:22:52 +02:00
currentUser: (state) => state.users.currentUser,
backendInteractor: (state) => state.api.backendInteractor,
2020-05-07 16:10:53 +03:00
}),
2026-01-06 16:22:52 +02:00
...mapGetters(['findUser']),
2020-05-07 16:10:53 +03:00
},
methods: {
2026-01-06 16:22:52 +02:00
goBack() {
2020-05-07 16:10:53 +03:00
this.$emit('cancel')
},
2026-01-06 16:22:52 +02:00
goToChat(user) {
2020-05-07 16:10:53 +03:00
this.$router.push({ name: 'chat', params: { recipient_id: user.id } })
},
2026-01-06 16:22:52 +02:00
onInput() {
2020-05-07 16:10:53 +03:00
this.search(this.query)
},
2026-01-06 16:22:52 +02:00
addUser(user) {
2020-05-07 16:10:53 +03:00
this.selectedUserIds.push(user.id)
this.query = ''
},
2026-01-06 16:22:52 +02:00
removeUser(userId) {
this.selectedUserIds = this.selectedUserIds.filter((id) => id !== userId)
2020-05-07 16:10:53 +03:00
},
2026-01-06 16:22:52 +02:00
search(query) {
2020-05-07 16:10:53 +03:00
if (!query) {
this.loading = false
return
}
this.loading = true
this.userIds = []
2026-01-06 16:22:52 +02:00
this.$store
.dispatch('search', { q: query, resolve: true, type: 'accounts' })
.then((data) => {
2020-05-07 16:10:53 +03:00
this.loading = false
2026-01-06 16:22:52 +02:00
this.userIds = data.accounts.map((a) => a.id)
2020-05-07 16:10:53 +03:00
})
2026-01-06 16:22:52 +02:00
},
},
2020-05-07 16:10:53 +03:00
}
export default chatNew