github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/static_source/admin/src/views/Entities/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 {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 Form from './components/Form.vue'
    13  import {ContentWrap} from "@/components/ContentWrap";
    14  import {Entity} from "@/views/Entities/components/types";
    15  import {ApiNewEntityRequest} from "@/api/stub";
    16  
    17  const {register, elFormRef, methods} = useForm()
    18  const {required} = useValidator()
    19  const appStore = useAppStore()
    20  const permissionStore = usePermissionStore()
    21  const {currentRoute, addRoute, push} = useRouter()
    22  const route = useRoute();
    23  const {wsCache} = useCache()
    24  const {t} = useI18n()
    25  
    26  const writeRef = ref<ComponentRef<typeof Form>>()
    27  const loading = ref(false)
    28  const currentRow = ref<Nullable<Entity>>(null)
    29  
    30  const save = async () => {
    31    const write = unref(writeRef)
    32    const validate = await write?.elFormRef?.validate()?.catch(() => {
    33    })
    34    if (validate) {
    35      loading.value = true
    36      const data = (await write?.getFormData()) as Entity
    37      const body = {
    38        name: data.name,
    39        pluginName: data.plugin?.name,
    40        description: data.description,
    41        areaId: data.area?.id,
    42        icon: data.icon,
    43        imageId: data.image?.id,
    44        autoLoad: data.autoLoad,
    45        restoreState: data.restoreState,
    46        scriptIds: data.scriptIds,
    47        parentId: data.parent?.id,
    48        tags: data.tags,
    49      } as ApiNewEntityRequest
    50      const res = await api.v1.entityServiceAddEntity(body)
    51          .catch(() => {
    52          })
    53          .finally(() => {
    54            loading.value = false
    55          })
    56      if (res) {
    57        push(`/entities/edit/${res.data.id}`)
    58      }
    59    }
    60  }
    61  
    62  const cancel = () => {
    63    push('/entities')
    64  }
    65  
    66  </script>
    67  
    68  <template>
    69    <ContentWrap>
    70      <Form ref="writeRef" :current-row="currentRow"/>
    71  
    72      <div style="text-align: right">
    73  
    74        <ElButton type="primary" @click="save()">
    75          {{ t('main.save') }}
    76        </ElButton>
    77  
    78        <ElButton type="default" @click="cancel()">
    79          {{ t('main.cancel') }}
    80        </ElButton>
    81  
    82      </div>
    83    </ContentWrap>
    84  
    85  </template>
    86  
    87  <style lang="less" scoped>
    88  
    89  </style>