1
0
mirror of https://github.com/1Panel-dev/1Panel.git synced 2025-01-20 16:59:17 +08:00
1Panel/frontend/src/hooks/use-delete-data.ts
2022-09-07 14:03:24 +08:00

43 lines
1.5 KiB
Go

import { ElMessageBox, ElMessage } from 'element-plus';
import { HandleData } from './interface';
import i18n from '@/lang';
/**
* @description 删除操作使用
* @param {Function} api 操作数据接口的api方法(必传)
* @param {Object} params 携带的操作数据参数 {id,params}(必传)
* @param {String} message 提示信息(必传)
* @param {String} loading 页面loading
* @param {String} confirmType icon类型(不必传,默认为 warning)
* @return Promise
*/
export const useDeleteData = <P = any, R = any>(
api: (params: P) => Promise<R>,
params: Parameters<typeof api>[0],
message: string,
loading: boolean,
confirmType: HandleData.MessageType = 'error',
) => {
return new Promise((resolve, reject) => {
loading = true;
ElMessageBox.confirm(i18n.global.t(`${message}`) + '?', i18n.global.t('commons.msg.deleteTitle'), {
confirmButtonText: i18n.global.t('commons.button.confirm'),
cancelButtonText: i18n.global.t('commons.button.cancel'),
type: confirmType,
draggable: true,
})
.then(async () => {
const res = await api(params);
if (!res) return reject(false);
ElMessage({
type: 'success',
message: i18n.global.t('commons.msg.deleteSuccess'),
});
resolve(true);
})
.finally(() => {
loading = false;
});
});
};