gtsoft-snail-job-admin/src/views/notify/recipient/modules/notify-recipient-operate-drawer.vue

139 lines
4.2 KiB
Vue
Raw Normal View History

2024-04-17 23:20:57 +08:00
<script setup lang="ts">
2024-04-18 23:33:34 +08:00
import { computed, ref, watch } from 'vue';
import { useNaiveForm } from '@/hooks/common/form';
2024-04-17 23:20:57 +08:00
import OperateDrawer from '@/components/common/operate-drawer.vue';
import { $t } from '@/locales';
2024-04-18 23:33:34 +08:00
import { fetchAddNotifyRecipient, fetchEditNotifyRecipient } from '@/service/api';
import DingDingForm from './dingding-form.vue';
import LarkForm from './lark-form.vue';
import EmailForm from './email-form.vue';
import WeComForm from './wecom-form.vue';
import WebhookForm from './webhook-form.vue';
2024-04-17 23:20:57 +08:00
defineOptions({
name: 'NotifyRecipientOperateDrawer'
});
interface Props {
/** the type of operation */
operateType: NaiveUI.TableOperateType;
/** the edit row data */
rowData?: Api.NotifyRecipient.NotifyRecipient | null;
}
const props = defineProps<Props>();
interface Emits {
(e: 'submitted'): void;
}
const emit = defineEmits<Emits>();
const visible = defineModel<boolean>('visible', {
default: false
});
2024-04-19 17:41:21 +08:00
const notifyTabPane = defineModel<Api.NotifyRecipient.AlarmType>('notifyTabPane', {
default: 1
2024-04-18 23:33:34 +08:00
});
2024-04-19 17:41:21 +08:00
const { formRef, validate, restoreValidation } = useNaiveForm();
2024-04-17 23:20:57 +08:00
const title = computed(() => {
const titles: Record<NaiveUI.TableOperateType, string> = {
add: $t('page.notifyRecipient.addNotifyRecipient'),
edit: $t('page.notifyRecipient.editNotifyRecipient')
};
return titles[props.operateType];
});
2024-04-19 17:41:21 +08:00
type Model = Pick<
Api.NotifyRecipient.NotifyRecipient,
'id' | 'recipientName' | 'notifyType' | 'notifyAttribute' | 'description'
>;
const model = ref<Model>(createDefaultModel());
function createDefaultModel(): Model {
return {
recipientName: '',
notifyType: notifyTabPane.value!,
notifyAttribute: '{}',
description: ''
};
}
2024-04-17 23:20:57 +08:00
function handleUpdateModelWhenEdit() {
if (props.operateType === 'add') {
2024-04-19 17:41:21 +08:00
model.value = createDefaultModel();
notifyTabPane.value = 1;
2024-04-17 23:20:57 +08:00
return;
}
if (props.operateType === 'edit' && props.rowData) {
2024-04-19 17:41:21 +08:00
model.value = props.rowData;
notifyTabPane.value = props.rowData.notifyType;
2024-04-17 23:20:57 +08:00
}
}
async function handleSubmit() {
2024-04-19 17:41:21 +08:00
await validate();
2024-04-17 23:20:57 +08:00
// request
if (props.operateType === 'add') {
2024-04-19 17:41:21 +08:00
const { recipientName, notifyAttribute, notifyType, description } = model.value;
const { error } = await fetchAddNotifyRecipient({ recipientName, notifyAttribute, notifyType, description });
if (error) return;
window.$message?.success($t('common.addSuccess'));
2024-04-17 23:20:57 +08:00
}
if (props.operateType === 'edit') {
2024-04-19 17:41:21 +08:00
const { id, recipientName, notifyAttribute, notifyType, description } = model.value;
const { error } = await fetchEditNotifyRecipient({ id, recipientName, notifyAttribute, notifyType, description });
if (error) return;
window.$message?.success($t('common.updateSuccess'));
2024-04-17 23:20:57 +08:00
}
2024-04-19 17:41:21 +08:00
closeDrawer();
2024-04-17 23:20:57 +08:00
emit('submitted');
}
function closeDrawer() {
visible.value = false;
}
2024-04-17 23:20:57 +08:00
watch(visible, () => {
if (visible.value) {
handleUpdateModelWhenEdit();
restoreValidation();
}
});
</script>
<template>
<OperateDrawer v-model="visible" :min-size="480" :title="title">
2024-04-19 17:41:21 +08:00
<NTabs v-model:value="notifyTabPane" type="segment" animated>
<NTabPane :name="1" tab="钉钉" :disabled="notifyTabPane !== 1 && props.operateType === 'edit'">
2024-04-19 17:41:21 +08:00
<DingDingForm ref="formRef" v-model:value="model" />
</NTabPane>
<NTabPane :name="2" tab="邮箱" :disabled="notifyTabPane !== 2 && props.operateType === 'edit'">
2024-04-19 17:41:21 +08:00
<EmailForm ref="formRef" v-model:value="model" />
</NTabPane>
<NTabPane :name="3" tab="企业微信" :disabled="notifyTabPane !== 3 && props.operateType === 'edit'">
2024-04-19 17:41:21 +08:00
<WeComForm ref="formRef" v-model:value="model" />
</NTabPane>
<NTabPane :name="4" tab="飞书" :disabled="notifyTabPane !== 4 && props.operateType === 'edit'">
2024-04-19 17:41:21 +08:00
<LarkForm ref="formRef" v-model:value="model" />
</NTabPane>
<NTabPane :name="5" tab="Webhook" :disabled="notifyTabPane !== 5 && props.operateType === 'edit'">
<WebhookForm ref="formRef" v-model:value="model" />
</NTabPane>
</NTabs>
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-17 23:20:57 +08:00
</OperateDrawer>
</template>
<style scoped></style>