Browse Source

将updateThumbnail和保存打包成saveWithThumbnail后放入model中以解决异步编程问题

xiaoct 7 years ago
parent
commit
e8f916ed14

+ 2 - 7
src/components/dashboardDesigner/header.jsx

@@ -31,8 +31,7 @@ class Header extends React.Component {
                             this.setState({
                             this.setState({
                                 visibleConfirm: false
                                 visibleConfirm: false
                             });
                             });
-                            updateThumbnail()// 添加更新thumbnail
-                            dispatch({ type: 'dashboard/remoteModify' });
+                            dispatch({ type: 'dashboardDesigner/saveWithThumbnail' });
                             dispatch({ type: 'main/redirect', path: '/dashboard' });
                             dispatch({ type: 'main/redirect', path: '/dashboard' });
                         }}
                         }}
                         onCancel={() => {
                         onCancel={() => {
@@ -55,15 +54,11 @@ class Header extends React.Component {
                     </Popconfirm>
                     </Popconfirm>
                     <Button onClick={() => {
                     <Button onClick={() => {
                         if(dashboardDesigner.code && dashboardDesigner.code !== -1) {
                         if(dashboardDesigner.code && dashboardDesigner.code !== -1) {
-                            updateThumbnail()// 添加更新thumbnail
-                            dispatch({ type: 'dashboard/remoteModify' });
+                            dispatch({ type: 'dashboardDesigner/saveWithThumbnail' });
                         }else {
                         }else {
-                            updateThumbnail()// 添加更新thumbnail
                             dispatch({ type: 'dashboard/remoteAdd' });
                             dispatch({ type: 'dashboard/remoteAdd' });
                         }
                         }
                     }}><Icon type='save' />保存</Button>
                     }}><Icon type='save' />保存</Button>
-                    <Button onClick={updateThumbnail}
-                    ><Icon type='save' />保存Thumbnail(Debug用)</Button>
                 </div>
                 </div>
                 <div className='header-item toolbar-title'>
                 <div className='header-item toolbar-title'>
                     <Input
                     <Input

+ 0 - 7
src/components/dashboardDesigner/layout.jsx

@@ -35,13 +35,6 @@ class DashboardDesigner extends React.Component {
                 pdf.save('stone.pdf');
                 pdf.save('stone.pdf');
         });
         });
     }
     }
-    updateThumbnail = () => {
-        const { dispatch } = this.props
-        html2canvas(document.getElementsByClassName('dashboard-content')[0]).then(function(canvas){
-            let thumbnail = canvas.toDataURL('image/png', 1.0);
-            dispatch({ type: 'dashboardDesigner/setField', name: 'thumbnail', value: thumbnail })
-        })
-    }
 
 
     render() {
     render() {
         const { loading } = this.props;
         const { loading } = this.props;

+ 25 - 2
src/models/dashboardDesigner.js

@@ -1,3 +1,6 @@
+import html2canvas from 'html2canvas'
+import { message } from 'antd'
+
 export default {
 export default {
     namespace: 'dashboardDesigner',
     namespace: 'dashboardDesigner',
     state: {
     state: {
@@ -131,8 +134,28 @@ export default {
     },
     },
 
 
     effects: {
     effects: {
-        
-    },
+        *saveWithThumbnail(action, {put, call}) {
+            let thumbnail;
+            yield call(async ()=> {
+                try {
+                    await html2canvas(document.getElementsByClassName('dashboard-content')[0]).then(
+                        function(canvas){
+                            thumbnail = canvas.toDataURL('image/png', 1.0);
+                        }
+                    )
+                }catch (e) {
+                    return null;
+                }
+            }); 
+                try {
+                    yield put.resolve({ type: 'setField', name: 'thumbnail', value: thumbnail })
+                    yield put.resolve({ type: 'dashboard/remoteModify' })
+                }catch (e){
+                    message.error('保存失败');
+                }
+            }
+
+        },
     subscription: {
     subscription: {
 
 
     },
     },