auth-web/src/views/i18n/i18n-setting/utils/hooks.tsx

142 lines
3.4 KiB
TypeScript
Raw Normal View History

import { h, ref } from 'vue';
2024-09-29 16:52:09 +08:00
import { userI18nStore } from '@/store/i18n/i18n';
import { addDialog, closeDialog } from '@/components/BaseDialog/index';
2024-09-29 23:47:08 +08:00
import I18nDialog from '@/views/i18n/i18n-setting/i18n-dialog.vue';
import type { FormProps } from '@/views/i18n/i18n-setting/utils/types';
import { $t } from '@/plugins/i18n';
import { messageBox } from '@/utils/message';
2024-09-29 16:52:09 +08:00
2024-09-29 23:47:08 +08:00
export const formRef = ref();
2024-09-29 16:52:09 +08:00
const i18nStore = userI18nStore();
2024-10-01 17:08:00 +08:00
export const deleteIds = ref([]);
2024-09-29 16:52:09 +08:00
/**
* *
*/
2024-09-29 23:47:08 +08:00
export const onSearch = async () => {
2024-09-29 16:52:09 +08:00
i18nStore.loading = true;
await i18nStore.getI18nMangeList();
i18nStore.loading = false;
};
/**
2024-09-29 23:47:08 +08:00
* *
*
2024-09-29 16:52:09 +08:00
*/
2024-09-29 23:47:08 +08:00
export const onAdd = () => {
addDialog({
title: $t('add_multilingual'),
2024-09-29 23:47:08 +08:00
width: '30%',
props: { formInline: { keyName: '', translation: '', typeName: '' } },
2024-09-29 23:47:08 +08:00
draggable: true,
fullscreenIcon: true,
closeOnClickModal: false,
contentRenderer: () => h(I18nDialog, { ref: formRef }),
footerButtons: [
{
label: $t('cancel'),
text: true,
bg: true,
btnClick: ({ dialog: { options, index } }) => {
closeDialog(options, index);
},
},
{
label: $t('buttons.pureConfirm'),
type: 'primary',
text: true,
bg: true,
btnClick: ({ dialog: { options, index } }) => {
const form = options.props.formInline as FormProps;
formRef.value.ruleFormRef.validate(async (valid: any) => {
if (!valid) return;
2024-09-29 16:52:09 +08:00
const result = await i18nStore.addI18n(form);
if (!result) return;
closeDialog(options, index);
await onSearch();
});
},
},
{
label: $t('continue_adding'),
type: 'success',
text: true,
bg: true,
btnClick: ({ dialog: { options } }) => {
const form = options.props.formInline as FormProps;
formRef.value.ruleFormRef.validate(async (valid: any) => {
if (!valid) return;
const result = await i18nStore.addI18n(form);
if (!result) return;
await onSearch();
});
},
},
],
2024-09-29 23:47:08 +08:00
});
2024-09-29 16:52:09 +08:00
};
/**
2024-09-29 23:47:08 +08:00
* *
2024-09-29 16:52:09 +08:00
*/
2024-09-29 23:47:08 +08:00
export const onUpdate = (row: any) => {
const id = row.id;
2024-09-29 16:52:09 +08:00
2024-09-29 23:47:08 +08:00
addDialog({
title: $t('update_multilingual'),
2024-09-29 23:47:08 +08:00
width: '30%',
props: { formInline: { keyName: row.keyName, translation: row.translation, typeName: row.typeName } },
2024-09-29 23:47:08 +08:00
draggable: true,
fullscreenIcon: true,
closeOnClickModal: false,
contentRenderer: () => h(I18nDialog, { ref: formRef }),
beforeSure: (done, { options }) => {
const form = options.props.formInline as FormProps;
formRef.value.ruleFormRef.validate(async (valid: any) => {
if (!valid) return;
2024-09-29 16:52:09 +08:00
2024-09-29 23:47:08 +08:00
const result = await i18nStore.updateI18n({ ...form, id });
if (!result) return;
done();
await onSearch();
});
},
2024-09-29 16:52:09 +08:00
});
};
/**
* *
*/
export const onDelete = async (row: any) => {
2024-09-29 16:52:09 +08:00
const isConfirm = await messageBox({
message: $t('confirm_delete'),
title: $t('delete_warning'),
2024-09-29 16:52:09 +08:00
showMessage: false,
confirmMessage: $t('delete_success'),
cancelMessage: $t('cancel_delete'),
2024-09-29 16:52:09 +08:00
});
if (isConfirm) {
await i18nStore.deleteI18n([row.id]);
2024-09-29 23:47:08 +08:00
await onSearch();
2024-09-29 16:52:09 +08:00
}
};
2024-10-01 17:08:00 +08:00
/**
* *
*/
export const onDeleteBatch = async () => {
const isConfirm = await messageBox({
message: $t('confirm_delete'),
title: $t('delete_warning'),
showMessage: false,
confirmMessage: $t('delete_success'),
cancelMessage: $t('cancel_delete'),
});
if (isConfirm) {
await i18nStore.deleteI18n(deleteIds.value);
await onSearch();
}
};