index.jsx 6.0 KB
Newer Older
徐立's avatar
徐立 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/**
 * 提交按钮封装
 * 需要传入以下参数
 * form控件
 * appId 事务Id
 * draftId 草稿Id 可不传
 * callback 保存成功后执行函数
 */
import React, { Component } from 'react';
import { Button } from 'antd';
import AddModel from './AddModal';
import {preHandle} from '../../utils/myutils';
import { connect } from 'dva';
import { openToast } from '../../location/Notification';
徐立's avatar
徐立 committed
15
import { isEmpty } from '../../copy';
徐立's avatar
徐立 committed
16 17 18 19 20 21
@connect()
export default class idnex extends Component {
    constructor(props){
        super(props)
        this.state = {
            visible: false,
徐立's avatar
徐立 committed
22 23 24 25 26 27 28 29 30 31
            isLoading: false,
            draftId: props.draftId??'',
        }
    }
    componentWillReceiveProps(nextProps){
        const { draftId } = nextProps
        if(draftId) { // 接收到最新草稿Id更新
            this.setState({
                draftId
            })
徐立's avatar
徐立 committed
32 33 34 35 36 37 38 39 40 41 42 43
        }
    }
    showModal = () => {
        this.props.form.validateFields((err, values) => {
            if (!err) {
                this.setState({
                    visible: true,
                });
            }
        });
    }
    handleOk = () => {
44
        let {dispatch, draftCode } = this.props;
徐立's avatar
徐立 committed
45 46 47 48 49 50 51 52 53 54 55 56
        this.props.form.validateFields((err, values) => {
            if (!err) {
                preHandle(values); // 引入 import preHandle from '@/webPublic/one_stop_public/utils/myutils.js'
                console.log(values)
                this.setState({
                    isLoading: true,
                },() => {
                    dispatch({
                        type: 'affair/startProcess',
                        payload: {
                            content: JSON.stringify(values), // 表单数据
                            appId: this.props.appId, // 这里应该由上级路由跳转传入 事务Id
57 58
                            id: !!this.state.draftId ? this.state.draftId : null, // 确认是否存在草稿表单Id存在即传入
                            code: typeof draftCode !== 'undefined' ? draftCode : null, // 确认是否存在草稿表单code存在即传入
徐立's avatar
徐立 committed
59 60 61
                        },
                        callback: val => {
                            if (val) {
徐立's avatar
徐立 committed
62 63 64
                                this.setState({
                                    draftId:val.id
                                })
徐立's avatar
徐立 committed
65
                                if(this.props?.callback){
徐立's avatar
徐立 committed
66
                                    this.props.callback(val)
徐立's avatar
徐立 committed
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
                                }
                            }
                        },
                    }).then(()=>{
                        this.setState({
                            isLoading: false,
                            visible: false,
                        })
                    })
                })
            } else {
                openToast('error', '提交失败', '请填写必填项');
            }
        });
    }
    handleCancel = () => {
        this.setState({
            visible: false
        })
    }
徐立's avatar
徐立 committed
87
    handleDraft = () => {
88
        let {dispatch, id, radioValue,draftCallback, draftCode} = this.props;
徐立's avatar
徐立 committed
89 90 91 92 93 94 95 96 97
        this.setState({
            isLoading:true
        },() => {
            this.props.form.validateFields((err, values) => {
                if (!err) {
                    preHandle(values); // 引入 import {preHandle} from '@/webPublic/one_stop_public/utils/myutils.js'
                    dispatch({
                    type: 'affair/saveDraft',
                    payload: {
98
                        content: JSON.stringify(values), // 表单数据
徐立's avatar
徐立 committed
99
                        appId: this.props.appId, // 这里应该由上级路由跳转传入 事务Id
100 101
                        id: !!this.state.draftId ? this.state.draftId : null, // 确认是否存在草稿表单Id存在即传入
                        code: typeof draftCode !== 'undefined' ? draftCode : null, // 确认是否存在草稿表单code存在即传入
徐立's avatar
徐立 committed
102 103 104 105 106 107
                    },
                    callback: val => {
                        if (isEmpty(val) === false) {
                            this.setState({ // 存在草稿后保存草稿Id 后续修改在这草稿Id上处理
                                draftId: val.id
                            })
徐立's avatar
徐立 committed
108 109 110
                            if(this.props?.draftCallback){
                                this.props.draftCallback(val)
                            }
徐立's avatar
徐立 committed
111 112 113 114 115 116 117 118 119 120 121 122 123 124
                            openToast('success', '保存成功', '草稿保存成功');
                        } else {
                            openToast('error', '保存失败', '请尝试');
                        }
                    },
                    }).then((res) => {
                        this.setState({
                            isLoading:false
                        })
                    })
                }
            });
        })
        };
徐立's avatar
徐立 committed
125 126
    render() {
        const {
徐立's avatar
徐立 committed
127 128 129
            text,
            openDraftButton,
            DraftButtonText,
徐立's avatar
徐立 committed
130 131 132 133 134 135 136
        } = this.props
        const {
            visible,
            isLoading,
        } = this.state
        return (
            <>
徐立's avatar
徐立 committed
137 138 139 140
            {
                openDraftButton?
                        <Button
                            style={{
王绍森's avatar
王绍森 committed
141
                                marginRight:24
徐立's avatar
徐立 committed
142
                            }}
王绍森's avatar
王绍森 committed
143
                            shape='round'
徐立's avatar
徐立 committed
144 145 146 147 148 149 150
                            isLoading={isLoading}
                            onClick={this.handleDraft}
                            >
                            {DraftButtonText??'暂存草稿'}
                        </Button>
                    :null
            }
徐立's avatar
徐立 committed
151 152 153 154
            <Button
                onClick={this.showModal}
                isLoading={isLoading}
                type='primary'
王绍森's avatar
王绍森 committed
155 156
                shape='round'
                ghost
徐立's avatar
徐立 committed
157 158 159 160 161
                >
                {
                    text??'提交'
                }
            </Button>
162
            <AddModel
徐立's avatar
徐立 committed
163 164 165 166 167 168 169 170 171
                visible={visible}
                handleOk={this.handleOk}
                isLoading={isLoading}
                handleCancel={this.handleCancel}
                />
            </>
        )
    }
}