EdgeInstanceFormDrawer.vue
4.08 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
<script lang="ts" setup>
import { nextTick, ref, unref, computed } from 'vue';
import { BasicForm, useForm } from '/@/components/Form';
import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
import { FormFieldsEnum, formSchema } from './config';
import { HandleOperationEnum, HandleOperationNameEnum } from '../../config';
import { useMessage } from '/@/hooks/web/useMessage';
import { createOrEditEdgeInstance } from '/@/api/edgeManage/edgeInstance';
import { EdgeInstanceItemType } from '/@/api/edgeManage/model/edgeInstance';
import { buildUUID, randomString } from '/@/utils/uuid';
import { handeleCopy } from '/@/views/device/profiles/step/topic';
import { useI18n } from '/@/hooks/web/useI18n';
const emits = defineEmits(['success', 'register']);
const { createMessage } = useMessage();
const isUpdate = ref<Boolean>();
const isUpdateText = ref<String>();
const recordData = ref<EdgeInstanceItemType>();
const { t } = useI18n();
const [registerForm, { resetFields, validate, setFieldsValue }] = useForm({
labelWidth: 120,
schemas: formSchema,
showActionButtonGroup: false,
});
const cacheTitle = computed(() =>
!unref(isUpdate) ? t('edge.instance.action.create') : t('edge.instance.action.edit')
);
const handleCopyRoutingKey = (text: string) => handeleCopy(text);
const handleCopySecret = (text: string) => handeleCopy(text);
const setDefaultValue = (event: HandleOperationEnum) => {
if (event === HandleOperationEnum.CREATE) {
setFieldsValue({
[FormFieldsEnum.ROUTINGKEY]: buildUUID(),
[FormFieldsEnum.SECRET]: randomString(),
});
}
};
const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
setDrawerProps({ loading: true });
await resetFields();
setDefaultValue(data.event);
isUpdate.value = data.isUpdate;
isUpdateText.value = data.isUpdateText;
recordData.value = data.record;
try {
await nextTick();
setFieldsValue(data.record);
setFieldsValue({
[FormFieldsEnum.DESCRIPTION]: data.record?.additionalInfo[FormFieldsEnum.DESCRIPTION],
});
} finally {
setDrawerProps({ loading: false });
}
});
const getValue = async () => {
const values = await validate();
if (!values) return;
const additionalInfo = {
description: values[FormFieldsEnum.DESCRIPTION],
};
const mergeValues = {
...recordData.value,
...values,
...{ additionalInfo },
};
Reflect.deleteProperty(mergeValues, FormFieldsEnum.DESCRIPTION);
await createOrEditEdgeInstance(mergeValues);
createMessage.success(
unref(isUpdateText) === HandleOperationNameEnum.CREATE
? t('common.createSuccessText')
: t('common.editText') + t('common.successText')
);
closeDrawer();
setTimeout(() => {
emits('success');
}, 500);
};
const handleSubmit = () => getValue();
</script>
<template>
<div>
<BasicDrawer
destroyOnClose
v-bind="$attrs"
showFooter
:title="cacheTitle"
width="30%"
:maskClosable="true"
@register="registerDrawer"
@ok="handleSubmit"
>
<BasicForm @register="registerForm">
<template #routingKey="{ model, field }">
<div class="!flex justify-between items-center gap-5">
<a-input disabled v-model:value="model[field]" />
<a-button
type="link"
@click="handleCopyRoutingKey(model[field])"
class="cursor-pointer"
>
{{ t('edge.instance.action.copy') }}
</a-button>
</div>
</template>
<template #secret="{ model, field }">
<div class="!flex justify-between items-center gap-5">
<a-input disabled v-model:value="model[field]" />
<a-button type="link" @click="handleCopySecret(model[field])" class="cursor-pointer">
{{ t('edge.instance.action.copy') }}
</a-button>
</div>
</template>
</BasicForm>
</BasicDrawer>
</div>
</template>
<style lang="less" scoped></style>