reCoreD-UI/web/src/components/domains/DomainEditModal.vue

217 lines
7.1 KiB
Vue
Raw Normal View History

2024-04-08 09:30:25 +00:00
<template>
2024-04-08 16:18:18 +00:00
<NModal :mask-closable="false" :show="show">
<NCard style="width: 640px" role="dialog" aria-modal="true">
<template #header>
<span v-if="!domain || !domain.id || domain.id < 1">{{ t('common.new') }}</span><span v-else>{{
t('common.edit')
2024-04-08 16:19:03 +00:00
}}</span><span>{{
2024-04-08 16:18:18 +00:00
t('domains._') }}</span>
</template>
<template #header-extra></template>
2024-04-08 09:30:25 +00:00
2024-04-08 16:18:18 +00:00
<NForm :model="domain" :rules="rules">
<NFormItem :label="t('domains._')" path="domain_name">
<NInput placeholder="example.com" v-model:value="domain.domain_name" @input="easyInput"
@keydown.enter.prevent />
</NFormItem>
<NFormItem :label="t('domains.form.mainDNS')" path="main_dns">
<NInput placeholder="ns1.example.com" v-model:value="domain.main_dns" @keydown.enter.prevent />
</NFormItem>
<NFormItem :label="t('domains.form.adminMail')" path="admin_email">
<NInput placeholder="admin@example.com" v-model:value="domain.admin_email" @keydown.enter.prevent />
</NFormItem>
</NForm>
<NForm :model="domain" :rules="rules" inline>
<NFormItem :label="t('records.refresh')" path="refresh_interval">
<NInputNumber v-model:value="domain.refresh_interval" :show-button="false">
<template #suffix>
2024-04-10 03:00:47 +00:00
{{ t('common.unitForSecond') }}
2024-04-08 16:18:18 +00:00
</template>
</NInputNumber>
</NFormItem>
<NFormItem :label="t('records.retry')" path="retry_interval">
<NInputNumber v-model:value="domain.retry_interval" :show-button="false">
<template #suffix>
2024-04-10 03:00:47 +00:00
{{ t('common.unitForSecond') }}
2024-04-08 16:18:18 +00:00
</template>
</NInputNumber>
</NFormItem>
<NFormItem :label="t('records.expire')" path="expiry_period">
<NInputNumber v-model:value="domain.expiry_period" :show-button="false">
<template #suffix>
2024-04-10 03:00:47 +00:00
{{ t('common.unitForSecond') }}
2024-04-08 16:18:18 +00:00
</template>
</NInputNumber>
</NFormItem>
<NFormItem :label="t('records.ttl')" path="negative_ttl">
<NInputNumber v-model:value="domain.negative_ttl" :show-button="false">
<template #suffix>
2024-04-10 03:00:47 +00:00
{{ t('common.unitForSecond') }}
2024-04-08 16:18:18 +00:00
</template>
</NInputNumber>
</NFormItem>
</NForm>
2024-04-08 09:30:25 +00:00
2024-04-08 16:18:18 +00:00
<template #action>
<NFlex justify="end">
<NButton size="small" @click="show = false">
2024-04-08 09:30:25 +00:00
<template #icon>
<NIcon>
2024-04-08 16:18:18 +00:00
<Times />
2024-04-08 09:30:25 +00:00
</NIcon>
</template>
2024-04-08 16:18:18 +00:00
{{ t('common.cancel') }}
2024-04-08 09:30:25 +00:00
</NButton>
2024-04-10 03:00:47 +00:00
<NButton size="small" type="primary" :disabled="invalidData !== allFlags" :loading="loading"
@click="confirm" attr-type="submit">
<template #icon>
<NIcon>
<Check />
</NIcon>
</template>
{{ t('common.confirm') }}
</NButton>
2024-04-08 16:18:18 +00:00
</NFlex>
</template>
</NCard>
2024-04-08 09:30:25 +00:00
</NModal>
</template>
<script setup lang="ts">
2024-04-08 16:18:18 +00:00
import { getErrorInfo } from '@/apis/api';
2024-04-08 09:30:25 +00:00
import { useDomainStore, type Domain } from '@/stores/domains';
import { Check, Times } from '@vicons/fa';
import {
NModal,
2024-04-08 16:18:18 +00:00
NCard,
2024-04-08 09:30:25 +00:00
NForm,
NFormItem,
2024-04-08 16:18:18 +00:00
NFlex,
NIcon,
2024-04-08 09:30:25 +00:00
NButton,
NInput,
NInputNumber,
useNotification,
type FormRules,
type FormItemRule
} from 'naive-ui'
2024-04-10 03:00:47 +00:00
import { ref } from 'vue';
2024-04-08 09:30:25 +00:00
import { useI18n } from 'vue-i18n';
2024-04-09 00:25:01 +00:00
const enum validFlags {
domainNameValid = 1,
mainNsValid = domainNameValid << 1,
adminEmailValid = mainNsValid << 1
}
2024-04-10 03:00:47 +00:00
const allFlags = validFlags.adminEmailValid | validFlags.mainNsValid | validFlags.domainNameValid
2024-04-09 00:25:01 +00:00
2024-04-08 09:30:25 +00:00
const { t } = useI18n()
const props = defineProps<{
domain: Domain
}>()
const show = defineModel<boolean>('show', { default: false })
2024-04-08 16:18:18 +00:00
const loading = ref(false)
2024-04-09 00:25:01 +00:00
const invalidData = ref(0)
2024-04-08 16:18:18 +00:00
const rules = {
domain_name: [{
2024-04-08 09:30:25 +00:00
required: true,
trigger: 'blur',
2024-04-08 16:18:18 +00:00
validator: (_rule: FormItemRule, value: string) => {
2024-04-08 09:30:25 +00:00
return validate(
value,
2024-04-09 02:06:47 +00:00
/^([\w-]+\.)+[\w-]+$/,
2024-04-09 00:25:01 +00:00
'domains.errors.domainName',
validFlags.domainNameValid
2024-04-08 09:30:25 +00:00
)
}
2024-04-08 16:18:18 +00:00
}],
main_dns: [{
2024-04-08 09:30:25 +00:00
required: true,
trigger: 'blur',
2024-04-08 16:18:18 +00:00
validator: (_rule: FormItemRule, value: string) => {
2024-04-08 09:30:25 +00:00
return validate(
value,
2024-04-09 02:06:47 +00:00
/^([\w-]+\.)+[\w-]+$/,
2024-04-09 00:25:01 +00:00
'domains.errors.domainName',
validFlags.mainNsValid,
2024-04-08 09:30:25 +00:00
)
}
2024-04-08 16:18:18 +00:00
}],
admin_email: [{
2024-04-08 09:30:25 +00:00
required: true,
trigger: 'blur',
2024-04-08 16:18:18 +00:00
validator: (_rule: FormItemRule, value: string) => {
2024-04-08 09:30:25 +00:00
return validate(
value,
2024-04-09 02:06:47 +00:00
/^[\w-.]+@([\w-]+\.)+[\w-]+$/,
2024-04-09 00:25:01 +00:00
'domains.errors.mail',
validFlags.adminEmailValid
2024-04-08 09:30:25 +00:00
)
}
2024-04-08 16:18:18 +00:00
}],
refresh_interval: [{
2024-04-08 09:30:25 +00:00
required: true,
trigger: 'blur',
2024-04-08 16:18:18 +00:00
type: 'number',
}],
retry_interval: [{
2024-04-08 09:30:25 +00:00
required: true,
trigger: 'blur',
2024-04-08 16:18:18 +00:00
type: 'number',
}],
expiry_period: [{
2024-04-08 09:30:25 +00:00
required: true,
trigger: 'blur',
2024-04-08 16:18:18 +00:00
type: 'number',
}],
negative_ttl: [{
2024-04-08 09:30:25 +00:00
required: true,
trigger: 'blur',
2024-04-10 03:00:47 +00:00
type: 'number'
2024-04-08 16:18:18 +00:00
}]
} as FormRules
2024-04-08 09:30:25 +00:00
const domainStore = useDomainStore()
const notification = useNotification()
async function confirm() {
2024-04-08 16:18:18 +00:00
loading.value = true;
try {
if (!props.domain.id || props.domain.id < 1) {
await domainStore.addDomain(props.domain)
} else {
await domainStore.updateDomain(props.domain)
}
show.value = false
} catch (e) {
const msg = getErrorInfo(e)
notification.error(msg)
2024-04-09 00:30:32 +00:00
console.error(e)
2024-04-08 16:18:18 +00:00
}
loading.value = false
2024-04-08 09:30:25 +00:00
}
2024-04-09 00:25:01 +00:00
function validate(value: string, reg: RegExp, msg: string, flag: validFlags): Promise<void> {
2024-04-10 03:00:47 +00:00
return new Promise<void>((resolve, reject) => {
2024-04-08 16:18:18 +00:00
if (!value) {
2024-04-09 00:25:01 +00:00
invalidData.value &= ~flag
2024-04-08 16:18:18 +00:00
reject(Error(t('common.mandatory')))
} else if (!reg.test(value)) {
2024-04-09 00:25:01 +00:00
invalidData.value &= ~flag
2024-04-08 16:18:18 +00:00
reject(Error(t(msg)))
} else {
2024-04-09 00:25:01 +00:00
invalidData.value |= flag
2024-04-08 16:18:18 +00:00
resolve()
}
})
2024-04-08 09:30:25 +00:00
}
function easyInput(domain: string) {
props.domain.admin_email = `admin@${domain}`
props.domain.main_dns = `ns1.${domain}`
}
</script>