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
|
<script>
import { GlAlert } from '@gitlab/ui';
import * as Sentry from '@sentry/browser';
import Api from '~/api';
import SafeHtml from '~/vue_shared/directives/safe_html';
import {
PROJECT_TOGGLE_TEXT,
PROJECT_HEADER_TEXT,
FETCH_PROJECTS_ERROR,
FETCH_PROJECT_ERROR,
} from './constants';
import EntitySelector from './entity_select.vue';
export default {
components: {
GlAlert,
EntitySelector,
},
directives: {
SafeHtml,
},
props: {
label: {
type: String,
required: true,
},
hasHtmlLabel: {
type: Boolean,
required: false,
default: false,
},
inputName: {
type: String,
required: true,
},
inputId: {
type: String,
required: true,
},
groupId: {
type: String,
required: false,
default: null,
},
userId: {
type: String,
required: false,
default: null,
},
includeSubgroups: {
type: Boolean,
required: false,
default: false,
},
membership: {
type: Boolean,
required: false,
default: false,
},
orderBy: {
type: String,
required: false,
default: 'similarity',
},
initialSelection: {
type: String,
required: false,
default: null,
},
},
data() {
return {
errorMessage: '',
};
},
methods: {
async fetchProjects(searchString = '') {
let projects = [];
try {
const { data = [] } = await (() => {
const commonParams = {
order_by: this.orderBy,
simple: true,
};
if (this.groupId) {
return Api.groupProjects(this.groupId, searchString, {
...commonParams,
with_shared: true,
include_subgroups: this.includeSubgroups,
simple: true,
});
}
// Note: the whole userId handling supports a single project selector that is slated for
// removal. Once we have deleted app/views/clusters/clusters/_advanced_settings.html.haml,
// we should be able to clean this up.
if (this.userId) {
return Api.userProjects(
this.userId,
searchString,
{
with_shared: true,
include_subgroups: this.includeSubgroups,
},
(res) => ({ data: res }),
);
}
return Api.projects(searchString, {
...commonParams,
membership: this.membership,
});
})();
projects = data.map((item) => ({
text: item.name_with_namespace || item.name,
value: String(item.id),
}));
} catch (error) {
this.handleError({ message: FETCH_PROJECTS_ERROR, error });
}
return { items: projects, totalPages: 1 };
},
async fetchProjectName(projectId) {
let projectName = '';
try {
const { data: project } = await Api.project(projectId);
projectName = project.name_with_namespace;
} catch (error) {
this.handleError({ message: FETCH_PROJECT_ERROR, error });
}
return projectName;
},
handleError({ message, error }) {
Sentry.captureException(error);
this.errorMessage = message;
},
dismissError() {
this.errorMessage = '';
},
},
i18n: {
searchForProject: PROJECT_TOGGLE_TEXT,
selectProject: PROJECT_HEADER_TEXT,
},
};
</script>
<template>
<entity-selector
:label="label"
:input-name="inputName"
:input-id="inputId"
:initial-selection="initialSelection"
:header-text="$options.i18n.selectProject"
:default-toggle-text="$options.i18n.searchForProject"
:fetch-items="fetchProjects"
:fetch-initial-selection-text="fetchProjectName"
clearable
>
<template v-if="hasHtmlLabel" #label>
<span v-safe-html="label"></span>
</template>
<template #error>
<gl-alert v-if="errorMessage" class="gl-mb-3" variant="danger" @dismiss="dismissError">{{
errorMessage
}}</gl-alert>
</template>
</entity-selector>
</template>
|