/
EnvironmentSelect.vue
155 lines (141 loc) · 3.41 KB
/
EnvironmentSelect.vue
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
<template>
<v-combobox
:items="items"
:label="label"
:loading="loading"
:menu-props="{ maxHeight: '400' }"
:search-input.sync="search"
@update:search-input="getFilteredData({ q: $event })"
item-text="name"
item-value="id"
clearable
v-model="environment"
>
<template v-slot:no-data>
<v-list-item>
<v-list-item-content>
<v-list-item-title>
No environments matching
<strong>"{{ search }}"</strong>
</v-list-item-title>
</v-list-item-content>
</v-list-item>
</template>
<template v-slot:selection="{ item }">
{{ item.name }}
</template>
<template v-slot:item="data">
<v-list-item-content>
<v-list-item-title>
{{ data.item.name }}
</v-list-item-title>
<v-list-item-subtitle style="width: 200px" class="text-truncate">
{{ data.item.description }}
</v-list-item-subtitle>
</v-list-item-content>
</template>
<template v-slot:append-item>
<v-list-item v-if="more" @click="loadMore()">
<v-list-item-content>
<v-list-item-subtitle> Load More </v-list-item-subtitle>
</v-list-item-content>
</v-list-item>
</template>
</v-combobox>
</template>
<script>
import { cloneDeep, debounce } from "lodash"
import SearchUtils from "@/search/utils"
import SourceEnvironmentApi from "@/data/source/environment/api"
export default {
name: "SourceEnvironmentSelect",
props: {
value: {
type: Object,
default: function () {
return {}
},
},
label: {
type: String,
default: "Environment",
},
project: {
type: Object,
default: null,
},
},
data() {
return {
loading: false,
items: [],
more: false,
numItems: 5,
search: null,
}
},
computed: {
environment: {
get() {
return cloneDeep(this.value)
},
set(value) {
if (typeof value !== "string") {
this.$emit("input", value)
}
},
},
},
created() {
this.fetchData()
this.$watch(
(vm) => [vm.project],
() => {
this.fetchData()
}
)
},
methods: {
loadMore() {
this.numItems = this.numItems + 5
this.fetchData()
},
fetchData() {
this.error = null
this.loading = "error"
let filterOptions = {
q: this.search,
itemsPerPage: this.numItems,
}
if (this.project) {
filterOptions = {
...filterOptions,
filters: {
project: [this.project],
},
}
}
filterOptions = SearchUtils.createParametersFromTableOptions({ ...filterOptions })
SourceEnvironmentApi.getAll(filterOptions).then((response) => {
this.items = response.data.items
if (this.environment) {
// check to see if the current selection is available in the list and if not we add it
if (!this.items.find((match) => match.id === this.environment.id)) {
this.items = [this.environment].concat(this.items)
}
}
this.total = response.data.total
if (this.items.length < this.total) {
this.more = true
} else {
this.more = false
}
this.loading = false
})
},
getFilteredData: debounce(function () {
this.fetchData()
}, 500),
},
}
</script>