github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/static_source/admin/src/views/Automation/Triggers/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 {ApiNewTriggerRequest, ApiTrigger} from "@/api/stub";
     9  import {ContentWrap} from "@/components/ContentWrap";
    10  import TriggerForm from "@/views/Automation/components/TriggerForm.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<ApiTrigger>>(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 tr = (await write?.getFormData()) as ApiTrigger;
    26      let data = {
    27        name: tr.name,
    28        description: tr.description,
    29        entityIds: tr.entityIds || [],
    30        scriptId: tr.script?.id || null,
    31        areaId: tr.area?.id || null,
    32        pluginName: tr.pluginName,
    33        attributes: {},
    34        enabled: tr.enabled,
    35      } as ApiNewTriggerRequest
    36      if (tr.pluginName === 'time') {
    37        data.attributes['cron'] = {
    38          string: tr?.timePluginOptions || '',
    39          type: "STRING",
    40        }
    41      }
    42      if (tr.pluginName === 'system') {
    43        data.attributes['system'] = {
    44          string: tr?.systemPluginOptions || '',
    45          type: "STRING",
    46        }
    47      }
    48      const res = await api.v1.triggerServiceAddTrigger(data)
    49        .catch(() => {
    50        })
    51        .finally(() => {
    52          loading.value = false
    53        })
    54      if (res) {
    55        ElMessage({
    56          title: t('Success'),
    57          message: t('message.createdSuccessfully'),
    58          type: 'success',
    59          duration: 2000
    60        })
    61  
    62        cancel()
    63      }
    64    }
    65  }
    66  
    67  const cancel = () => {
    68    push('/automation/triggers')
    69  }
    70  
    71  </script>
    72  
    73  <template>
    74    <ContentWrap>
    75      <TriggerForm ref="writeRef" :trigger="currentRow"/>
    76  
    77      <div style="text-align: right">
    78  
    79        <ElButton type="primary" @click="save()">
    80          {{ t('main.save') }}
    81        </ElButton>
    82  
    83        <ElButton type="default" @click="cancel()">
    84          {{ t('main.cancel') }}
    85        </ElButton>
    86  
    87      </div>
    88    </ContentWrap>
    89  
    90  </template>
    91  
    92  <style lang="less" scoped>
    93  
    94  </style>