Files
admin-vben5/apps/web-antd/src/views/system/config/config-modal.vue

91 lines
2.1 KiB
Vue
Raw Normal View History

2024-09-11 20:23:44 +08:00
<script setup lang="ts">
import { computed, ref } from 'vue';
import { useVbenModal } from '@vben/common-ui';
import { $t } from '@vben/locales';
2024-09-25 14:46:02 +08:00
import { cloneDeep } from '@vben/utils';
2024-09-11 20:23:44 +08:00
2024-10-17 15:16:22 +08:00
import { useVbenForm } from '#/adapter/form';
2024-09-23 08:28:09 +08:00
import { configAdd, configInfo, configUpdate } from '#/api/system/config';
import { defaultFormValueGetter, useBeforeCloseDiff } from '#/utils/popup';
2024-09-12 08:25:38 +08:00
import { modalSchema } from './data';
2024-09-11 20:23:44 +08:00
const emit = defineEmits<{ reload: [] }>();
const isUpdate = ref(false);
const title = computed(() => {
return isUpdate.value ? $t('pages.common.edit') : $t('pages.common.add');
});
const [BasicForm, formApi] = useVbenForm({
2024-09-12 21:56:21 +08:00
commonConfig: {
labelWidth: 80,
},
2024-09-25 14:03:40 +08:00
schema: modalSchema(),
2024-09-11 20:23:44 +08:00
showDefaultActions: false,
});
const { onBeforeClose, updateInitialized, setSubmitted } = useBeforeCloseDiff({
initializedGetter: defaultFormValueGetter(formApi),
currentGetter: defaultFormValueGetter(formApi),
});
2024-09-11 21:32:30 +08:00
const [BasicModal, modalApi] = useVbenModal({
fullscreenButton: false,
onBeforeClose,
onClosed: handleClosed,
2024-09-11 21:32:30 +08:00
onConfirm: handleConfirm,
onOpenChange: async (isOpen) => {
if (!isOpen) {
return null;
}
2025-04-05 13:38:09 +08:00
try {
modalApi.lock(true);
2024-09-23 09:55:40 +08:00
2025-04-05 13:38:09 +08:00
const { id } = modalApi.getData() as { id?: number | string };
isUpdate.value = !!id;
2024-09-23 09:55:40 +08:00
2025-04-05 13:38:09 +08:00
if (isUpdate.value && id) {
const record = await configInfo(id);
await formApi.setValues(record);
}
await updateInitialized();
} catch (error) {
console.error(error);
} finally {
modalApi.lock(false);
2024-09-11 21:32:30 +08:00
}
},
});
2024-09-11 20:23:44 +08:00
async function handleConfirm() {
try {
2025-04-05 13:38:09 +08:00
modalApi.lock(true);
2024-09-11 21:32:30 +08:00
const { valid } = await formApi.validate();
2024-09-11 20:23:44 +08:00
if (!valid) {
return;
}
2024-09-25 14:46:02 +08:00
const data = cloneDeep(await formApi.getValues());
2024-09-11 21:32:30 +08:00
await (isUpdate.value ? configUpdate(data) : configAdd(data));
setSubmitted();
2024-09-11 20:23:44 +08:00
emit('reload');
modalApi.close();
2024-09-11 20:23:44 +08:00
} catch (error) {
console.error(error);
} finally {
2025-04-05 13:38:09 +08:00
modalApi.lock(false);
2024-09-11 20:23:44 +08:00
}
}
async function handleClosed() {
2024-09-11 20:23:44 +08:00
await formApi.resetForm();
}
</script>
<template>
<BasicModal :title="title" class="w-[550px]">
2024-09-11 20:23:44 +08:00
<BasicForm />
</BasicModal>
</template>