|
@@ -1,25 +1,43 @@
|
1
|
|
-import React, { useRef, useCallback, useState } from 'react';
|
|
1
|
+import React, { useRef, useCallback, useMemo } from 'react';
|
2
|
2
|
import { connect, history } from 'umi';
|
3
|
3
|
import { PageContainer } from '@ant-design/pro-layout';
|
4
|
4
|
import ProTable from '@ant-design/pro-table';
|
5
|
|
-import { PlusOutlined, MenuOutlined } from '@ant-design/icons';
|
|
5
|
+import { PlusOutlined } from '@ant-design/icons';
|
6
|
6
|
import { Button, Popconfirm, Image, Space, notification } from 'antd';
|
7
|
|
-import { sortableContainer, sortableElement, sortableHandle } from 'react-sortable-hoc';
|
8
|
7
|
import request, { queryTable } from '@/utils/request';
|
9
|
|
-// import arrayMove from 'array-move';
|
10
|
|
-
|
11
|
|
-const DragHandle = sortableHandle(() => <MenuOutlined style={{ cursor: 'grab', color: '#999' }} />);
|
12
|
|
-
|
13
|
|
-const SortableItem = sortableElement((props) => <tr {...props} />);
|
14
|
|
-const SortableContainer = sortableContainer((props) => <tbody {...props} />);
|
|
8
|
+import { DraggableContainer, withDraggableRow, DragHandle } from '@/components/DragTable'
|
15
|
9
|
|
16
|
10
|
const typeList = { post: '科普' };
|
17
|
|
-// const stateList = { post: '科普' };
|
18
|
11
|
|
19
|
12
|
const BannerList = () => {
|
20
|
|
- const [, setLoading] = useState(false);
|
21
|
|
- const [dataSource, setDataSource] = useState([]);
|
22
|
|
- const ref = useRef();
|
|
13
|
+ const list = useRef([])
|
|
14
|
+ const actRef = useRef();
|
|
15
|
+
|
|
16
|
+ const handleDragEnd = useCallback(
|
|
17
|
+ ({oldIndex, newIndex}) => {
|
|
18
|
+ if (oldIndex === newIndex) {
|
|
19
|
+ return
|
|
20
|
+ }
|
|
21
|
+
|
|
22
|
+ const from = list.current[oldIndex]?.serialNo
|
|
23
|
+ const to = list.current[newIndex]?.serialNo
|
|
24
|
+
|
|
25
|
+ request('/sort/banner', { method: 'PUT', data: { from, to } }).then(() => {
|
|
26
|
+ actRef.current.reload()
|
|
27
|
+ }).catch((e) => {
|
|
28
|
+ notification.error({ message: e.message })
|
|
29
|
+ })
|
|
30
|
+ },
|
|
31
|
+ [],
|
|
32
|
+ );
|
|
33
|
+
|
|
34
|
+ const tableWrapper = useMemo(() => ({
|
|
35
|
+ body: {
|
|
36
|
+ wrapper: (props) => <DraggableContainer {...props} onSortEnd={handleDragEnd} />,
|
|
37
|
+ row: withDraggableRow(list, { rowKey: 'serialNo' }),
|
|
38
|
+ },
|
|
39
|
+ }), [handleDragEnd]);
|
|
40
|
+
|
23
|
41
|
const handleBannerClick = useCallback((id) => {
|
24
|
42
|
history.push(id ? `/cms/banner/edit?id=${id}` : '/cms/banner/edit');
|
25
|
43
|
}, []);
|
|
@@ -31,34 +49,27 @@ const BannerList = () => {
|
31
|
49
|
];
|
32
|
50
|
|
33
|
51
|
const deleteBannerClick = (id) => {
|
34
|
|
- setLoading(true);
|
35
|
|
-
|
36
|
52
|
request(`/banner/${id}`, { method: 'delete' })
|
37
|
53
|
.then(() => {
|
38
|
54
|
notification.success({ message: '删除成功' });
|
39
|
|
- ref.current.submit();
|
|
55
|
+ actRef.current.reload()
|
40
|
56
|
})
|
41
|
57
|
.catch((e) => {
|
42
|
|
- setLoading(false);
|
43
|
58
|
notification.error({ message: e.message });
|
44
|
59
|
});
|
45
|
60
|
};
|
46
|
61
|
|
47
|
62
|
const changeStatus = (row) => {
|
48
|
|
- setLoading(true);
|
49
|
|
-
|
50
|
63
|
request(`/banner/${row.serialNo}`, {
|
51
|
64
|
method: 'put',
|
52
|
65
|
data: { ...row, status: row.status === 1 ? 0 : 1 },
|
53
|
66
|
})
|
54
|
67
|
.then(() => {
|
55
|
68
|
notification.info({ message: '修改成功' });
|
56
|
|
- setLoading(false);
|
57
|
|
- ref.current.submit();
|
|
69
|
+ actRef.current.reload();
|
58
|
70
|
})
|
59
|
71
|
.catch((e) => {
|
60
|
72
|
notification.error({ message: e.message });
|
61
|
|
- setLoading(false);
|
62
|
73
|
return Promise.reject(e.message);
|
63
|
74
|
});
|
64
|
75
|
};
|
|
@@ -131,61 +142,26 @@ const BannerList = () => {
|
131
|
142
|
},
|
132
|
143
|
];
|
133
|
144
|
|
134
|
|
- const onSortEnd = ({ oldIndex, newIndex }) => {
|
135
|
|
- // const { dataSource } = this.state;
|
136
|
|
- console.log('Sorted items: ', dataSource, oldIndex, newIndex);
|
137
|
|
- if (oldIndex !== newIndex) {
|
138
|
|
- // const newData = arrayMove([].concat(dataSource), oldIndex, newIndex).filter((el) => !!el);
|
139
|
|
- // console.log('Sorted items: ', newData);
|
140
|
|
- // // this.setState({ dataSource: newData });
|
141
|
|
- // setDataSource(newData);
|
142
|
|
- }
|
143
|
|
- };
|
144
|
|
-
|
145
|
|
- const DraggableContainer = (props) => (
|
146
|
|
- <SortableContainer
|
147
|
|
- useDragHandle
|
148
|
|
- disableAutoscroll
|
149
|
|
- helperClass="row-dragging"
|
150
|
|
- onSortEnd={onSortEnd}
|
151
|
|
- {...props}
|
152
|
|
- />
|
153
|
|
- );
|
154
|
|
-
|
155
|
|
- const DraggableBodyRow = ({ className, style, ...restProps }) => {
|
156
|
|
- // const { dataSource } = this.state;
|
157
|
|
- // function findIndex base on Table rowKey props and should always be a right array index
|
158
|
|
- console.log(restProps, dataSource, 'restProps');
|
159
|
|
- const index = dataSource?.findIndex((x) => x.serialNo === restProps['data-row-key']);
|
160
|
|
- return <SortableItem index={index} {...restProps} />;
|
161
|
|
- };
|
162
|
|
-
|
163
|
|
- const getData = (params, sort, filter) => {
|
164
|
|
- return queryTable('/banner')(params, sort, filter).then((res) => {
|
165
|
|
- setDataSource(res.data);
|
166
|
|
- return res;
|
167
|
|
- });
|
168
|
|
- };
|
|
145
|
+ const requestWrapper = (...args) => {
|
|
146
|
+ return new Promise((resolve, reject) => {
|
|
147
|
+ queryTable('/banner')(...args).then((res) => {
|
|
148
|
+ resolve(res)
|
|
149
|
+ list.current = res.data
|
|
150
|
+ }).catch(reject)
|
|
151
|
+ })
|
|
152
|
+ }
|
169
|
153
|
|
170
|
154
|
return (
|
171
|
155
|
<PageContainer>
|
172
|
156
|
<ProTable
|
|
157
|
+ pagination={false}
|
173
|
158
|
columns={columns}
|
174
|
|
- dataSource={dataSource}
|
175
|
|
- request={getData}
|
176
|
|
- postData={(data) => data}
|
177
|
|
- formRef={ref}
|
|
159
|
+ actionRef={actRef}
|
|
160
|
+ request={requestWrapper}
|
178
|
161
|
rowKey="serialNo"
|
179
|
|
- headerTitle="科普列表"
|
180
|
|
- search={{
|
181
|
|
- labelWidth: '4em',
|
182
|
|
- }}
|
183
|
|
- components={{
|
184
|
|
- body: {
|
185
|
|
- wrapper: DraggableContainer,
|
186
|
|
- row: DraggableBodyRow,
|
187
|
|
- },
|
188
|
|
- }}
|
|
162
|
+ headerTitle="Banner列表"
|
|
163
|
+ search={{ labelWidth: '4em' }}
|
|
164
|
+ components={tableWrapper}
|
189
|
165
|
toolBarRender={() => actions}
|
190
|
166
|
/>
|
191
|
167
|
</PageContainer>
|