NetActionPropertyPage.vue
18.7 KB
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
<script setup lang="ts">
import { ref, shallowRef, onBeforeMount, computed, watch } from 'vue'
import { useI18n } from 'vue-i18n'
import { useToast } from 'vuestic-ui'
import netActionPropertysApi from '../../services/netActionPropertys'
import { useGridFilters } from '../../composables/useGridFilters'
import { useOptions } from '../../composables/useOptions'
import {
ColDef,
GridReadyEvent,
FilterChangedEvent,
ITooltipParams
} from "ag-grid-community"
const rowData = ref<any[] | null>(null)
const { t } = useI18n()
const { init: notify } = useToast()
const { containsFilterParams, dateFilterParams, gridLocaleText, createSelectFilterParams, createSelectByArrayFilterParams } = useGridFilters()
const { loadOptions, getResponseWaitTypeOptions } = useOptions()
const requestMethodOptions = ['GET', 'POST', 'PUT', 'DELETE', 'PATCH']
const waitResponseTypeOptions = computed(() => getResponseWaitTypeOptions())
const columnDefs = shallowRef<ColDef[]>([
{ checkboxSelection: true, headerCheckboxSelection: true, width: 50, pinned: 'left', filter: false, sortable: false },
{
headerName: t('netActionPropertys.table.serialNumber'),
width: 80,
filter: false,
sortable: false,
valueGetter: (params: any) => (params.node?.rowIndex ?? 0) + 1,
cellStyle: { textAlign: 'center' }
},
{ field: 'actionName', headerName: t('netActionPropertys.table.actionName'), width: 150 },
{ field: 'requestMethod', headerName: t('netActionPropertys.table.requestMethod'),
width: 100 ,
filterParams: createSelectByArrayFilterParams(requestMethodOptions),
valueGetter: (params: any) => {
const value = params.data?.requestMethod
const option = requestMethodOptions.find(opt => opt === value)
return option
}
},
{ field: 'requestUrl', headerName: t('netActionPropertys.table.requestUrl'), width: 200 },
{
field: 'waitResponseType',
headerName: t('netActionPropertys.form.waitResponseType'),
width: 120,
filterParams: createSelectFilterParams(waitResponseTypeOptions.value),
valueGetter: (params: any) => {
const value = params.data?.waitResponseType
const option = waitResponseTypeOptions.value.find(opt => opt.value === value)
return option?.text || value
}
},
{ field: 'description', headerName: t('netActionPropertys.table.description'), width: 150 },
{ field: 'isActive', headerName: t('netActionPropertys.table.isActive'), width: 80, filterParams: null,
valueFormatter: (p: any) => p.value ? t('netActionPropertys.options.yes') : t('netActionPropertys.options.no')
},
{
field: 'createdAt',
headerName: t('netActionPropertys.table.createdAt'),
minWidth: 150,
flex: 1,
filter: 'agDateColumnFilter',
filterParams: {
...dateFilterParams.value,
comparator: (filterLocalDateAtMidnight: Date, cellValue: string) => {
if (!cellValue) return -1
const cellDate = new Date(cellValue)
if (cellDate < filterLocalDateAtMidnight) return -1
if (cellDate > filterLocalDateAtMidnight) return 1
return 0
}
}
},
{
headerName: t('netActionPropertys.table.actions'),
field: 'actions',
width: 120,
pinned: 'right',
filter: false,
sortable: false,
cellRenderer: (p: any) => {
const editLabel = t('netActionPropertys.actions.edit')
const deleteLabel = t('netActionPropertys.actions.delete')
const id = p.data?.netActionId ?? null
const idLiteral = id !== null ? `'${String(id).replace(/'/g, "\\'")}'` : 'null'
const containerStyle = 'display:flex; align-items:center; justify-content:center; height:100%;'
const groupStyle = 'display:inline-flex; border-radius:6px; overflow:hidden;'
const btnStyle = 'border-radius:0; margin:0; border-right:1px solid rgba(255,255,255,0.06);'
const btnStyleLast = 'border-radius:0; margin:0;'
return '<div style="' + containerStyle + '">' +
'<div style="' + groupStyle + '">' +
'<button class="va-button va-button--small va-button--primary" style="' + btnStyle + '" onclick="window.onEditNetActionProperty(' + idLiteral + ')" title="' + editLabel + '" aria-label="' + editLabel + '"><i class="material-icons" style="font-size:16px;color:#000000">edit</i></button>' +
'<button class="va-button va-button--small va-button--primary" style="' + btnStyleLast + '" onclick="window.onDeleteNetActionProperty(' + idLiteral + ')" title="' + deleteLabel + '" aria-label="' + deleteLabel + '"><i class="material-icons" style="font-size:16px;color:#d32f2f">delete</i></button>' +
'</div></div>'
}
}
])
const defaultColDef = ref<ColDef>({
floatingFilter: true,
filter: true,
cellStyle: {
whiteSpace: 'nowrap',
overflow: 'hidden',
textOverflow: 'ellipsis'
},
filterParams: containsFilterParams.value,
tooltipValueGetter: (p: ITooltipParams) => p.value
})
const gridApi = ref<any>(null)
const isGridInitialized = ref(false)
const isFetchingData = ref(false)
const paginationRowCount = ref<number | null>(null)
const currentPage = ref(1)
const totalPages = ref(1)
const queryParams = ref({
pageNumber: 1,
pageSize: 20,
filterModel: {} as any
})
function onGridReady(params: GridReadyEvent) {
gridApi.value = params.api;
queryParams.value.pageNumber = 1
queryParams.value.pageSize = 20
isGridInitialized.value = true
fetchRowData()
}
const tbLoading = ref(false)
const editModalOpen = ref(false)
const editFormRef = ref<any>({})
const editForm = ref<any>(null)
const confirmModalOpen = ref(false)
const confirmMessage = ref('')
const confirmCallback = ref<(() => void) | null>(null)
const showConfirm = (message: string, callback: () => void) => {
confirmMessage.value = message
confirmCallback.value = callback
confirmModalOpen.value = true
}
const handleConfirm = () => {
confirmModalOpen.value = false
if (confirmCallback.value) {
confirmCallback.value()
}
}
const ShowLoading = () => {
tbLoading.value = true
}
const HideLoading = () => {
tbLoading.value = false
}
const onEdit = async (paramsOrId: any) => {
try {
ShowLoading()
const id = String((paramsOrId && (paramsOrId.netActionId ?? paramsOrId.id)) ?? paramsOrId)
const res = await netActionPropertysApi.get(id)
HideLoading()
const data = (res && (res.Data ?? res.data)) || res || {}
editFormRef.value = { ...data }
editModalOpen.value = true
} catch (err: any) {
HideLoading()
notify({ message: err?.message || t('netActionPropertys.messages.operationFailed'), color: 'danger' })
}
}
const saveEdit = async () => {
if (editForm.value && !editForm.value.validate()) {
return
}
try {
ShowLoading()
const res = await netActionPropertysApi.createOrUpdate(editFormRef.value)
HideLoading()
if (res?.success === false || res?.Success === false) {
notify({ message: res.message || res.Message || t('netActionPropertys.messages.updateFailed'), color: 'danger' })
return
}
notify({ message: res?.message || res?.Message || t('netActionPropertys.messages.updated'), color: 'success' })
editModalOpen.value = false
fetchRowData()
} catch (err: any) {
HideLoading()
notify({ message: err?.message || t('netActionPropertys.messages.operationFailed'), color: 'danger' })
}
}
const onDelete = async (paramsOrId: any) => {
const id = String((paramsOrId && (paramsOrId.netActionId ?? paramsOrId.id)) ?? paramsOrId)
if (!id || id === 'null' || id === 'undefined') {
notify({ message: t('netActionPropertys.messages.missingId'), color: 'failed' })
return
}
showConfirm(t('netActionPropertys.messages.confirmDelete'), async () => {
try {
ShowLoading()
const res = await netActionPropertysApi.delete(id)
HideLoading()
if (res && (res.success === false)) {
notify({ message: res.message || t('netActionPropertys.messages.deleteFailed'), color: 'failed' })
} else {
notify({ message: res?.message || t('netActionPropertys.messages.deleted'), color: 'success' })
fetchRowData()
}
} catch (err: any) {
HideLoading()
notify({ message: err?.message || t('netActionPropertys.messages.operationFailed'), color: 'danger' })
}
})
}
const bulkDelete = async () => {
const selected = gridApi.value ? gridApi.value.getSelectedRows() : []
if (!selected || selected.length === 0) {
notify({ message: t('netActionPropertys.messages.selectForDelete'), color: 'warning' })
return
}
showConfirm(t('netActionPropertys.messages.confirmBulkDelete', { count: selected.length }), async () => {
try {
ShowLoading()
const results = await Promise.all(selected.map((r: any) => {
const id = String(r?.netActionId ?? r?.id ?? '')
return id && id !== 'null' && id !== 'undefined' ? netActionPropertysApi.delete(id) : Promise.resolve(null)
}))
HideLoading()
notify({ message: t('netActionPropertys.messages.deletedCount', { count: results.length }), color: 'success' })
fetchRowData()
} catch (err: any) {
HideLoading()
notify({ message: err?.message || t('netActionPropertys.messages.bulkDeleteFailed'), color: 'danger' })
}
})
}
const addNetActionProperty = () => {
editFormRef.value = {
netActionId: null,
actionName: '',
isActive: true,
requestMethod: 'POST',
requestUrl: '',
requestParams: '',
repeatCount: 1,
intervalTimeMs: 3000,
waitResponseType: 0,
responseValidationRule: '',
description: '',
extraProperties: ''
}
editModalOpen.value = true
}
const onFilterChanged = (params: FilterChangedEvent) => {
if (!isGridInitialized.value || isFetchingData.value) return
const filterModel = params.api.getFilterModel()
if (filterModel.requestMethod) {
const selectedValue = filterModel.requestMethod.type
// Transform custom option selection (where type is the value) into a standard text equals filter
if (selectedValue && requestMethodOptions.includes(selectedValue)) {
filterModel.requestMethod = {
filterType: 'text',
type: 'equals',
filter: selectedValue
}
}
}
if (filterModel.waitResponseType) {
const selectedValue = filterModel.waitResponseType.type // AG-Grid 的 type 是选中的 displayKey
filterModel.waitResponseType = {
filterType: 'enum',
type: 'equals',
filter: parseInt(selectedValue, 10) || selectedValue
}
}
queryParams.value.filterModel = filterModel
fetchRowData()
}
const onPaginationChange = (page: number) => {
if (page !== queryParams.value.pageNumber) {
queryParams.value.pageNumber = page
fetchRowData()
}
}
const refreshData = async () => {
try {
await fetchRowData()
notify({ message: t('netActionPropertys.messages.refreshSuccess'), color: 'success' })
} catch (err: any) {
notify({ message: err?.message || t('netActionPropertys.messages.refreshFailed'), color: 'danger' })
}
}
const resetFilters = () => {
try {
if (gridApi.value) {
queryParams.value.filterModel = {}
gridApi.value.setFilterModel(null)
fetchRowData()
notify({ message: t('netActionPropertys.messages.resetSuccess'), color: 'success' })
}
} catch (err: any) {
notify({ message: err?.message || t('netActionPropertys.messages.resetFailed'), color: 'danger' })
}
}
const fetchRowData = async () => {
if (isFetchingData.value) return
try {
isFetchingData.value = true
ShowLoading()
const params = {
...queryParams.value,
filterModel: JSON.stringify(queryParams.value.filterModel || {})
}
console.log(params)
const res = await netActionPropertysApi.list(params)
HideLoading()
if (!res.success) {
notify({ message: res.message, color: 'failed' })
}
const data = (res && (res.Data ?? res.data)) || []
rowData.value = Array.isArray(data) ? data : []
if (res?.pageInfo) {
const pageInfo = res.pageInfo
paginationRowCount.value = pageInfo.totalCount || 0
totalPages.value = pageInfo.totalPages || 1
currentPage.value = pageInfo.pageNumber || 1
}
} catch (err: any) {
HideLoading()
notify({ message: err?.message || t('netActionPropertys.messages.fetchFailed'), color: 'danger' })
} finally {
isFetchingData.value = false
}
}
(window as any).onEditNetActionProperty = onEdit;
(window as any).onDeleteNetActionProperty = onDelete;
onBeforeMount(async () => {
await loadOptions()
})
</script>
<template>
<VaCard>
<VaCardContent>
<div class="flex flex-col md:flex-row gap-2 mb-2 justify-between">
<div class="flex items-center gap-2">
<div role="group" aria-label="net-action-property-actions" style="display:inline-flex; border-radius:6px; overflow:hidden;">
<VaButton size="small" @click="addNetActionProperty" :title="t('netActionPropertys.actions.add')" :aria-label="t('netActionPropertys.actions.add')" preset="primary" style="border-radius:0; margin:0; border-right:1px solid rgba(255,255,255,0.06)" icon="add" />
<VaButton size="small" @click="bulkDelete" :title="t('netActionPropertys.actions.delete')" :aria-label="t('netActionPropertys.actions.delete')" preset="primary" style="border-radius:0; margin:0; border-right:1px solid rgba(255,255,255,0.06)" icon="delete" />
<VaButton size="small" @click="refreshData" :title="t('netActionPropertys.actions.refresh')" :aria-label="t('netActionPropertys.actions.refresh')" preset="primary" style="border-radius:0; margin:0; border-right:1px solid rgba(255,255,255,0.06)" icon="sync" />
<VaButton size="small" @click="resetFilters" :title="t('netActionPropertys.actions.resetFilters')" :aria-label="t('netActionPropertys.actions.resetFilters')" preset="primary" style="border-radius:0; margin:0;" icon="clear_all" />
</div>
</div>
<div class="flex items-center">
<VaPagination
v-model="currentPage"
class="justify-end map-pagination"
style="height: 24px; min-height: 24px;"
:pages="totalPages"
input
@update:model-value="onPaginationChange"
/>
</div>
</div>
<div class="table-container">
<AgGridVue
style="width: 100%; height: 100%;"
@grid-ready="onGridReady"
@filter-changed="onFilterChanged"
:tooltipShowDelay="ref(500)"
:columnDefs="columnDefs"
:loading="tbLoading"
:localeText="gridLocaleText"
:defaultColDef="defaultColDef"
:rowSelection="'multiple'"
:checkboxSelection="true"
:headerCheckboxSelection="true"
:rowData="rowData"></AgGridVue>
</div>
</VaCardContent>
</VaCard>
<VaModal v-model="confirmModalOpen" size="small" close-button :ok-text="t('vuestic.ok')" :cancel-text="t('vuestic.cancel')" @ok="handleConfirm">
<h3 class="modal-title">{{ t('common.confirmOperation') }}</h3>
<p style="margin: 1rem 0;">{{ confirmMessage }}</p>
</VaModal>
<VaModal v-model="editModalOpen" size="medium" close-button max-height="calc(100vh - 20px)" :ok-text="t('vuestic.ok')" :cancel-text="t('vuestic.cancel')" @ok="saveEdit" hide-default-actions>
<template #header>
<h3 class="modal-title">{{ editFormRef.netActionId ? t('netActionPropertys.form.title.edit') : t('netActionPropertys.form.title.add') }}</h3>
</template>
<div class="modal-content-scrollable">
<VaForm ref="editForm">
<div class="section">
<div class="form-row">
<div class="input-with-required-mark">
<div class="required-label"><span class="required-star">*</span><span>{{ t('netActionPropertys.form.requestMethod') }}</span></div>
<VaSelect v-model="editFormRef.requestMethod" :options="requestMethodOptions" />
</div>
<div class="input-with-required-mark">
<div class="required-label"><span class="required-star">*</span><span>{{ t('netActionPropertys.form.actionName') }}</span></div>
<VaInput v-model="editFormRef.actionName" :rules="[(v: any) => !!v || t('auth.required_field')]" />
</div>
</div>
<div class="form-row">
<div class="input-with-required-mark">
<div class="required-label"><span class="required-star">*</span><span>{{ t('netActionPropertys.form.requestUrl') }}</span></div>
<VaInput v-model="editFormRef.requestUrl" :rules="[(v: any) => !!v || t('auth.required_field')]" />
</div>
</div>
<div class="form-row">
<VaInput v-model="editFormRef.repeatCount" type="number" :label="t('netActionPropertys.form.repeatCount')" />
<VaInput v-model="editFormRef.intervalTimeMs" type="number" :label="t('netActionPropertys.form.intervalTimeMs')" />
</div>
<div class="form-row">
<VaSelect v-model="editFormRef.waitResponseType" :label="t('netActionPropertys.form.waitResponseType')" :options="waitResponseTypeOptions" text-by="text" value-by="value" />
<VaSwitch v-model="editFormRef.isActive" :label="t('netActionPropertys.form.isActive')" style="margin-top: 1rem;" />
</div>
<div class="form-row">
<VaTextarea v-model="editFormRef.requestParams" :label="t('netActionPropertys.form.requestParams')" :min-rows="3" class="form-textarea" />
</div>
<div class="form-row">
<VaTextarea v-model="editFormRef.responseValidationRule" :label="t('netActionPropertys.form.responseValidationRule')" :min-rows="2" class="form-textarea" />
</div>
<div class="form-row">
<VaTextarea v-model="editFormRef.description" :label="t('netActionPropertys.form.description')" :min-rows="2" class="form-textarea" />
</div>
<div class="form-row">
<VaTextarea v-model="editFormRef.extraProperties" :label="t('netActionPropertys.form.extraProperties')" :min-rows="2" class="form-textarea" />
</div>
</div>
</VaForm>
</div>
<template #footer>
<VaButton preset="secondary" @click="editModalOpen = false">{{ t('vuestic.cancel') }}</VaButton>
<VaButton @click="saveEdit">{{ t('vuestic.ok') }}</VaButton>
</template>
</VaModal>
</template>
<style scoped>
.modal-content-scrollable {
max-height: calc(100vh - 150px);
overflow-y: auto;
padding: 1rem;
}
.table-container {
height: calc(100vh - 200px);
min-height: 400px;
}
.form-row {
display: flex;
gap: 1rem;
margin-bottom: 1rem;
}
.form-row > * {
flex: 1;
}
.input-with-required-mark {
display: flex;
flex-direction: column;
}
.required-label {
display: flex;
align-items: center;
margin-bottom: 0.25rem;
font-size: 0.8rem; /* Match VaInput label size roughly */
font-weight: 700;
color: #000000;
text-transform: uppercase;
letter-spacing: 0.6px;
}
.required-star {
color: var(--va-danger);
margin-right: 0.25rem;
}
.form-textarea {
width: 100%;
}
</style>