-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Expand file tree
/
Copy pathUserFormManager.vue
More file actions
109 lines (96 loc) · 2.67 KB
/
Copy pathUserFormManager.vue
File metadata and controls
109 lines (96 loc) · 2.67 KB
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
<!--
- SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
- SPDX-License-Identifier: AGPL-3.0-or-later
-->
<template>
<div class="user-form__item user-form__managers">
<NcSelectUsers
:modelValue="managerModel"
class="user-form__select"
:input-label="t('settings', 'Manager')"
:placeholder="t('settings', 'Search for a manager…')"
:options="managerOptions"
:loading="loading"
@update:modelValue="onManagerChange"
@search="searchUserManager" />
</div>
</template>
<script>
import NcSelectUsers from '@nextcloud/vue/components/NcSelectUsers'
import logger from '../../logger.ts'
export default {
name: 'UserFormManager',
components: {
NcSelectUsers,
},
inject: ['formData'],
data() {
return {
possibleManagers: [],
loading: false,
searchTimeout: null,
}
},
computed: {
/**
* Map internal formData.manager to NcSelectUsersModel shape.
* Cached to keep object identity stable across reads, so NcSelectUsers
* doesn't see a fresh :modelValue on every parent re-render.
*/
managerModel() {
const m = this.formData.manager
if (!m) {
return null
}
const id = typeof m === 'object' ? m.id : m
const displayName = typeof m === 'object' ? (m.displayname ?? m.id) : m
if (this._managerModelCache?.id === id && this._managerModelCache?.displayName === displayName) {
return this._managerModelCache
}
// eslint-disable-next-line vue/no-side-effects-in-computed-properties
this._managerModelCache = { id, displayName }
return this._managerModelCache
},
/** Map API users to NcSelectUsersModel shape */
managerOptions() {
return this.possibleManagers.map((u) => ({
id: u.id,
displayName: u.displayname ?? u.id,
subname: u.email ?? '',
}))
},
},
beforeUnmount() {
clearTimeout(this.searchTimeout)
},
methods: {
/** Map NcSelectUsersModel back to internal formData shape */
onManagerChange(value) {
this.formData.manager = value
? { id: value.id, displayname: value.displayName }
: ''
},
/** Debounce keystrokes so a 10-char query produces 1-2 requests, not 10. */
searchUserManager(query) {
clearTimeout(this.searchTimeout)
this.searchTimeout = setTimeout(() => this.fetchManagers(query), 200)
},
async fetchManagers(query) {
this.loading = true
try {
const response = await this.$store.dispatch('searchUsers', {
offset: 0,
limit: 10,
search: query,
})
const users = response?.data ? Object.values(response.data.ocs.data.users) : []
this.possibleManagers = users
} catch (error) {
logger.error('Failed to search user managers', { error })
} finally {
this.loading = false
}
},
},
}
</script>