FlowGroupModal.jsx 2.84 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 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
import React, { useEffect } from 'react';
import { UpdateFlowGroup } from '@/services/platform/workflow';
import { Form, Modal, Input, notification } from 'antd';

const FlowGroupModal = props => {
  const { onSubumit, handleCancel, visible, msg, modalType, treeData } = props;
  const [form] = Form.useForm();
  useEffect(() => {
    form.resetFields();
    if (visible) {
      if (modalType === 'edit') {
        getFormData();
      }
    }
  }, [visible]);
  // 获取表单回显
  const getFormData = () => {
    form.setFieldsValue({ name: msg.name });
  };
  // 提交表单
  const onFinish = () => {
    form.validateFields().then(validate => {
      if (validate) {
        let hasName;
        hasName = treeData.some(item => item.name === validate.name);
        if (modalType === 'edit') {
          if (hasName && validate.name !== msg.name) {
            notification.error({
              message: '提示',
              duration: 3,
              description: '流程组名称不能重复',
            });
            return;
          }
          if (validate.name === msg.name) {
            onSubumit();
            return;
          }
          if (!msg.isOld) {
            onSubumit(validate.name);
            return;
          }
          UpdateFlowGroup({
            oldName: msg.name,
            newName: validate.name,
          }).then(res => {
            if (res.code === 0) {
              onSubumit();
              notification.success({
                message: '提示',
                duration: 3,
                description: '编辑成功',
              });
            } else {
              onSubumit(validate.name);
              notification.error({
                message: '提示',
                duration: 3,
                description: res.msg,
              });
            }
          });
        } else {
          // 新增
          if (hasName) {
            notification.error({
              message: '提示',
              duration: 3,
              description: '流程组名称不能重复',
            });
            return;
          }
          notification.success({
            message: '提示',
            duration: 3,
            description: '新增成功',
          });
          onSubumit(validate.name);
        }
      }
    });
  };
  return (
    <Modal
      title={`${modalType === 'edit' ? '编辑' : '创建'}分组`}
      visible={visible}
      onOk={onFinish}
      onCancel={handleCancel}
      maskClosable={false}
      destroyOnClose
    >
      <Form form={form} labelCol={{ span: 5 }} wrapperCol={{ span: 18 }}>
        <Form.Item
          label="分组名称"
          name="name"
          rules={[{ required: true, message: '请输入分组名称' }]}
        >
          <Input placeholder="请输入分组名称" />
        </Form.Item>
      </Form>
    </Modal>
  );
};
export default FlowGroupModal;