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