2024-04-17 23:48:25 +08:00
|
|
|
<script setup lang="ts">
|
|
|
|
import { computed, reactive, watch } from 'vue';
|
|
|
|
import { useFormRules, useNaiveForm } from '@/hooks/common/form';
|
|
|
|
import { $t } from '@/locales';
|
|
|
|
|
|
|
|
defineOptions({
|
|
|
|
name: 'GroupConfigOperateDrawer'
|
|
|
|
});
|
|
|
|
|
|
|
|
interface Props {
|
|
|
|
/** the type of operation */
|
|
|
|
operateType: NaiveUI.TableOperateType;
|
|
|
|
/** the edit row data */
|
|
|
|
rowData?: Api.GroupConfig.GroupConfig | null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const props = defineProps<Props>();
|
|
|
|
|
|
|
|
interface Emits {
|
|
|
|
(e: 'submitted'): void;
|
|
|
|
}
|
|
|
|
|
|
|
|
const emit = defineEmits<Emits>();
|
|
|
|
|
|
|
|
const visible = defineModel<boolean>('visible', {
|
|
|
|
default: false
|
|
|
|
});
|
|
|
|
|
|
|
|
const { formRef, validate, restoreValidation } = useNaiveForm();
|
|
|
|
const { defaultRequiredRule } = useFormRules();
|
|
|
|
|
|
|
|
const title = computed(() => {
|
|
|
|
const titles: Record<NaiveUI.TableOperateType, string> = {
|
|
|
|
add: $t('page.groupConfig.addGroupConfig'),
|
|
|
|
edit: $t('page.groupConfig.editGroupConfig')
|
|
|
|
};
|
|
|
|
return titles[props.operateType];
|
|
|
|
});
|
|
|
|
|
|
|
|
type Model = Pick<Api.GroupConfig.GroupConfig, 'namespaceId'>;
|
|
|
|
|
|
|
|
const model: Model = reactive(createDefaultModel());
|
|
|
|
|
|
|
|
function createDefaultModel(): Model {
|
|
|
|
return {
|
|
|
|
namespaceId: ''
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-04-18 14:49:49 +08:00
|
|
|
type RuleKey = Extract<keyof Model, 'namespaceId'>;
|
2024-04-17 23:48:25 +08:00
|
|
|
|
|
|
|
const rules: Record<RuleKey, App.Global.FormRule> = {
|
|
|
|
namespaceId: defaultRequiredRule
|
|
|
|
};
|
|
|
|
|
|
|
|
function handleUpdateModelWhenEdit() {
|
|
|
|
if (props.operateType === 'add') {
|
|
|
|
Object.assign(model, createDefaultModel());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (props.operateType === 'edit' && props.rowData) {
|
|
|
|
Object.assign(model, props.rowData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function closeDrawer() {
|
|
|
|
visible.value = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function handleSubmit() {
|
|
|
|
await validate();
|
|
|
|
// request
|
|
|
|
window.$message?.success($t('common.updateSuccess'));
|
|
|
|
closeDrawer();
|
|
|
|
emit('submitted');
|
|
|
|
}
|
|
|
|
|
|
|
|
watch(visible, () => {
|
|
|
|
if (visible.value) {
|
|
|
|
handleUpdateModelWhenEdit();
|
|
|
|
restoreValidation();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<template>
|
2024-04-18 14:49:49 +08:00
|
|
|
<OperateDrawer v-model="visible" :title="title" @submitted="handleSubmit">
|
2024-04-18 09:47:26 +08:00
|
|
|
<NForm ref="formRef" :model="model" :rules="rules">
|
|
|
|
<NFormItem :label="$t('page.groupConfig.namespaceId')" path="namespaceId">
|
|
|
|
<NInput v-model:value="model.namespaceId" :placeholder="$t('page.groupConfig.form.namespaceId')" />
|
|
|
|
</NFormItem>
|
|
|
|
</NForm>
|
2024-04-18 14:49:49 +08:00
|
|
|
<template #footer>
|
|
|
|
<NSpace :size="16">
|
|
|
|
<NButton @click="closeDrawer">{{ $t('common.cancel') }}</NButton>
|
|
|
|
<NButton type="primary" @click="handleSubmit">{{ $t('common.save') }}</NButton>
|
|
|
|
</NSpace>
|
|
|
|
</template>
|
2024-04-18 09:47:26 +08:00
|
|
|
</OperateDrawer>
|
2024-04-17 23:48:25 +08:00
|
|
|
</template>
|
|
|
|
|
|
|
|
<style scoped></style>
|