github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/static_source/admin/src/views/Automation/Tasks/edit.vue (about) 1 <script setup lang="ts"> 2 import {computed, ref, unref} from 'vue' 3 import {useI18n} from '@/hooks/web/useI18n' 4 import {ElButton, ElCard, ElMessage, ElPopconfirm, ElTabPane, ElTabs, ElTimeline, ElTimelineItem} from 'element-plus' 5 import {useRoute, useRouter} from 'vue-router' 6 import api from "@/api/api"; 7 import {ApiTask} from "@/api/stub"; 8 import {ContentWrap} from "@/components/ContentWrap"; 9 import {Form} from "@/components/Form"; 10 import TaskForm from "@/views/Automation/components/TaskForm.vue"; 11 import {TriggersSearch} from "@/components/TriggersSearch"; 12 import {ConditionsSearch} from "@/components/ConditionsSearch"; 13 import {ActionsSearch} from "@/components/ActionsSearch"; 14 import TaskTelemetry from "@/views/Automation/components/TaskTelemetry.vue"; 15 16 const {push} = useRouter() 17 const route = useRoute(); 18 const {t} = useI18n() 19 20 const writeRef = ref<ComponentRef<typeof Form>>() 21 const loading = ref(false) 22 const taskId = computed(() => route.params.id as number); 23 const currentTask = ref<Nullable<ApiTask>>(null) 24 const activeTab = ref('pipeline') 25 const triggerIds = ref([]) 26 const conditionIds = ref([]) 27 const actionIds = ref([]) 28 29 const fetch = async () => { 30 loading.value = true 31 const res = await api.v1.automationServiceGetTask(taskId.value) 32 .catch(() => { 33 }) 34 .finally(() => { 35 loading.value = false 36 }) 37 if (res) { 38 let task = res.data as ApiTask 39 triggerIds.value = Object.assign([], task.triggerIds); 40 conditionIds.value = Object.assign([], task.conditionIds); 41 actionIds.value = Object.assign([], task.actionIds); 42 currentTask.value = task 43 } else { 44 currentTask.value = null 45 } 46 } 47 48 const prepareForSave = async () => { 49 const write = unref(writeRef) 50 const validate = await write?.elFormRef?.validate()?.catch(() => { 51 }) 52 const con = (await write?.getFormData()) as ApiTask; 53 if (validate) { 54 return { 55 name: con.name, 56 description: con.description, 57 enabled: con.enabled, 58 condition: con.condition, 59 triggerIds: triggerIds.value, 60 conditionIds: conditionIds.value, 61 actionIds: actionIds.value, 62 areaId: con.area?.id, 63 } 64 } 65 } 66 67 const save = async () => { 68 const body = await prepareForSave() 69 const res = await api.v1.automationServiceUpdateTask(taskId.value, body) 70 .catch(() => { 71 }) 72 .finally(() => { 73 74 }) 75 if (res) { 76 fetch() 77 ElMessage({ 78 title: t('Success'), 79 message: t('message.uploadSuccessfully'), 80 type: 'success', 81 duration: 2000 82 }) 83 } 84 } 85 86 const remove = async () => { 87 loading.value = true 88 const res = await api.v1.automationServiceDeleteTask(taskId.value) 89 .catch(() => { 90 }) 91 .finally(() => { 92 loading.value = false 93 }) 94 if (res) { 95 cancel() 96 } 97 } 98 99 const cancel = () => { 100 push('/automation/tasks') 101 } 102 103 fetch() 104 105 </script> 106 107 <template> 108 <ContentWrap> 109 110 <el-tabs class="demo-tabs" v-model="activeTab"> 111 <!-- main --> 112 <el-tab-pane :label="$t('automation.main')" name="main"> 113 <TaskForm ref="writeRef" :task="currentTask"/> 114 </el-tab-pane> 115 <!-- /main --> 116 117 <!-- telemetry --> 118 <el-tab-pane :label="$t('automation.telemetry')" name="telemetry" :lazy="true"> 119 <TaskTelemetry :task="currentTask"/> 120 </el-tab-pane> 121 <!-- /telemetry --> 122 123 <!-- pipeline --> 124 <el-tab-pane :label="$t('automation.tasks.pipeline')" name="pipeline" class="mt-20px"> 125 <el-timeline> 126 <el-timeline-item :timestamp="$t('automation.tasks.eventStart')" placement="top" type="primary"/> 127 128 <el-timeline-item :timestamp="$t('automation.tasks.triggers')" placement="top" type="primary" center> 129 <el-card> 130 <TriggersSearch v-model="triggerIds"/> 131 </el-card> 132 </el-timeline-item> 133 <el-timeline-item :timestamp="$t('automation.tasks.conditions')" placement="top" type="primary" center> 134 <el-card> 135 <ConditionsSearch v-model="conditionIds"/> 136 </el-card> 137 </el-timeline-item> 138 <el-timeline-item :timestamp="$t('automation.tasks.actions')" placement="top" type="primary" center> 139 <el-card> 140 <ActionsSearch v-model="actionIds"/> 141 </el-card> 142 </el-timeline-item> 143 <el-timeline-item :timestamp="$t('automation.tasks.eventEnd')" placement="top" type="success"/> 144 </el-timeline> 145 </el-tab-pane> 146 <!-- /pipeline --> 147 </el-tabs> 148 149 <div style="text-align: right"> 150 <ElButton type="primary" @click="save()"> 151 {{ t('main.save') }} 152 </ElButton> 153 154 <!-- <ElButton type="primary" @click="exportTask()">--> 155 <!-- <Icon icon="uil:file-export" class="mr-5px"/>--> 156 <!-- {{ t('main.export') }}--> 157 <!-- </ElButton>--> 158 159 <ElButton type="default" @click="fetch()"> 160 {{ t('main.loadFromServer') }} 161 </ElButton> 162 163 <ElButton type="default" @click="cancel()"> 164 {{ t('main.return') }} 165 </ElButton> 166 167 <ElPopconfirm 168 :confirm-button-text="$t('main.ok')" 169 :cancel-button-text="$t('main.no')" 170 width="250" 171 style="margin-left: 10px;" 172 :title="$t('main.are_you_sure_to_do_want_this?')" 173 @confirm="remove" 174 > 175 <template #reference> 176 <ElButton class="mr-10px" type="danger" plain> 177 <Icon icon="ep:delete" class="mr-5px"/> 178 {{ t('main.remove') }} 179 </ElButton> 180 </template> 181 </ElPopconfirm> 182 183 </div> 184 185 </ContentWrap> 186 187 </template> 188 189 <style lang="less" scoped> 190 191 </style>