张延森 4 anos atrás
pai
commit
324958a92e

+ 0
- 2
package.json Ver arquivo

@@ -61,8 +61,6 @@
61 61
     "omit.js": "^2.0.2",
62 62
     "react": "17.0.0",
63 63
     "react-dev-inspector": "^1.1.1",
64
-    "react-dnd": "^14.0.2",
65
-    "react-dnd-html5-backend": "^14.0.0",
66 64
     "react-dom": "^17.0.0",
67 65
     "react-helmet-async": "^1.0.4",
68 66
     "react-player": "^2.9.0",

+ 0
- 9
src/components/DndRow/Provider.jsx Ver arquivo

@@ -1,9 +0,0 @@
1
-import React from 'react'
2
-import { DndProvider } from 'react-dnd'
3
-import { HTML5Backend } from 'react-dnd-html5-backend'
4
-
5
-export default (props) => {
6
-  return (
7
-    <DndProvider backend={HTML5Backend}><tbody>{props.children}</tbody></DndProvider>
8
-  )
9
-}

+ 0
- 24
src/components/DndRow/index.jsx Ver arquivo

@@ -1,24 +0,0 @@
1
-import React, { useRef } from 'react'
2
-import useDnd from '@/utils/hooks/useDnd'
3
-
4
-export default (props) => {
5
-  const ref = useRef();
6
-  const { id, moveRow, ...restProps } = props;
7
-  const { dragPreview, dragSource, dropTarget, onDrop } = useDnd({
8
-    item: { id },
9
-  });
10
-
11
-  onDrop((item) => {
12
-    moveRow({
13
-      from: item.id,
14
-      to: id,
15
-    });
16
-  });
17
-
18
-  dragPreview(ref)
19
-  dragSource(dropTarget(ref))
20
-
21
-  return (
22
-    <tr ref={ref} {...restProps} />
23
-  )
24
-}

+ 2
- 2
src/components/DragTable/DraggableRow.jsx Ver arquivo

@@ -3,9 +3,9 @@ import { sortableElement } from 'react-sortable-hoc';
3 3
 
4 4
 export const SortableItem = sortableElement(props => <tr {...props} />);
5 5
 
6
-export default function withDraggableRow(dataSource) {
6
+export default function withDraggableRow(dataSource, config) {
7 7
   return (props) => {
8
-    const index = dataSource.current.findIndex(x => x.serialNo === props['data-row-key']);
8
+    const index = dataSource.current.findIndex(x => x[config.rowKey] === props['data-row-key']);
9 9
     return  <SortableItem index={index} {...props} />
10 10
   }
11 11
 }

+ 2
- 5
src/components/DragTable/style.less Ver arquivo

@@ -1,11 +1,8 @@
1 1
 
2 2
 .row-dragging {
3 3
   background: #fafafa;
4
-  border: 1px solid rgba(0, 0, 0, .02);
5
-
6
-  td {
7
-    // padding: 1em;
8
-  }
4
+  border: 1px solid rgba(0, 0, 0, .06);
5
+  display: table;
9 6
 
10 7
   .drag-visible {
11 8
     visibility: visible;

+ 47
- 71
src/pages/Cms/Banner/List/index.jsx Ver arquivo

@@ -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>

+ 7
- 9
src/pages/Cms/Hot/List/index.jsx Ver arquivo

@@ -1,8 +1,8 @@
1
-import React, { useRef, useCallback, useMemo, 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 7
 import request, { queryTable } from '@/utils/request';
8 8
 import { DraggableContainer, withDraggableRow, DragHandle } from '@/components/DragTable'
@@ -21,8 +21,6 @@ const HotList = () => {
21 21
 
22 22
       const from = list.current[oldIndex]?.serialNo
23 23
       const to = list.current[newIndex]?.serialNo
24
-
25
-      console.log('----------->', from, to)
26 24
   
27 25
       request('/sort/topic', { method: 'PUT', data: { from, to } }).then(() => {
28 26
         actRef.current.reload()
@@ -36,9 +34,9 @@ const HotList = () => {
36 34
   const tableWrapper = useMemo(() => ({
37 35
     body: {
38 36
       wrapper: (props) => <DraggableContainer {...props} onSortEnd={handleDragEnd} />,
39
-      row: withDraggableRow(list),
37
+      row: withDraggableRow(list, { rowKey: 'serialNo' }),
40 38
     },
41
-  }), [handleDragEnd])
39
+  }), [handleDragEnd]);
42 40
 
43 41
   const handleHotClick = useCallback((id) => {
44 42
     history.push(id ? `/cms/hot/edit?id=${id}` : '/cms/hot/edit');
@@ -54,7 +52,7 @@ const HotList = () => {
54 52
     request(`/topic/${id}`, { method: 'delete' })
55 53
       .then(() => {
56 54
         notification.success({ message: '删除成功' });
57
-        // ref.current.submit();
55
+        actRef.current.reload();
58 56
       })
59 57
       .catch((e) => {
60 58
         notification.error({ message: e.message });
@@ -68,7 +66,7 @@ const HotList = () => {
68 66
     })
69 67
       .then(() => {
70 68
         notification.info({ message: '修改成功' });
71
-        // ref.current.submit();
69
+        actRef.current.reload();
72 70
       })
73 71
       .catch((e) => {
74 72
         notification.error({ message: e.message });
@@ -159,7 +157,7 @@ const HotList = () => {
159 157
         actionRef={actRef}
160 158
         request={requestWrapper}
161 159
         rowKey="serialNo"
162
-        headerTitle="科普列表"
160
+        headerTitle="热门列表"
163 161
         search={{ labelWidth: '4em' }}
164 162
         components={tableWrapper}
165 163
         toolBarRender={() => actions}

+ 0
- 38
src/utils/hooks/useDnD.js Ver arquivo

@@ -1,38 +0,0 @@
1
-import { useRef } from "react";
2
-import { useDrag, useDrop } from "react-dnd";
3
-
4
-export default function useDnD(options, deps) {
5
-  const dndType = 'dnd_item';
6
-  const handleDrop = useRef(x => x);
7
-
8
-  const [{ isDragging }, dragSource, dragPreview ] = useDrag(() => ({
9
-    type: options.type || dndType,
10
-    item: options.item,
11
-    collect: (monitor) => ({
12
-      isDragging: monitor.isDragging(),
13
-    }),
14
-  }), deps)
15
-
16
-  const [{ isOver }, dropTarget ]  = useDrop(() => ({
17
-    accept: options.type || dndType,
18
-    collect: (monitor) => ({
19
-      isOver: monitor.isOver(),
20
-    }),
21
-    drop: (item) => {
22
-      handleDrop.current(item)
23
-    }
24
-  }), deps)
25
-
26
-  const onDrop = (cb) => {
27
-    handleDrop.current = cb
28
-  }
29
-
30
-  return {
31
-    dragSource,
32
-    dragPreview,
33
-    dropTarget,
34
-    isDragging,
35
-    isOver,
36
-    onDrop,
37
-  }
38
-}