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
|
<script>
import { GlDropdown, GlDropdownItem, GlDropdownDivider, GlSearchBoxByType } from '@gitlab/ui';
import { createAlert } from '~/flash';
import { __, sprintf } from '~/locale';
import branchesQuery from '../queries/branches.query.graphql';
export const i18n = {
fetchBranchesError: __('An error occurred while fetching branches.'),
noMatch: __('No matching results'),
};
export default {
i18n,
name: 'BranchDropdown',
components: {
GlDropdown,
GlDropdownItem,
GlDropdownDivider,
GlSearchBoxByType,
},
apollo: {
branchNames: {
query: branchesQuery,
variables() {
return {
projectPath: this.projectPath,
searchPattern: `*${this.searchTerm}*`,
};
},
update({ project: { repository = {} } } = {}) {
return repository.branchNames || [];
},
error(e) {
createAlert({
message: this.$options.i18n.fetchBranchesError,
captureError: true,
error: e,
});
},
},
},
props: {
projectPath: {
type: String,
required: true,
},
value: {
type: String,
required: false,
default: null,
},
},
data() {
return {
searchTerm: '',
branchNames: [],
};
},
computed: {
createButtonLabel() {
return sprintf(__('Create wildcard: %{searchTerm}'), { searchTerm: this.searchTerm });
},
shouldRenderCreateButton() {
return this.searchTerm && !this.branchNames.includes(this.searchTerm);
},
isLoading() {
return this.$apollo.queries.branchNames.loading;
},
},
methods: {
selectBranch(selected) {
this.$emit('input', selected);
},
createWildcard() {
this.$emit('createWildcard', this.searchTerm);
},
isSelected(branch) {
return this.value === branch;
},
},
};
</script>
<template>
<gl-dropdown :text="value || branchNames[0]">
<gl-search-box-by-type
v-model.trim="searchTerm"
data-testid="branch-search"
debounce="250"
:is-loading="isLoading"
/>
<gl-dropdown-item
v-for="branch in branchNames"
:key="branch"
:is-checked="isSelected(branch)"
is-check-item
@click="selectBranch(branch)"
>
{{ branch }}
</gl-dropdown-item>
<gl-dropdown-item v-if="!branchNames.length && !isLoading" data-testid="no-data">{{
$options.i18n.noMatch
}}</gl-dropdown-item>
<template v-if="shouldRenderCreateButton">
<gl-dropdown-divider />
<gl-dropdown-item data-testid="create-wildcard-button" @click="createWildcard">
{{ createButtonLabel }}
</gl-dropdown-item>
</template>
</gl-dropdown>
</template>
|