github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/static_source/admin/src/views/Automation/Tasks/new.vue (about) 1 <script setup lang="ts"> 2 import {defineEmits, ref, unref} from 'vue' 3 import {useI18n} from '@/hooks/web/useI18n' 4 import {ElButton} from 'element-plus' 5 import {useForm} from '@/hooks/web/useForm' 6 import {useCache} from '@/hooks/web/useCache' 7 import {useAppStore} from '@/store/modules/app' 8 import {usePermissionStore} from '@/store/modules/permission' 9 import {useRoute, useRouter} from 'vue-router' 10 import {useValidator} from '@/hooks/web/useValidator' 11 import api from "@/api/api"; 12 import {ApiNewTaskRequest, ApiTask} from "@/api/stub"; 13 import {ContentWrap} from "@/components/ContentWrap"; 14 import TaskForm from "@/views/Automation/components/TaskForm.vue"; 15 import {Form} from "@/components/Form"; 16 17 const {register, elFormRef, methods} = useForm() 18 const {required} = useValidator() 19 const emit = defineEmits(['to-restore']) 20 const appStore = useAppStore() 21 const permissionStore = usePermissionStore() 22 const {currentRoute, addRoute, push} = useRouter() 23 const route = useRoute(); 24 const {wsCache} = useCache() 25 const {t} = useI18n() 26 27 const writeRef = ref<ComponentRef<typeof Form>>() 28 const loading = ref(false) 29 const currentRow = ref<Nullable<ApiTask>>(null) 30 31 const save = async () => { 32 const write = unref(writeRef) 33 const validate = await write?.elFormRef?.validate()?.catch(() => { 34 }) 35 if (validate) { 36 loading.value = true 37 const data = (await write?.getFormData()) 38 const body = { 39 name: data.name, 40 description: data.description, 41 enabled: data.enabled, 42 condition: data.condition, 43 triggers: data.triggers, 44 conditions: data.conditions, 45 actions: data.actions, 46 areaId: data.area?.id || null, 47 } as ApiNewTaskRequest 48 const res = await api.v1.automationServiceAddTask(body) 49 .catch(() => { 50 }) 51 .finally(() => { 52 loading.value = false 53 }) 54 if (res) { 55 const {id} = res.data 56 push(`/automation/tasks/edit/${id}`) 57 } 58 } 59 } 60 61 const cancel = () => { 62 push('/automation/tasks') 63 } 64 65 </script> 66 67 <template> 68 <ContentWrap> 69 <TaskForm ref="writeRef" :current-row="currentRow"/> 70 71 <div style="text-align: right"> 72 73 <ElButton type="primary" @click="save()"> 74 {{ t('main.save') }} 75 </ElButton> 76 77 <ElButton type="default" @click="cancel()"> 78 {{ t('main.cancel') }} 79 </ElButton> 80 81 </div> 82 </ContentWrap> 83 84 </template> 85 86 <style lang="less" scoped> 87 88 </style>