2020-11-19 21:12:15 +08:00
|
|
|
import React, { useEffect, useRef, useState } from 'react';
|
2020-11-21 23:49:59 +08:00
|
|
|
import { Table as AntdTable, Card, Pagination } from 'antd';
|
2020-11-11 15:23:39 +08:00
|
|
|
import { Actions } from '@/components/actions';
|
|
|
|
import ViewFactory from '@/components/views';
|
2020-11-19 21:12:15 +08:00
|
|
|
import { useRequest } from 'umi';
|
2020-11-13 22:01:14 +08:00
|
|
|
import api from '@/api-client';
|
2020-11-19 21:12:15 +08:00
|
|
|
import { components, fields2columns } from './SortableTable';
|
2020-11-11 15:23:39 +08:00
|
|
|
|
2020-11-13 22:01:14 +08:00
|
|
|
export interface SimpleTableProps {
|
|
|
|
schema?: any;
|
|
|
|
activeTab?: any;
|
|
|
|
resourceName: string;
|
|
|
|
associatedName?: string;
|
|
|
|
associatedKey?: string;
|
|
|
|
[key: string]: any;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function SimpleTable(props: SimpleTableProps) {
|
2020-11-11 15:23:39 +08:00
|
|
|
console.log(props);
|
2020-11-13 22:01:14 +08:00
|
|
|
const {
|
|
|
|
activeTab = {},
|
|
|
|
pageInfo = {},
|
|
|
|
schema,
|
|
|
|
resourceName,
|
|
|
|
associatedName,
|
|
|
|
associatedKey,
|
|
|
|
} = props;
|
2020-12-15 14:32:56 +08:00
|
|
|
const { rowKey = 'id', name: viewName, fields = [], rowViewName, actions = [], paginated = true, defaultPerPage = 10 } = schema;
|
2020-11-13 22:01:14 +08:00
|
|
|
const { sourceKey = 'id' } = activeTab.field||{};
|
2020-11-11 15:23:39 +08:00
|
|
|
const drawerRef = useRef<any>();
|
2020-12-01 20:11:39 +08:00
|
|
|
const name = associatedName ? `${associatedName}.${resourceName}` : resourceName;
|
2020-12-12 16:32:18 +08:00
|
|
|
const { data, loading, pagination, mutate, refresh, params, run } = useRequest((params = {}) => {
|
2020-12-13 00:09:25 +08:00
|
|
|
const { current, pageSize, sorter, filter, ...restParams } = params;
|
2020-11-21 23:49:59 +08:00
|
|
|
return api.resource(name).list({
|
|
|
|
associatedKey,
|
|
|
|
page: paginated ? current : 1,
|
|
|
|
perPage: paginated ? pageSize : -1,
|
2020-12-12 16:32:18 +08:00
|
|
|
sorter,
|
2020-12-13 00:09:25 +08:00
|
|
|
filter,
|
2020-12-15 14:32:56 +08:00
|
|
|
viewName,
|
2020-11-21 23:49:59 +08:00
|
|
|
})
|
|
|
|
.then(({data = [], meta = {}}) => {
|
|
|
|
return {
|
|
|
|
data: {
|
|
|
|
list: data,
|
|
|
|
total: meta.count||data.length,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}, {
|
|
|
|
paginated,
|
2020-12-13 00:09:25 +08:00
|
|
|
defaultPageSize: defaultPerPage,
|
2020-11-21 23:49:59 +08:00
|
|
|
});
|
|
|
|
console.log(schema, data);
|
2020-11-19 21:12:15 +08:00
|
|
|
const [selectedRowKeys, setSelectedRowKeys] = useState([]);
|
|
|
|
const onChange = (selectedRowKeys: React.ReactText[]) => {
|
|
|
|
setSelectedRowKeys(selectedRowKeys);
|
|
|
|
}
|
|
|
|
const tableProps: any = {};
|
|
|
|
if (actions.length) {
|
|
|
|
tableProps.rowSelection = {
|
|
|
|
selectedRowKeys,
|
|
|
|
onChange,
|
|
|
|
}
|
|
|
|
}
|
2020-11-21 23:49:59 +08:00
|
|
|
console.log('rowViewName', {rowViewName})
|
2020-11-11 15:23:39 +08:00
|
|
|
return (
|
|
|
|
<Card bordered={false}>
|
2020-12-04 21:09:39 +08:00
|
|
|
<Actions
|
|
|
|
{...props}
|
|
|
|
style={{ marginBottom: 14 }}
|
|
|
|
actions={actions}
|
|
|
|
onFinish={() => {
|
|
|
|
refresh();
|
|
|
|
}}
|
2020-12-11 15:11:45 +08:00
|
|
|
onTrigger={{
|
2020-12-12 16:32:18 +08:00
|
|
|
async filter(values) {
|
|
|
|
console.log('filter', values);
|
|
|
|
// @ts-ignore
|
|
|
|
run({...params[0], filter: values.filter});
|
|
|
|
},
|
2020-12-11 15:11:45 +08:00
|
|
|
async destroy() {
|
|
|
|
await api.resource(name).destroy({
|
|
|
|
associatedKey,
|
|
|
|
filter: {
|
|
|
|
[`${rowKey}.in`]: selectedRowKeys,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
await refresh();
|
|
|
|
console.log('destroy.onTrigger', selectedRowKeys);
|
2020-12-12 16:32:18 +08:00
|
|
|
},
|
2020-12-11 15:11:45 +08:00
|
|
|
}}
|
2020-12-04 21:09:39 +08:00
|
|
|
/>
|
2020-11-19 21:12:15 +08:00
|
|
|
<ViewFactory
|
|
|
|
{...props}
|
2020-12-04 21:09:39 +08:00
|
|
|
mode={'update'}
|
2020-11-19 21:12:15 +08:00
|
|
|
viewName={rowViewName}
|
|
|
|
reference={drawerRef}
|
2020-12-07 17:25:36 +08:00
|
|
|
onFinish={() => {
|
|
|
|
refresh();
|
|
|
|
}}
|
2020-11-19 21:12:15 +08:00
|
|
|
/>
|
|
|
|
<AntdTable
|
|
|
|
rowKey={rowKey}
|
2020-11-21 23:49:59 +08:00
|
|
|
loading={loading}
|
|
|
|
columns={fields2columns(fields)}
|
|
|
|
dataSource={data?.list||(data as any)}
|
2020-12-12 16:32:18 +08:00
|
|
|
onChange={(pagination, filters, sorter, extra) => {
|
|
|
|
run({...params[0], sorter});
|
|
|
|
}}
|
2020-12-01 20:11:39 +08:00
|
|
|
components={components({
|
|
|
|
data,
|
|
|
|
mutate,
|
|
|
|
rowKey,
|
2020-12-13 00:09:25 +08:00
|
|
|
onMoved: async ({resourceKey, target}) => {
|
2020-12-01 20:11:39 +08:00
|
|
|
await api.resource(name).sort({
|
|
|
|
associatedKey,
|
|
|
|
resourceKey,
|
2020-12-13 00:09:25 +08:00
|
|
|
values: {
|
|
|
|
field: 'sort',
|
|
|
|
target,
|
|
|
|
},
|
2020-12-01 20:11:39 +08:00
|
|
|
});
|
|
|
|
await refresh();
|
2020-12-13 00:09:25 +08:00
|
|
|
console.log({resourceKey, target});
|
2020-12-01 20:11:39 +08:00
|
|
|
}
|
|
|
|
})}
|
2020-11-19 21:12:15 +08:00
|
|
|
onRow={(record) => ({
|
|
|
|
onClick: () => {
|
|
|
|
drawerRef.current.setVisible(true);
|
|
|
|
drawerRef.current.getData(record[rowKey]);
|
|
|
|
}
|
|
|
|
})}
|
2020-11-21 23:49:59 +08:00
|
|
|
pagination={false}
|
2020-11-19 21:12:15 +08:00
|
|
|
{...tableProps}
|
|
|
|
/>
|
2020-11-21 23:49:59 +08:00
|
|
|
{paginated && (
|
2020-11-29 16:26:53 +08:00
|
|
|
<div className={'table-pagination'}>
|
2020-11-21 23:49:59 +08:00
|
|
|
<Pagination {...pagination} showQuickJumper showSizeChanger size={'default'}/>
|
|
|
|
</div>
|
|
|
|
)}
|
2020-11-11 15:23:39 +08:00
|
|
|
</Card>
|
|
|
|
);
|
|
|
|
}
|