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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
|
<script>
import { GlTokenSelector, GlAvatar, GlAvatarLabeled, GlIcon, GlSprintf } from '@gitlab/ui';
import { debounce, isEmpty } from 'lodash';
import { __ } from '~/locale';
import { getUsers } from '~/rest_api';
import { memberName } from '../utils/member_utils';
import {
SEARCH_DELAY,
USERS_FILTER_ALL,
USERS_FILTER_SAML_PROVIDER_ID,
VALID_TOKEN_BACKGROUND,
INVALID_TOKEN_BACKGROUND,
} from '../constants';
export default {
components: {
GlTokenSelector,
GlAvatar,
GlAvatarLabeled,
GlIcon,
GlSprintf,
},
props: {
canUseEmailToken: {
type: Boolean,
required: false,
default: true,
},
placeholder: {
type: String,
required: false,
default: '',
},
ariaLabelledby: {
type: String,
required: true,
},
exceptionState: {
type: Boolean,
required: false,
default: false,
},
usersFilter: {
type: String,
required: false,
default: USERS_FILTER_ALL,
},
filterId: {
type: Number,
required: false,
default: null,
},
invalidMembers: {
type: Object,
required: true,
},
inputId: {
type: String,
required: false,
default: '',
},
},
data() {
return {
loading: false,
query: '',
originalInput: '',
users: [],
selectedTokens: [],
hasBeenFocused: false,
hideDropdownWithNoItems: true,
};
},
computed: {
emailIsValid() {
if (!this.canUseEmailToken) {
return false;
}
const regex = /^\S+@\S+$/;
return this.originalInput.match(regex) !== null;
},
placeholderText() {
if (this.selectedTokens.length === 0) {
return this.placeholder;
}
return '';
},
queryOptions() {
if (this.usersFilter === USERS_FILTER_SAML_PROVIDER_ID) {
return {
saml_provider_id: this.filterId,
...this.$options.defaultQueryOptions,
};
}
return this.$options.defaultQueryOptions;
},
hasInvalidMembers() {
return !isEmpty(this.invalidMembers);
},
textInputAttrs() {
return {
'data-testid': 'members-token-select-input',
'data-qa-selector': 'members_token_select_input',
id: this.inputId,
};
},
},
watch: {
// We might not really want this to be *reactive* since we want the "class" state to be
// tied to the specific `selectedToken` such that if the token is removed and re-added, this
// state is reset.
// See https://gitlab.com/gitlab-org/gitlab/-/merge_requests/90076#note_1027165312
hasInvalidMembers: {
handler(updatedInvalidMembers) {
// Only update tokens if we receive invalid members
if (!updatedInvalidMembers) {
return;
}
this.updateTokenClasses();
},
},
},
methods: {
handleTextInput(inputQuery) {
this.hideDropdownWithNoItems = false;
this.originalInput = inputQuery;
this.query = inputQuery.trim();
this.loading = true;
this.retrieveUsers();
},
updateTokenClasses() {
this.selectedTokens = this.selectedTokens.map((token) => ({
...token,
class: this.tokenClass(token),
}));
},
retrieveUsers: debounce(function debouncedRetrieveUsers() {
return getUsers(this.query, this.queryOptions)
.then((response) => {
this.users = response.data.map((token) => ({
id: token.id,
name: token.name,
username: token.username,
avatar_url: token.avatar_url,
}));
})
.finally(() => {
this.loading = false;
});
}, SEARCH_DELAY),
tokenClass(token) {
if (this.hasError(token)) {
return INVALID_TOKEN_BACKGROUND;
}
// assume success for this token
return VALID_TOKEN_BACKGROUND;
},
handleInput() {
this.$emit('input', this.selectedTokens);
},
handleBlur() {
this.hideDropdownWithNoItems = false;
},
handleFocus() {
// The modal auto-focuses on the input when opened.
// This prevents the dropdown from opening when the modal opens.
if (this.hasBeenFocused) {
this.loading = true;
this.retrieveUsers();
}
this.hasBeenFocused = true;
},
handleTokenRemove(value) {
if (this.selectedTokens.length) {
this.$emit('token-remove', value);
return;
}
this.$emit('clear');
},
hasError(token) {
return Object.keys(this.invalidMembers).includes(memberName(token));
},
},
defaultQueryOptions: { without_project_bots: true, active: true },
i18n: {
inviteTextMessage: __('Invite "%{email}" by email'),
},
};
</script>
<template>
<gl-token-selector
v-model="selectedTokens"
:state="exceptionState"
:dropdown-items="users"
:loading="loading"
:allow-user-defined-tokens="emailIsValid"
:hide-dropdown-with-no-items="hideDropdownWithNoItems"
:placeholder="placeholderText"
:aria-labelledby="ariaLabelledby"
:text-input-attrs="textInputAttrs"
@blur="handleBlur"
@text-input="handleTextInput"
@input="handleInput"
@focus="handleFocus"
@token-remove="handleTokenRemove"
>
<template #token-content="{ token }">
<gl-icon
v-if="hasError(token)"
name="error"
:size="16"
class="gl-mr-2"
:data-testid="`error-icon-${token.id}`"
/>
<gl-avatar
v-else-if="token.avatar_url"
:src="token.avatar_url"
:size="16"
data-testid="token-avatar"
/>
{{ token.name }}
</template>
<template #dropdown-item-content="{ dropdownItem }">
<gl-avatar-labeled
:src="dropdownItem.avatar_url"
:size="32"
:label="dropdownItem.name"
:sub-label="dropdownItem.username"
/>
</template>
<template #user-defined-token-content="{ inputText: email }">
<gl-sprintf :message="$options.i18n.inviteTextMessage">
<template #email>
<span>{{ email }}</span>
</template>
</gl-sprintf>
</template>
</gl-token-selector>
</template>
|