-
Notifications
You must be signed in to change notification settings - Fork 10
/
AddConsumerModal.vue
291 lines (257 loc) · 8.11 KB
/
AddConsumerModal.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
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
<template>
<KPrompt
:action-pending="isPending"
class="kong-ui-entities-add-consumers-modal"
data-testid="add-consumer-modal"
:is-visible="visible"
:title="t('consumers.consumer_groups.add.title')"
@canceled="cancel"
@proceed="addConsumers"
>
<template #body-content>
<div class="add-consumer-form-container">
<p class="add-consumer-form-cta">
{{ t('consumers.consumer_groups.add.ctaText') }}
</p>
<KMultiselect
v-model="selectedConsumers"
autosuggest
data-testid="add-consumers-multiselect"
:dropdown-footer-text="additionalRecordsExist ? t('consumers.consumer_groups.add.footer') : undefined"
:items="availableConsumers"
:label="t('consumers.consumer_groups.add.consumers_label')"
:loading="loading"
:placeholder="t('consumers.consumer_groups.add.consumer_placeholder')"
:readonly="isPending"
required
width="100%"
@query-change="debouncedQueryChange"
>
<template #item-template="{ item }">
<div class="select-item-label">
{{ item.label }}
</div>
<div
v-if="getGeneric(item).data.username && getGeneric(item).data.custom_id"
class="select-item-desc"
>
{{ getGeneric(item).data.custom_id }}
</div>
</template>
</KMultiselect>
<div
v-if="error || fetchErrorMessage || errorArray.length"
class="kong-ui-entity-add-consumers-error"
>
<KAlert appearance="danger">
<template #alertMessage>
<p>{{ t('consumers.errors.add') }}</p>
<ul v-if="errorArray.length">
<li
v-for="(err, idx) in errorArray"
:key="idx"
>
{{ err }}
</li>
</ul>
<div>
{{ error || fetchErrorMessage }}
</div>
</template>
</KAlert>
</div>
</div>
</template>
</KPrompt>
</template>
<script setup lang="ts">
import type { PropType } from 'vue'
import { computed, ref, watch } from 'vue'
import type {
KongManagerConsumerListConfig,
KonnectConsumerListConfig,
} from '../types'
import { useDebouncedFilter, useAxios } from '@kong-ui-public/entities-shared'
import composables from '../composables'
import endpoints from '../consumers-endpoints'
import type { MultiselectItem } from '@kong/kongponents'
const { i18n: { t } } = composables.useI18n()
const props = defineProps({
/** The base konnect or kongManger config. Pass additional config props in the shared entity component as needed. */
config: {
type: Object as PropType<KonnectConsumerListConfig | KongManagerConsumerListConfig>,
required: true,
validator: (config: KonnectConsumerListConfig | KongManagerConsumerListConfig): boolean => {
if (!config || !['konnect', 'kongManager'].includes(config?.app)) return false
if (!config.consumerGroupId) return false
return true
},
},
visible: {
type: Boolean,
required: true,
default: false,
},
})
const emit = defineEmits<{
(e: 'cancel'): void
(e: 'add:success', consumers: string[]): void
(e: 'add:partial-success', consumers: string[]): void
(e: 'error', msg: string): void
}>()
const { axiosInstance } = useAxios({
headers: props.config?.requestHeaders,
})
/**
* ------------------------------
* Consumer Handling
* ------------------------------
*/
// Multiselect
const selectedConsumers = ref<string[]>([])
const {
debouncedQueryChange,
loading,
allRecords,
error: fetchError,
loadItems,
results,
} = useDebouncedFilter(
props.config,
endpoints.list[props.config.app].all,
'',
{
fetchedItemsKey: 'data',
searchKeys: ['username', 'id'],
},
)
// generic typing to get around `MultiselectItem` type being too strict to allow
// a `data` key
const getGeneric = (item: MultiselectItem) => {
return item as unknown as any
}
const fetchErrorMessage = computed((): string => fetchError.value ? t('consumers.errors.general') : '')
const consumersSelectKey = ref<number>(0)
// this will only be defined if we were able to initially fetch ALL available records
const additionalRecordsExist = computed((): boolean => allRecords.value === undefined)
const availableConsumers = computed((): MultiselectItem[] => {
return results.value.map((record: Record<string, any>) => ({
label: record.username || record.customId,
value: record.id,
selected: selectedConsumers.value.includes(record.id),
data: record, // we need this to determine whether or not to show the description text
}))
})
const getConsumerIdentifier = (consumerId: string): string => {
const consumer = additionalRecordsExist.value ? results.value.find(con => con.id === consumerId) : allRecords.value?.find(con => con.id === consumerId)
if (consumer) {
return consumer.username || consumer.custom_id
}
return consumerId
}
const cancel = () => {
error.value = ''
successArray.value = []
errorArray.value = []
emit('cancel')
}
// Add
const successArray = ref<string[]>([])
const isPending = ref(false)
const error = ref('')
const addConsumers = async (): Promise<void> => {
if (!selectedConsumers.value.length) {
error.value = ''
errorArray.value = []
emit('cancel')
return
}
isPending.value = true
error.value = ''
successArray.value = []
errorArray.value = []
try {
const bulkOperation = selectedConsumers.value.map(consumerId => addConsumerToGroup(consumerId))
const results = await Promise.allSettled(bulkOperation)
let allPass = true
results.forEach(result => {
if (result.status !== 'fulfilled') {
allPass = false
}
})
if (allPass) {
successArray.value = []
errorArray.value = []
// Emit the success event for the host app
emit('add:success', selectedConsumers.value)
selectedConsumers.value = []
} else {
if (successArray.value.length) {
emit('add:partial-success', successArray.value)
}
selectedConsumers.value = []
handleBulkError(results)
}
} catch (_err: any) {
error.value = t('consumers.errors.add')
// Emit the error event for the host app
emit('error', error.value)
} finally {
isPending.value = false
}
}
const submitUrl = computed((): string => {
let url = `${props.config.apiBaseUrl}${endpoints.list[props.config.app].forConsumerGroup}`
if (props.config.app === 'konnect') {
url = url
.replace(/{controlPlaneId}/gi, props.config?.controlPlaneId || '')
.replace(/{consumerGroupId}/gi, props.config?.consumerGroupId || '')
} else if (props.config.app === 'kongManager') {
url = url
.replace(/\/{workspace}/gi, props.config?.workspace ? `/${props.config.workspace}` : '')
.replace(/{consumerGroupId}/gi, props.config?.consumerGroupId || '')
}
return url
})
const addConsumerToGroup = async (consumerId: string): Promise<any> => {
const payload = {
consumer: consumerId,
}
try {
const res = await axiosInstance.post(submitUrl.value, payload)
successArray.value.push(consumerId)
return res
} catch (error: any) {
let msg = `${getConsumerIdentifier(consumerId)} - ${error.message}`
if (error.response.status === 409) {
msg = `${getConsumerIdentifier(consumerId)} - ${t('consumers.errors.already_added')}`
}
return Promise.reject(Error(msg))
}
}
const errorArray = ref<string[]>([])
const handleBulkError = (array: any[]): void => {
errorArray.value = array.map(err => err.reason?.message).filter(Boolean)
}
watch(availableConsumers, () => {
consumersSelectKey.value++
}, { immediate: true, deep: true })
watch(() => props.visible, () => {
if (props.visible) {
loadItems() // load consumers
}
}, { immediate: true })
</script>
<style lang="scss" scoped>
.kong-ui-entities-add-consumers-modal {
.add-consumer-form-cta {
margin-top: 0;
}
.add-consumer-form-container {
margin-bottom: 20px;
.kong-ui-entity-add-consumers-error {
margin-top: 16px;
}
}
}
</style>