chore: add delete action in table fetch hook
This commit is contained in:
@@ -1,12 +1,16 @@
|
|||||||
import useTableRowSelection from '@/hooks/use-table-row-selection';
|
import useTableRowSelection from '@/hooks/use-table-row-selection';
|
||||||
import useTableSort from '@/hooks/use-table-sort';
|
import useTableSort from '@/hooks/use-table-sort';
|
||||||
|
import { handleBatchRequest } from '@/utils';
|
||||||
import _ from 'lodash';
|
import _ from 'lodash';
|
||||||
import { useEffect, useState } from 'react';
|
import { useEffect, useRef, useState } from 'react';
|
||||||
|
|
||||||
export default function useTableFetch<ListItem>(options: {
|
export default function useTableFetch<ListItem>(options: {
|
||||||
fetchAPI: (params: any) => Promise<Global.PageResponse<ListItem>>;
|
fetchAPI: (params: any) => Promise<Global.PageResponse<ListItem>>;
|
||||||
|
deleteAPI?: (id: number) => Promise<any>;
|
||||||
|
contentForDelete?: string;
|
||||||
}) {
|
}) {
|
||||||
const { fetchAPI } = options;
|
const { fetchAPI, deleteAPI, contentForDelete } = options;
|
||||||
|
const modalRef = useRef<any>(null);
|
||||||
const rowSelection = useTableRowSelection();
|
const rowSelection = useTableRowSelection();
|
||||||
const { sortOrder, setSortOrder } = useTableSort({
|
const { sortOrder, setSortOrder } = useTableSort({
|
||||||
defaultSortOrder: 'descend'
|
defaultSortOrder: 'descend'
|
||||||
@@ -91,6 +95,33 @@ export default function useTableFetch<ListItem>(options: {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const handleDelete = (row: ListItem & { name: string; id: number }) => {
|
||||||
|
modalRef.current.show({
|
||||||
|
content: contentForDelete,
|
||||||
|
operation: 'common.delete.single.confirm',
|
||||||
|
name: row.name,
|
||||||
|
async onOk() {
|
||||||
|
console.log('OK');
|
||||||
|
await deleteAPI?.(row.id);
|
||||||
|
fetchData();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleDeleteBatch = () => {
|
||||||
|
modalRef.current.show({
|
||||||
|
content: contentForDelete,
|
||||||
|
operation: 'common.delete.confirm',
|
||||||
|
selection: true,
|
||||||
|
async onOk() {
|
||||||
|
if (!deleteAPI) return;
|
||||||
|
await handleBatchRequest(rowSelection.selectedRowKeys, deleteAPI);
|
||||||
|
rowSelection.clearSelections();
|
||||||
|
fetchData();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
fetchData();
|
fetchData();
|
||||||
}, []);
|
}, []);
|
||||||
@@ -100,6 +131,9 @@ export default function useTableFetch<ListItem>(options: {
|
|||||||
rowSelection,
|
rowSelection,
|
||||||
sortOrder,
|
sortOrder,
|
||||||
queryParams,
|
queryParams,
|
||||||
|
modalRef,
|
||||||
|
handleDelete,
|
||||||
|
handleDeleteBatch,
|
||||||
fetchData,
|
fetchData,
|
||||||
handlePageChange,
|
handlePageChange,
|
||||||
handleTableChange,
|
handleTableChange,
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ import { PageAction } from '@/config';
|
|||||||
import HotKeys from '@/config/hotkeys';
|
import HotKeys from '@/config/hotkeys';
|
||||||
import type { PageActionType } from '@/config/types';
|
import type { PageActionType } from '@/config/types';
|
||||||
import useTableFetch from '@/hooks/use-table-fetch';
|
import useTableFetch from '@/hooks/use-table-fetch';
|
||||||
import { handleBatchRequest } from '@/utils';
|
|
||||||
import { DeleteOutlined, PlusOutlined, SyncOutlined } from '@ant-design/icons';
|
import { DeleteOutlined, PlusOutlined, SyncOutlined } from '@ant-design/icons';
|
||||||
import { PageContainer } from '@ant-design/pro-components';
|
import { PageContainer } from '@ant-design/pro-components';
|
||||||
import { useIntl } from '@umijs/max';
|
import { useIntl } from '@umijs/max';
|
||||||
@@ -19,7 +18,7 @@ import {
|
|||||||
Tooltip
|
Tooltip
|
||||||
} from 'antd';
|
} from 'antd';
|
||||||
import dayjs from 'dayjs';
|
import dayjs from 'dayjs';
|
||||||
import { useRef, useState } from 'react';
|
import { useState } from 'react';
|
||||||
import { useHotkeys } from 'react-hotkeys-hook';
|
import { useHotkeys } from 'react-hotkeys-hook';
|
||||||
import { deleteApisKey, queryApisKeysList } from './apis';
|
import { deleteApisKey, queryApisKeysList } from './apis';
|
||||||
import AddAPIKeyModal from './components/add-apikey';
|
import AddAPIKeyModal from './components/add-apikey';
|
||||||
@@ -33,17 +32,21 @@ const APIKeys: React.FC = () => {
|
|||||||
rowSelection,
|
rowSelection,
|
||||||
queryParams,
|
queryParams,
|
||||||
sortOrder,
|
sortOrder,
|
||||||
|
modalRef,
|
||||||
|
handleDelete,
|
||||||
|
handleDeleteBatch,
|
||||||
fetchData,
|
fetchData,
|
||||||
handlePageChange,
|
handlePageChange,
|
||||||
handleTableChange,
|
handleTableChange,
|
||||||
handleSearch,
|
handleSearch,
|
||||||
handleNameChange
|
handleNameChange
|
||||||
} = useTableFetch<ListItem>({
|
} = useTableFetch<ListItem>({
|
||||||
fetchAPI: queryApisKeysList
|
fetchAPI: queryApisKeysList,
|
||||||
|
deleteAPI: deleteApisKey,
|
||||||
|
contentForDelete: 'apikeys.table.apikeys'
|
||||||
});
|
});
|
||||||
|
|
||||||
const intl = useIntl();
|
const intl = useIntl();
|
||||||
const modalRef = useRef<any>(null);
|
|
||||||
|
|
||||||
const [openAddModal, setOpenAddModal] = useState(false);
|
const [openAddModal, setOpenAddModal] = useState(false);
|
||||||
const [action, setAction] = useState<PageActionType>(PageAction.CREATE);
|
const [action, setAction] = useState<PageActionType>(PageAction.CREATE);
|
||||||
@@ -67,32 +70,6 @@ const APIKeys: React.FC = () => {
|
|||||||
setOpenAddModal(false);
|
setOpenAddModal(false);
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleDelete = (row: ListItem) => {
|
|
||||||
modalRef.current.show({
|
|
||||||
content: 'apikeys.table.apikeys',
|
|
||||||
operation: 'common.delete.single.confirm',
|
|
||||||
name: row.name,
|
|
||||||
async onOk() {
|
|
||||||
console.log('OK');
|
|
||||||
await deleteApisKey(row.id);
|
|
||||||
fetchData();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleDeleteBatch = () => {
|
|
||||||
modalRef.current.show({
|
|
||||||
content: 'apikeys.table.apikeys',
|
|
||||||
operation: 'common.delete.confirm',
|
|
||||||
selection: true,
|
|
||||||
async onOk() {
|
|
||||||
await handleBatchRequest(rowSelection.selectedRowKeys, deleteApisKey);
|
|
||||||
rowSelection.clearSelections();
|
|
||||||
fetchData();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
const renderEmpty = (type?: string) => {
|
const renderEmpty = (type?: string) => {
|
||||||
if (type !== 'Table') return;
|
if (type !== 'Table') return;
|
||||||
if (
|
if (
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import InfoColumn from '@/components/simple-table/info-column';
|
|||||||
import StatusTag from '@/components/status-tag';
|
import StatusTag from '@/components/status-tag';
|
||||||
import Hotkeys from '@/config/hotkeys';
|
import Hotkeys from '@/config/hotkeys';
|
||||||
import useTableFetch from '@/hooks/use-table-fetch';
|
import useTableFetch from '@/hooks/use-table-fetch';
|
||||||
import { convertFileSize, handleBatchRequest } from '@/utils';
|
import { convertFileSize } from '@/utils';
|
||||||
import {
|
import {
|
||||||
DeleteOutlined,
|
DeleteOutlined,
|
||||||
EditOutlined,
|
EditOutlined,
|
||||||
@@ -27,7 +27,7 @@ import {
|
|||||||
message
|
message
|
||||||
} from 'antd';
|
} from 'antd';
|
||||||
import _ from 'lodash';
|
import _ from 'lodash';
|
||||||
import { useCallback, useRef, useState } from 'react';
|
import { useCallback, useState } from 'react';
|
||||||
import { useHotkeys } from 'react-hotkeys-hook';
|
import { useHotkeys } from 'react-hotkeys-hook';
|
||||||
import { deleteWorker, queryWorkersList, updateWorker } from '../apis';
|
import { deleteWorker, queryWorkersList, updateWorker } from '../apis';
|
||||||
import { WorkerStatusMapValue, status } from '../config';
|
import { WorkerStatusMapValue, status } from '../config';
|
||||||
@@ -80,25 +80,42 @@ const ActionList = [
|
|||||||
}
|
}
|
||||||
];
|
];
|
||||||
|
|
||||||
|
const formateUtilazation = (val1: number, val2: number): number => {
|
||||||
|
if (!val2 || !val1) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return _.round((val1 / val2) * 100, 0);
|
||||||
|
};
|
||||||
|
|
||||||
|
const calcStorage = (files: Filesystem[]) => {
|
||||||
|
const mountRoot = _.find(
|
||||||
|
files,
|
||||||
|
(item: Filesystem) => item.mount_point === '/'
|
||||||
|
);
|
||||||
|
return mountRoot ? formateUtilazation(mountRoot.used, mountRoot.total) : 0;
|
||||||
|
};
|
||||||
|
|
||||||
const Workers: React.FC = () => {
|
const Workers: React.FC = () => {
|
||||||
const {
|
const {
|
||||||
dataSource,
|
dataSource,
|
||||||
rowSelection,
|
rowSelection,
|
||||||
queryParams,
|
queryParams,
|
||||||
sortOrder,
|
modalRef,
|
||||||
|
handleDelete,
|
||||||
|
handleDeleteBatch,
|
||||||
fetchData,
|
fetchData,
|
||||||
handlePageChange,
|
handlePageChange,
|
||||||
handleTableChange,
|
handleTableChange,
|
||||||
handleSearch,
|
handleSearch,
|
||||||
handleNameChange
|
handleNameChange
|
||||||
} = useTableFetch<ListItem>({
|
} = useTableFetch<ListItem>({
|
||||||
fetchAPI: queryWorkersList
|
fetchAPI: queryWorkersList,
|
||||||
|
deleteAPI: deleteWorker,
|
||||||
|
contentForDelete: 'worker'
|
||||||
});
|
});
|
||||||
|
|
||||||
const modalRef = useRef<any>(null);
|
|
||||||
const intl = useIntl();
|
const intl = useIntl();
|
||||||
const [open, setOpen] = useState(false);
|
const [open, setOpen] = useState(false);
|
||||||
|
|
||||||
const [updateLabelsData, setUpdateLabelsData] = useState<{
|
const [updateLabelsData, setUpdateLabelsData] = useState<{
|
||||||
open: boolean;
|
open: boolean;
|
||||||
data: ListItem;
|
data: ListItem;
|
||||||
@@ -107,51 +124,10 @@ const Workers: React.FC = () => {
|
|||||||
data: {} as ListItem
|
data: {} as ListItem
|
||||||
});
|
});
|
||||||
|
|
||||||
const formateUtilazation = (val1: number, val2: number): number => {
|
|
||||||
if (!val2 || !val1) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
return _.round((val1 / val2) * 100, 0);
|
|
||||||
};
|
|
||||||
|
|
||||||
const calcStorage = (files: Filesystem[]) => {
|
|
||||||
const mountRoot = _.find(
|
|
||||||
files,
|
|
||||||
(item: Filesystem) => item.mount_point === '/'
|
|
||||||
);
|
|
||||||
return mountRoot ? formateUtilazation(mountRoot.used, mountRoot.total) : 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleAddWorker = () => {
|
const handleAddWorker = () => {
|
||||||
setOpen(true);
|
setOpen(true);
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleDelete = (row: ListItem) => {
|
|
||||||
modalRef.current.show({
|
|
||||||
content: 'worker',
|
|
||||||
operation: 'common.delete.single.confirm',
|
|
||||||
name: row.name,
|
|
||||||
async onOk() {
|
|
||||||
console.log('OK');
|
|
||||||
await deleteWorker(row.id);
|
|
||||||
fetchData();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleDeleteBatch = () => {
|
|
||||||
modalRef.current.show({
|
|
||||||
content: 'wokers',
|
|
||||||
operation: 'common.delete.confirm',
|
|
||||||
selection: true,
|
|
||||||
async onOk() {
|
|
||||||
await handleBatchRequest(rowSelection.selectedRowKeys, deleteWorker);
|
|
||||||
rowSelection.clearSelections();
|
|
||||||
fetchData();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleUpdateLabelsOk = useCallback(
|
const handleUpdateLabelsOk = useCallback(
|
||||||
async (values: Record<string, any>) => {
|
async (values: Record<string, any>) => {
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ export interface ListItem {
|
|||||||
is_admin: boolean;
|
is_admin: boolean;
|
||||||
full_name: string;
|
full_name: string;
|
||||||
id: number;
|
id: number;
|
||||||
|
username: string;
|
||||||
created_at: string;
|
created_at: string;
|
||||||
updated_at: string;
|
updated_at: string;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ import { PageAction } from '@/config';
|
|||||||
import HotKeys from '@/config/hotkeys';
|
import HotKeys from '@/config/hotkeys';
|
||||||
import type { PageActionType } from '@/config/types';
|
import type { PageActionType } from '@/config/types';
|
||||||
import useTableFetch from '@/hooks/use-table-fetch';
|
import useTableFetch from '@/hooks/use-table-fetch';
|
||||||
import { handleBatchRequest } from '@/utils';
|
|
||||||
import {
|
import {
|
||||||
DeleteOutlined,
|
DeleteOutlined,
|
||||||
EditOutlined,
|
EditOutlined,
|
||||||
@@ -27,7 +26,7 @@ import {
|
|||||||
message
|
message
|
||||||
} from 'antd';
|
} from 'antd';
|
||||||
import dayjs from 'dayjs';
|
import dayjs from 'dayjs';
|
||||||
import { useRef, useState } from 'react';
|
import { useState } from 'react';
|
||||||
import { useHotkeys } from 'react-hotkeys-hook';
|
import { useHotkeys } from 'react-hotkeys-hook';
|
||||||
import { createUser, deleteUser, queryUsersList, updateUser } from './apis';
|
import { createUser, deleteUser, queryUsersList, updateUser } from './apis';
|
||||||
import AddModal from './components/add-modal';
|
import AddModal from './components/add-modal';
|
||||||
@@ -56,17 +55,21 @@ const Users: React.FC = () => {
|
|||||||
rowSelection,
|
rowSelection,
|
||||||
queryParams,
|
queryParams,
|
||||||
sortOrder,
|
sortOrder,
|
||||||
|
modalRef,
|
||||||
|
handleDelete,
|
||||||
|
handleDeleteBatch,
|
||||||
fetchData,
|
fetchData,
|
||||||
handlePageChange,
|
handlePageChange,
|
||||||
handleTableChange,
|
handleTableChange,
|
||||||
handleSearch,
|
handleSearch,
|
||||||
handleNameChange
|
handleNameChange
|
||||||
} = useTableFetch<ListItem>({
|
} = useTableFetch<ListItem>({
|
||||||
fetchAPI: queryUsersList
|
fetchAPI: queryUsersList,
|
||||||
|
deleteAPI: deleteUser,
|
||||||
|
contentForDelete: 'users.table.user'
|
||||||
});
|
});
|
||||||
|
|
||||||
const intl = useIntl();
|
const intl = useIntl();
|
||||||
const modalRef = useRef<any>(null);
|
|
||||||
const [openAddModal, setOpenAddModal] = useState(false);
|
const [openAddModal, setOpenAddModal] = useState(false);
|
||||||
|
|
||||||
const [action, setAction] = useState<PageActionType>(PageAction.CREATE);
|
const [action, setAction] = useState<PageActionType>(PageAction.CREATE);
|
||||||
@@ -110,32 +113,6 @@ const Users: React.FC = () => {
|
|||||||
setOpenAddModal(false);
|
setOpenAddModal(false);
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleDelete = (row: ListItem) => {
|
|
||||||
modalRef.current.show({
|
|
||||||
content: 'users.table.user',
|
|
||||||
operation: 'common.delete.single.confirm',
|
|
||||||
name: row.name,
|
|
||||||
async onOk() {
|
|
||||||
console.log('OK');
|
|
||||||
await deleteUser(row.id);
|
|
||||||
fetchData();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleDeleteBatch = () => {
|
|
||||||
modalRef.current.show({
|
|
||||||
content: 'users.table.user',
|
|
||||||
operation: 'common.delete.confirm',
|
|
||||||
selection: true,
|
|
||||||
async onOk() {
|
|
||||||
await handleBatchRequest(rowSelection.selectedRowKeys, deleteUser);
|
|
||||||
rowSelection.clearSelections();
|
|
||||||
fetchData();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleEditUser = (row: ListItem) => {
|
const handleEditUser = (row: ListItem) => {
|
||||||
setCurrentData(row);
|
setCurrentData(row);
|
||||||
setOpenAddModal(true);
|
setOpenAddModal(true);
|
||||||
@@ -147,7 +124,7 @@ const Users: React.FC = () => {
|
|||||||
if (val === 'edit') {
|
if (val === 'edit') {
|
||||||
handleEditUser(row);
|
handleEditUser(row);
|
||||||
} else if (val === 'delete') {
|
} else if (val === 'delete') {
|
||||||
handleDelete(row);
|
handleDelete({ ...row, name: row.username });
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user