Skip to content

Commit

Permalink
feat: course10
Browse files Browse the repository at this point in the history
  • Loading branch information
aspirantzhang committed Feb 28, 2021
1 parent 32de788 commit 4a8b2f0
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 20 deletions.
4 changes: 2 additions & 2 deletions src/pages/BasicList/builder/ActionBuilder.tsx
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import type { ButtonType } from 'antd/lib/button';
const ActionBuilder = ( const ActionBuilder = (
actions: BasicListApi.Action[] | undefined, actions: BasicListApi.Action[] | undefined,
actionHandler: BasicListApi.ActionHandler, actionHandler: BasicListApi.ActionHandler,
loading: boolean, loading = false,
record: any, record = {},
) => { ) => {
return (actions || []).map((action) => { return (actions || []).map((action) => {
if (action.component === 'button') { if (action.component === 'button') {
Expand Down
15 changes: 11 additions & 4 deletions src/pages/BasicList/component/Modal.tsx
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const Modal = ({
modalUri, modalUri,
}: { }: {
modalVisible: boolean; modalVisible: boolean;
hideModal: () => void; hideModal: (reload?: boolean) => void;
modalUri: string; modalUri: string;
}) => { }) => {
const [form] = Form.useForm(); const [form] = Form.useForm();
Expand Down Expand Up @@ -46,7 +46,7 @@ const Modal = ({
content: data.message, content: data.message,
key: 'process', key: 'process',
}); });
hideModal(); hideModal(true);
}, },
formatResult: (res: any) => { formatResult: (res: any) => {
return res; return res;
Expand All @@ -73,7 +73,6 @@ const Modal = ({
}; };


const onFinish = (values: any) => { const onFinish = (values: any) => {
console.log(values);
request.run(values); request.run(values);
}; };


Expand All @@ -83,6 +82,12 @@ const Modal = ({
form.setFieldsValue({ uri: action.uri, method: action.method }); form.setFieldsValue({ uri: action.uri, method: action.method });
form.submit(); form.submit();
break; break;
case 'cancel':
hideModal();
break;
case 'reset':
form.resetFields();
break;


default: default:
break; break;
Expand All @@ -94,7 +99,9 @@ const Modal = ({
<AntdModal <AntdModal
title={init?.data?.page?.title} title={init?.data?.page?.title}
visible={modalVisible} visible={modalVisible}
onCancel={hideModal} onCancel={() => {
hideModal();
}}
footer={ActionBuilder(init?.data?.layout?.actions[0]?.data, actionHandler, request.loading)} footer={ActionBuilder(init?.data?.layout?.actions[0]?.data, actionHandler, request.loading)}
maskClosable={false} maskClosable={false}
> >
Expand Down
127 changes: 113 additions & 14 deletions src/pages/BasicList/index.tsx
Original file line number Original file line Diff line number Diff line change
@@ -1,7 +1,8 @@
import React, { useState, useEffect } from 'react'; 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 { 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 ColumnBuilder from './builder/ColumnBuilder';
import ActionBuilder from './builder/ActionBuilder'; import ActionBuilder from './builder/ActionBuilder';
import Modal from './component/Modal'; import Modal from './component/Modal';
Expand All @@ -13,33 +14,112 @@ const Index = () => {
const [sortQuery, setSortQuery] = useState(''); const [sortQuery, setSortQuery] = useState('');
const [modalVisible, setModalVisible] = useState(false); const [modalVisible, setModalVisible] = useState(false);
const [modalUri, setModalUri] = useState(''); const [modalUri, setModalUri] = useState('');
const [selectedRowKeys, setSelectedRowKeys] = useState([]);
const [selectedRows, setSelectedRows] = useState([]);
const [tableColumns, setTableColumns] = useState<BasicListApi.Field[]>([]);
const { confirm } = AntdModal;

const init = useRequest<{ data: BasicListApi.ListData }>( 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}`, `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(() => { useEffect(() => {
init.run(); init.run();
}, [page, per_page, sortQuery]); }, [page, per_page, sortQuery]);


useEffect(() => {
if (init?.data?.layout?.tableColumn) {
setTableColumns(ColumnBuilder(init?.data?.layout?.tableColumn, actionHandler));
}
}, [init?.data?.layout?.tableColumn]);

const searchLayout = () => {}; const searchLayout = () => {};
const actionHandler = (action: BasicListApi.Action) => {
const batchOverview = () => {
return (
<Table
size="small"
rowKey="id"
columns={[tableColumns[0] || {}, tableColumns[1] || {}]}
dataSource={selectedRows}
pagination={false}
/>
);
};

function actionHandler(action: BasicListApi.Action, record: any) {
switch (action.action) { switch (action.action) {
case 'modal': case 'modal':
setModalUri(action.uri as string); setModalUri(
action.uri?.replace(/:\w+/g, (field) => {
return record[field.replace(':', '')];
}) as string,
);
setModalVisible(true); setModalVisible(true);
break; break;
case 'reload':
init.run();
break;
case 'delete':
confirm({
title: 'Are you sure delete this task?',
icon: <ExclamationCircleOutlined />,
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: default:
break; break;
} }
}; }
const beforeTableLayout = () => { const beforeTableLayout = () => {
return ( return (
<Row> <Row>
<Col xs={24} sm={12}> <Col xs={24} sm={12}>
... ...
</Col> </Col>
<Col xs={24} sm={12} className={styles.tableToolbar}> <Col xs={24} sm={12} className={styles.tableToolbar}>
<Space>{ActionBuilder(init?.data?.layout?.tableToolBar, actionHandler, false)}</Space> <Space>{ActionBuilder(init?.data?.layout?.tableToolBar, actionHandler)}</Space>
</Col> </Col>
</Row> </Row>
); );
Expand Down Expand Up @@ -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 && (
<Space>{ActionBuilder(init?.data?.layout?.batchToolBar, actionHandler)}</Space>
)
);
};

return ( return (
<PageContainer> <PageContainer>
{searchLayout()} {searchLayout()}
Expand All @@ -88,20 +191,16 @@ const Index = () => {
<Table <Table
rowKey="id" rowKey="id"
dataSource={init?.data?.dataSource} dataSource={init?.data?.dataSource}
columns={ColumnBuilder(init?.data?.layout?.tableColumn, actionHandler)} columns={tableColumns}
pagination={false} pagination={false}
loading={init.loading} loading={init.loading}
onChange={tableChangeHandler} onChange={tableChangeHandler}
rowSelection={rowSelection}
/> />
{afterTableLayout()} {afterTableLayout()}
</Card> </Card>
<Modal <Modal modalVisible={modalVisible} hideModal={hideModal} modalUri={modalUri} />
modalVisible={modalVisible} <FooterToolbar extra={batchToolbar()} />
hideModal={() => {
setModalVisible(false);
}}
modalUri={modalUri}
/>
</PageContainer> </PageContainer>
); );
}; };
Expand Down

0 comments on commit 4a8b2f0

Please sign in to comment.