diff --git a/src/pages/BasicList/builder/ActionBuilder.tsx b/src/pages/BasicList/builder/ActionBuilder.tsx index d021b4d..096b5b5 100644 --- a/src/pages/BasicList/builder/ActionBuilder.tsx +++ b/src/pages/BasicList/builder/ActionBuilder.tsx @@ -4,8 +4,8 @@ import type { ButtonType } from 'antd/lib/button'; const ActionBuilder = ( actions: BasicListApi.Action[] | undefined, actionHandler: BasicListApi.ActionHandler, - loading: boolean, - record: any, + loading = false, + record = {}, ) => { return (actions || []).map((action) => { if (action.component === 'button') { diff --git a/src/pages/BasicList/component/Modal.tsx b/src/pages/BasicList/component/Modal.tsx index 2f47c26..377591b 100644 --- a/src/pages/BasicList/component/Modal.tsx +++ b/src/pages/BasicList/component/Modal.tsx @@ -12,7 +12,7 @@ const Modal = ({ modalUri, }: { modalVisible: boolean; - hideModal: () => void; + hideModal: (reload?: boolean) => void; modalUri: string; }) => { const [form] = Form.useForm(); @@ -46,7 +46,7 @@ const Modal = ({ content: data.message, key: 'process', }); - hideModal(); + hideModal(true); }, formatResult: (res: any) => { return res; @@ -73,7 +73,6 @@ const Modal = ({ }; const onFinish = (values: any) => { - console.log(values); request.run(values); }; @@ -83,6 +82,12 @@ const Modal = ({ form.setFieldsValue({ uri: action.uri, method: action.method }); form.submit(); break; + case 'cancel': + hideModal(); + break; + case 'reset': + form.resetFields(); + break; default: break; @@ -94,7 +99,9 @@ const Modal = ({ { + hideModal(); + }} footer={ActionBuilder(init?.data?.layout?.actions[0]?.data, actionHandler, request.loading)} maskClosable={false} > diff --git a/src/pages/BasicList/index.tsx b/src/pages/BasicList/index.tsx index b8b5784..95f331a 100644 --- a/src/pages/BasicList/index.tsx +++ b/src/pages/BasicList/index.tsx @@ -1,7 +1,8 @@ import React, { useState, useEffect } from 'react'; -import { Table, Row, Col, Card, Pagination, Space } from 'antd'; +import { Table, Row, Col, Card, Pagination, Space, Modal as AntdModal, message } from 'antd'; import { useRequest } from 'umi'; -import { PageContainer } from '@ant-design/pro-layout'; +import { PageContainer, FooterToolbar } from '@ant-design/pro-layout'; +import { ExclamationCircleOutlined } from '@ant-design/icons'; import ColumnBuilder from './builder/ColumnBuilder'; import ActionBuilder from './builder/ActionBuilder'; import Modal from './component/Modal'; @@ -13,25 +14,104 @@ const Index = () => { const [sortQuery, setSortQuery] = useState(''); const [modalVisible, setModalVisible] = useState(false); const [modalUri, setModalUri] = useState(''); + const [selectedRowKeys, setSelectedRowKeys] = useState([]); + const [selectedRows, setSelectedRows] = useState([]); + const [tableColumns, setTableColumns] = useState([]); + const { confirm } = AntdModal; + const init = useRequest<{ data: BasicListApi.ListData }>( `https://public-api-v2.aspirantzhang.com/api/admins?X-API-KEY=antd&page=${page}&per_page=${per_page}${sortQuery}`, ); + const request = useRequest( + (values: any) => { + message.loading({ content: 'Processing...', key: 'process', duration: 0 }); + const { uri, method, ...formValues } = values; + return { + url: `https://public-api-v2.aspirantzhang.com${uri}`, + method, + data: { + ...formValues, + 'X-API-KEY': 'antd', + }, + }; + }, + { + manual: true, + onSuccess: (data) => { + message.success({ + content: data.message, + key: 'process', + }); + }, + formatResult: (res: any) => { + return res; + }, + }, + ); + useEffect(() => { init.run(); }, [page, per_page, sortQuery]); + useEffect(() => { + if (init?.data?.layout?.tableColumn) { + setTableColumns(ColumnBuilder(init?.data?.layout?.tableColumn, actionHandler)); + } + }, [init?.data?.layout?.tableColumn]); + const searchLayout = () => {}; - const actionHandler = (action: BasicListApi.Action) => { + + const batchOverview = () => { + return ( + + ); + }; + + function actionHandler(action: BasicListApi.Action, record: any) { switch (action.action) { case 'modal': - setModalUri(action.uri as string); + setModalUri( + action.uri?.replace(/:\w+/g, (field) => { + return record[field.replace(':', '')]; + }) as string, + ); setModalVisible(true); break; + case 'reload': + init.run(); + break; + case 'delete': + confirm({ + title: 'Are you sure delete this task?', + icon: , + content: batchOverview(), + okText: 'Sure to Delete!!!', + okType: 'danger', + cancelText: 'Cancel', + onOk() { + return request.run({ + uri: action.uri, + method: action.method, + type: 'delete', + ids: selectedRowKeys, + }); + }, + onCancel() { + console.log('Cancel'); + }, + }); + break; default: break; } - }; + } const beforeTableLayout = () => { return ( @@ -39,7 +119,7 @@ const Index = () => { ... - {ActionBuilder(init?.data?.layout?.tableToolBar, actionHandler, false)} + {ActionBuilder(init?.data?.layout?.tableToolBar, actionHandler)} ); @@ -80,6 +160,29 @@ const Index = () => { ); }; + const hideModal = (reload = false) => { + setModalVisible(false); + if (reload) { + init.run(); + } + }; + + const rowSelection = { + selectedRowKeys, + onChange: (_selectedRowKeys: any, _selectedRows: any) => { + setSelectedRowKeys(_selectedRowKeys); + setSelectedRows(_selectedRows); + }, + }; + + const batchToolbar = () => { + return ( + selectedRowKeys.length > 0 && ( + {ActionBuilder(init?.data?.layout?.batchToolBar, actionHandler)} + ) + ); + }; + return ( {searchLayout()} @@ -88,20 +191,16 @@ const Index = () => {
{afterTableLayout()} - { - setModalVisible(false); - }} - modalUri={modalUri} - /> + + ); };