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
|
<script>
import { GlFilteredSearchSuggestion } from '@gitlab/ui';
import { ITEM_TYPE } from '~/groups/constants';
import { TYPENAME_CRM_ORGANIZATION } from '~/graphql_shared/constants';
import { getIdFromGraphQLId, convertToGraphQLId } from '~/graphql_shared/utils';
import { createAlert } from '~/flash';
import { isPositiveInteger } from '~/lib/utils/number_utils';
import { __ } from '~/locale';
import searchCrmOrganizationsQuery from '../queries/search_crm_organizations.query.graphql';
import { OPTIONS_NONE_ANY } from '../constants';
import BaseToken from './base_token.vue';
export default {
components: {
BaseToken,
GlFilteredSearchSuggestion,
},
props: {
config: {
type: Object,
required: true,
},
value: {
type: Object,
required: true,
},
active: {
type: Boolean,
required: true,
},
},
data() {
return {
organizations: this.config.initialOrganizations || [],
loading: false,
};
},
computed: {
defaultOrganizations() {
return this.config.defaultOrganizations || OPTIONS_NONE_ANY;
},
namespace() {
return this.config.isProject ? ITEM_TYPE.PROJECT : ITEM_TYPE.GROUP;
},
},
methods: {
getActiveOrganization(organizations, data) {
return organizations.find((organization) => {
return `${this.formatOrganizationId(organization)}` === data;
});
},
fetchOrganizations(searchTerm) {
let searchString = null;
let searchId = null;
if (isPositiveInteger(searchTerm)) {
searchId = this.formatOrganizationGraphQLId(searchTerm);
} else {
searchString = searchTerm;
}
this.loading = true;
this.$apollo
.query({
query: searchCrmOrganizationsQuery,
variables: {
fullPath: this.config.fullPath,
searchString,
searchIds: searchId ? [searchId] : null,
isProject: this.config.isProject,
},
})
.then(({ data }) => {
this.organizations = this.config.isProject
? data[this.namespace]?.group.organizations.nodes
: data[this.namespace]?.organizations.nodes;
})
.catch(() =>
createAlert({
message: __('There was a problem fetching CRM organizations.'),
}),
)
.finally(() => {
this.loading = false;
});
},
formatOrganizationId(organization) {
return `${getIdFromGraphQLId(organization.id)}`;
},
formatOrganizationGraphQLId(id) {
return convertToGraphQLId(TYPENAME_CRM_ORGANIZATION, id);
},
},
};
</script>
<template>
<base-token
:config="config"
:value="value"
:active="active"
:suggestions-loading="loading"
:suggestions="organizations"
:get-active-token-value="getActiveOrganization"
:default-suggestions="defaultOrganizations"
v-bind="$attrs"
@fetch-suggestions="fetchOrganizations"
v-on="$listeners"
>
<template #view="{ viewTokenProps: { inputValue, activeTokenValue } }">
{{ activeTokenValue ? activeTokenValue.name : inputValue }}
</template>
<template #suggestions-list="{ suggestions }">
<gl-filtered-search-suggestion
v-for="organization in suggestions"
:key="formatOrganizationId(organization)"
:value="formatOrganizationId(organization)"
>
{{ organization.name }}
</gl-filtered-search-suggestion>
</template>
</base-token>
</template>
|