ScriptDrawer.vue
3.12 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
<template>
<div>
<BasicDrawer
v-bind="$attrs"
:title="getTitle"
@register="register"
width="500px"
@ok="handleSubmit"
destroyOnClose
showFooter
>
<BasicForm @register="registerForm" />
</BasicDrawer>
</div>
</template>
<script lang="ts" setup>
import { ref, computed, unref } from 'vue';
import { useDrawerInner, BasicDrawer } from '/@/components/Drawer/index';
import { useForm, BasicForm } from '/@/components/Form/index';
import { formSchema } from '../config/config.data';
import { createOrEditTransformScriptApi } from '/@/api/device/TransformScriptApi';
import { useMessage } from '/@/hooks/web/useMessage';
import { useI18n } from '/@/hooks/web/useI18n';
const emit = defineEmits(['register', 'isStatus', 'success']);
const { t } = useI18n();
const isUpdate: any = ref(false);
const isView = ref(true);
const getTitle = computed(() =>
isUpdate.value ? t('rule.script.index.edit') : t('rule.script.index.add')
);
const editId = ref('');
const [register, { setDrawerProps, closeDrawer }] = useDrawerInner((data) => {
resetFields();
setDrawerProps({ confirmLoading: false });
isUpdate.value = data.isUpdate;
switch (isUpdate.value) {
case 'view':
isView.value = false;
setDrawerProps({
showFooter: unref(isView),
title: t('rule.script.index.view'),
loading: false,
});
editId.value = data.record.id;
setFieldsValue({
...data.record,
function: data?.record?.configuration,
});
break;
case true:
isView.value = true;
setDrawerProps({
showFooter: unref(isView),
title: t('rule.script.index.edit'),
loading: false,
});
editId.value = data.record.id;
setFieldsValue({
...data.record,
function: data?.record?.configuration,
});
break;
case false:
isView.value = true;
setDrawerProps({
showFooter: unref(isView),
title: t('rule.script.index.add'),
loading: false,
});
break;
}
});
const [registerForm, { validate, setFieldsValue, resetFields }] = useForm({
showActionButtonGroup: false,
baseColProps: { span: 24 },
schemas: formSchema,
});
const handleSubmit = async () => {
const editIdPost = isUpdate.value ? { id: editId.value } : {};
try {
setDrawerProps({ confirmLoading: true });
const fieldsValue = await validate();
if (!fieldsValue) return;
await createOrEditTransformScriptApi({
configuration: fieldsValue.function,
type: 'org.thingsboard.rule.engine.transform.TbTransformMsgNode',
...fieldsValue,
...editIdPost,
});
closeDrawer();
emit('success');
const { createMessage } = useMessage();
createMessage.success(
isUpdate.value ? t('common.editSuccessText') : t('common.createSuccessText')
);
} catch (e) {
} finally {
setTimeout(() => {
setDrawerProps({ confirmLoading: false });
}, 300);
}
};
</script>