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

346 lines
9.7 KiB
Vue
Raw Normal View History

<script setup lang="ts">
import { computed, nextTick, onMounted, reactive, ref, watch } from 'vue';
import { useFormRules, useNaiveForm } from '@/hooks/common/form';
import OperateDrawer from '@/components/common/operate-drawer.vue';
import { $t } from '@/locales';
2024-04-28 18:19:48 +08:00
import {
fetchAddNotify,
fetchEditNotify,
fetchGetJobList,
fetchGetNotifyRecipientList,
fetchGetRetrySceneList
} from '@/service/api';
import {
enableStatusNumberOptions,
jobNotifySceneOptions,
retryNotifySceneOptions,
systemTaskTypeOptions
} from '@/constants/business';
2024-04-28 18:19:48 +08:00
import { translateOptions } from '@/utils/common';
import SelectGroup from '@/components/common/select-group.vue';
defineOptions({
name: 'NotifyConfigOperateDrawer'
});
interface Props {
/** the type of operation */
operateType: NaiveUI.TableOperateType;
/** the edit row data */
rowData?: Api.NotifyConfig.NotifyConfig | null;
}
const notifyRecipientList = ref<CommonType.Option<number>[]>([]);
2024-04-28 18:19:48 +08:00
const retryScenes = ref<Api.RetryScene.Scene[]>([]);
const jobs = ref<Api.Job.Job[]>([]);
const props = defineProps<Props>();
interface Emits {
(e: 'submitted'): void;
}
const emit = defineEmits<Emits>();
const visible = defineModel<boolean>('visible', {
default: false
});
const options = defineModel<CommonType.Option<string | number>[]>('options', {
default: translateOptions(retryNotifySceneOptions)
});
const { formRef, validate, restoreValidation } = useNaiveForm();
const { defaultRequiredRule } = useFormRules();
const title = computed(() => {
const titles: Record<NaiveUI.TableOperateType, string> = {
add: $t('page.notifyConfig.addNotifyConfig'),
edit: $t('page.notifyConfig.editNotifyConfig')
};
return titles[props.operateType];
});
type Model = Pick<
Api.NotifyConfig.NotifyConfig,
| 'id'
| 'groupName'
| 'businessId'
| 'notifyRecipientIds'
| 'systemTaskType'
| 'notifyStatus'
| 'notifyScene'
| 'notifyThreshold'
| 'rateLimiterStatus'
| 'rateLimiterThreshold'
| 'description'
>;
onMounted(() => {
nextTick(() => {
getNotifyRecipientList();
});
});
async function getNotifyRecipientList() {
const res = await fetchGetNotifyRecipientList();
notifyRecipientList.value = res.data as CommonType.Option<number>[];
}
const model: Model = reactive(createDefaultModel());
function createDefaultModel(): Model {
return {
groupName: '',
businessId: '',
notifyRecipientIds: 0,
2024-04-20 22:36:09 +08:00
systemTaskType: 1,
notifyStatus: 1,
2024-04-28 18:19:48 +08:00
notifyScene: 1,
notifyThreshold: 0,
rateLimiterStatus: 0,
rateLimiterThreshold: 0,
description: ''
};
}
type RuleKey = Extract<
keyof Model,
| 'groupName'
| 'businessId'
| 'notifyRecipientIds'
| 'notifyStatus'
| 'notifyScene'
| 'rateLimiterStatus'
| 'notifyThreshold'
>;
const rules: Record<RuleKey, App.Global.FormRule> = {
groupName: defaultRequiredRule,
businessId: defaultRequiredRule,
notifyStatus: defaultRequiredRule,
notifyScene: defaultRequiredRule,
notifyRecipientIds: defaultRequiredRule,
rateLimiterStatus: defaultRequiredRule,
notifyThreshold: 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
if (props.operateType === 'add') {
const {
groupName,
businessId,
notifyRecipientIds,
systemTaskType,
notifyStatus,
notifyScene,
notifyThreshold,
rateLimiterStatus,
rateLimiterThreshold,
description
} = model;
2024-04-19 17:41:21 +08:00
const { error } = await fetchAddNotify({
groupName,
businessId,
notifyRecipientIds,
systemTaskType,
notifyStatus,
notifyScene,
notifyThreshold,
rateLimiterStatus,
rateLimiterThreshold,
description
});
2024-04-19 17:41:21 +08:00
if (error) return;
}
if (props.operateType === 'edit') {
const {
id,
groupName,
businessId,
notifyRecipientIds,
notifyStatus,
systemTaskType,
notifyScene,
notifyThreshold,
rateLimiterStatus,
rateLimiterThreshold,
description
} = model;
2024-04-19 17:41:21 +08:00
const { error } = await fetchEditNotify({
id,
groupName,
businessId,
notifyRecipientIds,
systemTaskType,
notifyStatus,
notifyScene,
notifyThreshold,
rateLimiterStatus,
rateLimiterThreshold,
description
});
2024-04-19 17:41:21 +08:00
if (error) return;
}
window.$message?.success($t('common.updateSuccess'));
closeDrawer();
emit('submitted');
}
2024-04-28 18:19:48 +08:00
async function systemTaskTypeChange(value: number) {
if (value === 1) {
const res = await fetchGetRetrySceneList({ groupName: model.groupName });
retryScenes.value = res.data as Api.RetryScene.Scene[];
options.value = translateOptions(retryNotifySceneOptions);
2024-04-28 18:19:48 +08:00
} else if (value === 3) {
const res = await fetchGetJobList({ groupName: model.groupName });
jobs.value = res.data as Api.Job.Job[];
options.value = translateOptions(jobNotifySceneOptions);
}
2024-04-28 18:19:48 +08:00
model.businessId = '';
model.notifyScene = 1;
}
function groupNameUpdate(groupName: string) {
handleUpdateModelWhenEdit();
model.groupName = groupName;
systemTaskTypeChange(1);
}
watch(visible, () => {
if (visible.value) {
handleUpdateModelWhenEdit();
restoreValidation();
}
});
</script>
<template>
<OperateDrawer v-model="visible" :title="title" @handle-submit="handleSubmit">
<NForm ref="formRef" :model="model" :rules="rules">
2024-04-21 09:47:13 +08:00
<NFormItem :label="$t('page.notifyConfig.groupName')" path="groupName">
2024-04-28 18:19:48 +08:00
<SelectGroup v-model:modelValue="model.groupName" @update:model-value="groupNameUpdate" />
</NFormItem>
<NFormItem :label="$t('page.notifyConfig.notifyStatus')" path="notifyStatus">
<NRadioGroup v-model:value="model.notifyStatus" name="notifyStatus">
<NSpace>
<NRadio
v-for="item in enableStatusNumberOptions"
:key="item.value"
:value="item.value"
:label="$t(item.label)"
/>
</NSpace>
</NRadioGroup>
</NFormItem>
<NFormItem :label="$t('page.notifyConfig.systemTaskType')" path="systemTaskType">
<NSelect
v-model:value="model.systemTaskType"
:placeholder="$t('page.notifyConfig.form.systemTaskType')"
:options="translateOptions(systemTaskTypeOptions)"
clearable
@update:value="systemTaskTypeChange"
/>
</NFormItem>
2024-04-28 18:19:48 +08:00
<NFormItem v-if="model.systemTaskType === 1" :label="$t('page.notifyConfig.retryScene')" path="businessId">
<NSelect
v-model:value="model.businessId"
:placeholder="$t('page.notifyConfig.form.groupName')"
:options="retryScenes"
label-field="sceneName"
value-field="sceneName"
clearable
/>
</NFormItem>
<NFormItem v-if="model.systemTaskType === 3" :label="$t('page.notifyConfig.job')" path="businessId">
<NSelect
v-model:value="model.businessId"
:placeholder="$t('page.notifyConfig.form.groupName')"
:options="jobs"
label-field="jobName"
value-field="id"
clearable
/>
</NFormItem>
<NFormItem v-if="model.systemTaskType === 4" :label="$t('page.notifyConfig.workflow')" path="businessId">
<NSelect v-model:value="model.businessId" :placeholder="$t('page.notifyConfig.form.groupName')" clearable />
</NFormItem>
<NFormItem :label="$t('page.notifyConfig.notifyScene')" path="notifyScene">
<NSelect
v-model:value="model.notifyScene"
:placeholder="$t('page.notifyConfig.form.notifyScene')"
:options="options"
clearable
/>
</NFormItem>
<NFormItem :label="$t('page.notifyConfig.notifyRecipient')" path="notifyRecipientIds">
<NSelect
v-model:value="model.notifyRecipientIds"
:placeholder="$t('page.notifyConfig.form.notifyRecipient')"
:options="notifyRecipientList"
clearable
multiple
/>
</NFormItem>
<NFormItem :label="$t('page.notifyConfig.rateLimiterStatus')" path="rateLimiterStatus">
2024-04-20 22:36:09 +08:00
<NRadioGroup v-model:value="model.rateLimiterStatus" name="rateLimiterStatus">
<NSpace>
<NRadio
v-for="item in enableStatusNumberOptions"
:key="item.value"
:value="item.value"
:label="$t(item.label)"
/>
</NSpace>
</NRadioGroup>
</NFormItem>
<NFormItem :label="$t('page.notifyConfig.rateLimiterThreshold')" path="rateLimiterThreshold">
<NInputNumber
v-model:value="model.rateLimiterThreshold"
:placeholder="$t('page.notifyConfig.form.rateLimiterThreshold')"
/>
</NFormItem>
<NFormItem :label="$t('page.notifyConfig.notifyThreshold')" path="notifyThreshold">
<NInputNumber
v-model:value="model.notifyThreshold"
:placeholder="$t('page.notifyConfig.form.notifyThreshold')"
/>
</NFormItem>
<NFormItem :label="$t('page.notifyConfig.description')" path="description">
<NInput
v-model:value="model.description"
type="textarea"
:placeholder="$t('page.notifyConfig.form.description')"
/>
</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>
</OperateDrawer>
</template>
<style scoped></style>