summaryrefslogtreecommitdiffstats
path: root/src/components/RightSidebar/Participants/ParticipantsTab.vue
blob: 5346f2a62b710590678a38a4fe44210c6fcf8905 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<!--
  - @copyright Copyright (c) 2019 Marco Ambrosini <marcoambrosini@icloud.com>
  -
  - @author Marco Ambrosini <marcoambrosini@icloud.com>
  -
  - @license GNU AGPL version 3 or any later version
  -
  - This program is free software: you can redistribute it and/or modify
  - it under the terms of the GNU Affero General Public License as
  - published by the Free Software Foundation, either version 3 of the
  - License, or (at your option) any later version.
  -
  - This program is distributed in the hope that it will be useful,
  - but WITHOUT ANY WARRANTY; without even the implied warranty of
  - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  - GNU Affero General Public License for more details.
  -
  - You should have received a copy of the GNU Affero General Public License
  - along with this program. If not, see <http://www.gnu.org/licenses/>.
-->

<template>
	<div>
		<SearchBox v-if="canSearch"
			v-model="searchText"
			:placeholder-text="searchBoxPlaceholder"
			:is-searching="isSearching"
			@input="handleInput"
			@abort-search="abortSearch" />
		<AppNavigationCaption v-if="isSearching && canAdd"
			:title="t('spreed', 'Participants')" />
		<CurrentParticipants :search-text="searchText"
			:participants-initialised="participantsInitialised" />
		<ParticipantsSearchResults v-if="canAdd && isSearching"
			:search-results="searchResults"
			:contacts-loading="contactsLoading"
			:no-results="noResults"
			:search-text="searchText"
			@click="addParticipants" />
	</div>
</template>

<script>
import CurrentParticipants from './CurrentParticipants/CurrentParticipants.vue'
import SearchBox from '../../LeftSidebar/SearchBox/SearchBox.vue'
import debounce from 'debounce'
import { EventBus } from '../../../services/EventBus.js'
import { searchPossibleConversations } from '../../../services/conversationsService.js'
import { addParticipant } from '../../../services/participantsService.js'
import { loadState } from '@nextcloud/initial-state'
import CancelableRequest from '../../../utils/cancelableRequest.js'
import { showError } from '@nextcloud/dialogs'
import AppNavigationCaption from '@nextcloud/vue/dist/Components/AppNavigationCaption'
import ParticipantsSearchResults from './ParticipantsSearchResults/ParticipantsSearchResults.vue'
import getParticipants from '../../../mixins/getParticipants.js'

export default {
	name: 'ParticipantsTab',
	components: {
		AppNavigationCaption,
		CurrentParticipants,
		SearchBox,
		ParticipantsSearchResults,
	},

	mixins: [getParticipants],

	props: {
		canSearch: {
			type: Boolean,
			required: true,
		},
		canAdd: {
			type: Boolean,
			required: true,
		},
	},

	data() {
		return {
			searchText: '',
			searchResults: [],
			contactsLoading: false,
			isCirclesEnabled: loadState('spreed', 'circles_enabled'),
			cancelSearchPossibleConversations: () => {},
		}
	},

	computed: {
		searchBoxPlaceholder() {
			return this.canAdd
				? t('spreed', 'Search or add participants')
				: t('spreed', 'Search participants')
		},
		show() {
			return this.$store.getters.getSidebarStatus
		},
		opened() {
			return !!this.token && this.show
		},
		token() {
			return this.$store.getters.getToken()
		},
		conversation() {
			return this.$store.getters.conversation(this.token) || this.$store.getters.dummyConversation
		},
		isSearching() {
			return this.searchText !== ''
		},
		noResults() {
			return this.searchResults === []
		},
	},

	beforeMount() {
		EventBus.$on('route-change', this.abortSearch)

		// Initialises the get participants mixin
		this.initialiseGetParticipantsMixin()
	},

	beforeDestroy() {
		EventBus.$off('route-change', this.abortSearch)

		this.cancelSearchPossibleConversations()
		this.cancelSearchPossibleConversations = null

		this.stopGetParticipantsMixin()
	},

	methods: {
		handleClose() {
			this.$store.dispatch('hideSidebar')
		},

		handleInput() {
			this.contactsLoading = true
			this.searchResults = []
			this.debounceFetchSearchResults()
		},

		debounceFetchSearchResults: debounce(function() {
			if (this.isSearching) {
				this.fetchSearchResults()
			}
		}, 250),

		async fetchSearchResults() {
			try {
				this.cancelSearchPossibleConversations('canceled')
				const { request, cancel } = CancelableRequest(searchPossibleConversations)
				this.cancelSearchPossibleConversations = cancel

				const response = await request({
					searchText: this.searchText,
					token: this.token,
				})

				this.searchResults = response?.data?.ocs?.data || []
				this.contactsLoading = false
			} catch (exception) {
				if (CancelableRequest.isCancel(exception)) {
					return
				}
				console.error(exception)
				showError(t('spreed', 'An error occurred while performing the search'))
			}
		},

		/**
		 * Add the selected group/user/circle to the conversation
		 *
		 * @param {object} item The autocomplete suggestion to start a conversation with
		 * @param {string} item.id The ID of the target
		 * @param {string} item.source The source of the target
		 */
		async addParticipants(item) {
			try {
				await addParticipant(this.token, item.id, item.source)
				this.abortSearch()
				this.cancelableGetParticipants()
			} catch (exception) {
				console.debug(exception)
				showError(t('spreed', 'An error occurred while adding the participants'))
			}
		},

		// Ends the search operation
		abortSearch() {
			this.searchText = ''
			if (this.cancelSearchPossibleConversations) {
				this.cancelSearchPossibleConversations()
			}
		},
	},
}
</script>

<style scoped>

/** TODO: fix these in the nextcloud-vue library **/

::v-deep .app-sidebar-header__menu {
	top: 6px !important;
	margin-top: 0 !important;
	right: 54px !important;
}

::v-deep .app-sidebar__close {
	top: 6px !important;
	right: 6px !important;
}

/*
 * The field will fully overlap the top of the sidebar content so
 * that elements will scroll behind it
 */
.app-navigation-search {
	top: -10px;
	margin: -10px;
	padding: 10px;
}

</style>