github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/static_source/admin/src/views/Scripts/new.vue (about) 1 <script setup lang="ts"> 2 import {ref, unref} from 'vue' 3 import {useI18n} from '@/hooks/web/useI18n' 4 import {ElButton} from 'element-plus' 5 import {useRouter} from 'vue-router' 6 import api from "@/api/api"; 7 import Form from './components/Form.vue' 8 import {ApiNewScriptRequest} from "@/api/stub"; 9 import {ContentWrap} from "@/components/ContentWrap"; 10 11 const {push} = useRouter() 12 const {t} = useI18n() 13 14 const writeRef = ref<ComponentRef<typeof Form>>() 15 const loading = ref(false) 16 const currentScript = ref<Nullable<ApiNewScriptRequest>>(null) 17 const sourceScript = ref('') 18 19 const save = async () => { 20 const write = unref(writeRef) 21 const validate = await write?.elFormRef?.validate()?.catch(() => { 22 }) 23 if (validate) { 24 loading.value = true 25 const data = (await write?.getFormData()) 26 const body = { 27 lang: data.lang, 28 name: data.name, 29 source: sourceScript.value, 30 description: data.description, 31 } as ApiNewScriptRequest 32 const res = await api.v1.scriptServiceAddScript(body) 33 .catch(() => { 34 }) 35 .finally(() => { 36 loading.value = false 37 }) 38 if (res) { 39 const {id} = res.data; 40 push(`/scripts/edit/${id}`) 41 } 42 } 43 } 44 45 const cancel = () => { 46 push('/scripts') 47 } 48 49 </script> 50 51 <template> 52 <ContentWrap> 53 <Form ref="writeRef" :current-row="currentScript"/> 54 55 <div style="text-align: right"> 56 57 <ElButton type="primary" @click="save()"> 58 {{ t('main.save') }} 59 </ElButton> 60 61 <ElButton type="default" @click="cancel()"> 62 {{ t('main.cancel') }} 63 </ElButton> 64 65 </div> 66 </ContentWrap> 67 68 </template> 69 70 <style lang="less" scoped> 71 72 </style>