|
@@ -1,43 +1,44 @@
|
1
|
|
-import React, { useRef, useCallback, useState, useMemo } from 'react';
|
|
1
|
+import React, { useRef, useCallback, useMemo, useState } 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
|
5
|
import { PlusOutlined, MenuOutlined } from '@ant-design/icons';
|
6
|
6
|
import { Button, Popconfirm, Image, Space, notification } from 'antd';
|
7
|
7
|
import request, { queryTable } from '@/utils/request';
|
8
|
|
-import DnDProvider from '@/components/DndTr/Provider';
|
9
|
|
-import DndTr from '@/components/DndTr';
|
|
8
|
+import { DraggableContainer, withDraggableRow, DragHandle } from '@/components/DragTable'
|
10
|
9
|
|
11
|
10
|
const typeList = { post: '科普' };
|
12
|
11
|
|
13
|
12
|
const HotList = () => {
|
14
|
|
- const [forceUpdate, setForceUpdate] = useState(1)
|
|
13
|
+ const list = useRef([])
|
15
|
14
|
const actRef = useRef()
|
16
|
15
|
|
17
|
|
- // eslint-disable-next-line react-hooks/exhaustive-deps
|
18
|
|
- const RowWrapper = useMemo(() => DndTr, [forceUpdate])
|
|
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
|
+ console.log('----------->', from, to)
|
|
26
|
+
|
|
27
|
+ request('/sort/topic', { method: 'PUT', data: { from, to } }).then(() => {
|
|
28
|
+ actRef.current.reload()
|
|
29
|
+ }).catch((e) => {
|
|
30
|
+ notification.error({ message: e.message })
|
|
31
|
+ })
|
|
32
|
+ },
|
|
33
|
+ [],
|
|
34
|
+ )
|
19
|
35
|
|
20
|
36
|
const tableWrapper = useMemo(() => ({
|
21
|
37
|
body: {
|
22
|
|
- wrapper: DnDProvider,
|
23
|
|
- row: RowWrapper,
|
|
38
|
+ wrapper: (props) => <DraggableContainer {...props} onSortEnd={handleDragEnd} />,
|
|
39
|
+ row: withDraggableRow(list),
|
24
|
40
|
},
|
25
|
|
- // eslint-disable-next-line react-hooks/exhaustive-deps
|
26
|
|
- }), [forceUpdate])
|
27
|
|
-
|
28
|
|
- const moveRow = ({ source, target }) => {
|
29
|
|
- console.log('--------->', source.index, target.index)
|
30
|
|
- if (source.index === target.index) {
|
31
|
|
- return
|
32
|
|
- }
|
33
|
|
-
|
34
|
|
- request('/sort/topic', { method: 'PUT', data: { from: source.index, to: target.index, id: source.id } }).then(() => {
|
35
|
|
- actRef.current.reload()
|
36
|
|
- setForceUpdate(forceUpdate + 1)
|
37
|
|
- }).catch((e) => {
|
38
|
|
- notification.error({ message: e.message })
|
39
|
|
- })
|
40
|
|
- }
|
|
41
|
+ }), [handleDragEnd])
|
41
|
42
|
|
42
|
43
|
const handleHotClick = useCallback((id) => {
|
43
|
44
|
history.push(id ? `/cms/hot/edit?id=${id}` : '/cms/hot/edit');
|
|
@@ -50,34 +51,27 @@ const HotList = () => {
|
50
|
51
|
];
|
51
|
52
|
|
52
|
53
|
const deleteHotClick = (id) => {
|
53
|
|
- setLoading(true);
|
54
|
|
-
|
55
|
54
|
request(`/topic/${id}`, { method: 'delete' })
|
56
|
55
|
.then(() => {
|
57
|
56
|
notification.success({ message: '删除成功' });
|
58
|
57
|
// ref.current.submit();
|
59
|
58
|
})
|
60
|
59
|
.catch((e) => {
|
61
|
|
- setLoading(false);
|
62
|
60
|
notification.error({ message: e.message });
|
63
|
61
|
});
|
64
|
62
|
};
|
65
|
63
|
|
66
|
64
|
const changeStatus = (row) => {
|
67
|
|
- setLoading(true);
|
68
|
|
-
|
69
|
65
|
request(`/topic/${row.serialNo}`, {
|
70
|
66
|
method: 'put',
|
71
|
67
|
data: { ...row, status: row.status === 1 ? 0 : 1 },
|
72
|
68
|
})
|
73
|
69
|
.then(() => {
|
74
|
70
|
notification.info({ message: '修改成功' });
|
75
|
|
- setLoading(false);
|
76
|
71
|
// ref.current.submit();
|
77
|
72
|
})
|
78
|
73
|
.catch((e) => {
|
79
|
74
|
notification.error({ message: e.message });
|
80
|
|
- setLoading(false);
|
81
|
75
|
return Promise.reject(e.message);
|
82
|
76
|
});
|
83
|
77
|
};
|
|
@@ -89,13 +83,13 @@ const HotList = () => {
|
89
|
83
|
width: 60,
|
90
|
84
|
hideInSearch: true,
|
91
|
85
|
align: 'center',
|
92
|
|
- render: () => (<MenuOutlined style={{ cursor: 'grab', color: '#999' }} />),
|
|
86
|
+ className: 'drag-visible',
|
|
87
|
+ render: () => <DragHandle />,
|
93
|
88
|
},
|
94
|
89
|
{
|
95
|
90
|
title: '主图',
|
96
|
91
|
dataIndex: 'poster',
|
97
|
92
|
hideInSearch: true,
|
98
|
|
-
|
99
|
93
|
align: 'center',
|
100
|
94
|
render: (text) => <Image width={64} height={64} src={text} />,
|
101
|
95
|
},
|
|
@@ -148,13 +142,14 @@ const HotList = () => {
|
148
|
142
|
},
|
149
|
143
|
];
|
150
|
144
|
|
151
|
|
- const handleRow = useCallback((record) => ({
|
152
|
|
- index: record.sortNo,
|
153
|
|
- bizId: record.serialNo,
|
154
|
|
- moveRow,
|
155
|
|
-
|
156
|
|
- // eslint-disable-next-line react-hooks/exhaustive-deps
|
157
|
|
- }), [forceUpdate])
|
|
145
|
+ const requestWrapper = (...args) => {
|
|
146
|
+ return new Promise((resolve, reject) => {
|
|
147
|
+ queryTable('/topic')(...args).then((res) => {
|
|
148
|
+ resolve(res)
|
|
149
|
+ list.current = res.data
|
|
150
|
+ }).catch(reject)
|
|
151
|
+ })
|
|
152
|
+ }
|
158
|
153
|
|
159
|
154
|
return (
|
160
|
155
|
<PageContainer>
|
|
@@ -162,12 +157,11 @@ const HotList = () => {
|
162
|
157
|
pagination={false}
|
163
|
158
|
columns={columns}
|
164
|
159
|
actionRef={actRef}
|
165
|
|
- request={queryTable('/topic')}
|
|
160
|
+ request={requestWrapper}
|
166
|
161
|
rowKey="serialNo"
|
167
|
162
|
headerTitle="科普列表"
|
168
|
163
|
search={{ labelWidth: '4em' }}
|
169
|
164
|
components={tableWrapper}
|
170
|
|
- onRow={handleRow}
|
171
|
165
|
toolBarRender={() => actions}
|
172
|
166
|
/>
|
173
|
167
|
</PageContainer>
|