github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/static_source/admin/src/views/Automation/Actions/new.vue (about)

     1  <script setup lang="ts">
     2  import {ref, unref} from 'vue'
     3  import {useI18n} from '@/hooks/web/useI18n'
     4  import {ElButton, ElMessage} from 'element-plus'
     5  import {useRouter} from 'vue-router'
     6  import api from "@/api/api";
     7  import Form from './components/Form.vue'
     8  import {ApiAction, ApiNewActionRequest} from "@/api/stub";
     9  import {ContentWrap} from "@/components/ContentWrap";
    10  import ActionForm from "@/views/Automation/components/ActionForm.vue";
    11  
    12  const {push} = useRouter()
    13  const {t} = useI18n()
    14  
    15  const writeRef = ref<ComponentRef<typeof Form>>()
    16  const loading = ref(false)
    17  const currentRow = ref<Nullable<ApiAction>>(null)
    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 act = (await write?.getFormData()) as ApiAction;
    26      const data = {
    27        name: act.name,
    28        description: act.description,
    29        scriptId: act.script?.id,
    30        entityId: act.entity?.id,
    31        areaId: act.area?.id,
    32        entityActionName: act.entityActionName,
    33      } as ApiNewActionRequest
    34      const res = await api.v1.actionServiceAddAction(data)
    35        .catch(() => {
    36        })
    37        .finally(() => {
    38          loading.value = false
    39        })
    40      if (res) {
    41        ElMessage({
    42          title: t('Success'),
    43          message: t('message.createdSuccessfully'),
    44          type: 'success',
    45          duration: 2000
    46        })
    47  
    48        cancel()
    49      }
    50    }
    51  }
    52  
    53  const cancel = () => {
    54    push('/automation/actions')
    55  }
    56  
    57  </script>
    58  
    59  <template>
    60    <ContentWrap>
    61      <ActionForm ref="writeRef" :action="currentRow"/>
    62  
    63      <div style="text-align: right">
    64  
    65        <ElButton type="primary" @click="save()">
    66          {{ t('main.save') }}
    67        </ElButton>
    68  
    69        <ElButton type="default" @click="cancel()">
    70          {{ t('main.cancel') }}
    71        </ElButton>
    72  
    73      </div>
    74    </ContentWrap>
    75  
    76  </template>
    77  
    78  <style lang="less" scoped>
    79  
    80  </style>