AddModal.jsx 4.42 KB
Newer Older
1 2
import React, { useState, useEffect } from 'react';
import { Form, Modal, Input, Select, notification } from 'antd';
3
import { AddConnString, EditConnString } from '@/services/database/api';
4 5 6 7 8 9 10 11 12 13 14 15 16 17

const { Item } = Form;
const { Option } = Select;
const AddModal = props => {
  const { callBackSubmit = () => {}, type, formObj, visible } = props;
  const [loading, setLoading] = useState(false);
  const [form] = Form.useForm();
  // 提交
  const onSubmit = () => {
    form.validateFields().then(validate => {
      if (validate) {
        setLoading(true);
        let obj = form.getFieldsValue();
        if (type === 'add') {
皮倩雯's avatar
皮倩雯 committed
18
          AddConnString({
19
            type: 'mysql',
20 21 22 23
            ...obj,
          })
            .then(res => {
              setLoading(false);
皮倩雯's avatar
皮倩雯 committed
24
              if (res.code == 0) {
25 26 27 28 29
                form.resetFields();
                callBackSubmit();
                notification.success({
                  message: '提示',
                  duration: 3,
30
                  description: '新增成功',
31 32 33 34 35
                });
              } else {
                notification.error({
                  message: '提示',
                  duration: 3,
36
                  description: res.msg || '新增失败',
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
                });
              }
            })
            .catch(err => {
              setLoading(false);
              console.error(err);
            });
        } else if (type === 'edit') {
          handleEdit();
        }
      }
    });
  };
  const handleEdit = () => {
    let obj = form.getFieldsValue();
皮倩雯's avatar
皮倩雯 committed
52
    EditConnString({
53
      type: 'mysql',
54 55 56 57 58
      ...obj,
      oldName: formObj.name,
    })
      .then(res => {
        setLoading(false);
皮倩雯's avatar
皮倩雯 committed
59
        if (res.code == 0) {
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
          form.resetFields();
          callBackSubmit();
          notification.success({
            message: '提示',
            duration: 3,
            description: res.message || '编辑成功',
          });
        } else {
          notification.error({
            message: '提示',
            duration: 3,
            description: res.message || '编辑失败',
          });
        }
      })
      .catch(err => setLoading(false));
  };
Maofei94's avatar
Maofei94 committed
77
  const onFinish = value => {};
78
  useEffect(() => {
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
    if (visible) {
      switch (type) {
        case 'add':
          form.setFieldsValue({ port: 3306 });
          break;
        case 'edit':
          form.setFieldsValue({ ...formObj });
          if (!formObj.port) {
            form.setFieldsValue({ port: 3306 });
          }
          break;
        default:
          break;
      }
    } else {
      form.resetFields();
95 96 97 98 99 100 101 102
    }
  }, [visible]);
  const layout = {
    layout: 'horizontal',
    labelCol: {
      span: 4,
    },
    wrapperCol: {
103
      span: 19,
104 105 106 107
    },
  };
  return (
    <Modal
108
      title={`${type === 'add' ? '添加' : '编辑'}MySQL数据库配置连接`}
109 110 111 112 113 114 115 116 117 118 119 120 121
      bodyStyle={{ width: '100%', minHeight: '100px' }}
      style={{ top: '150px' }}
      width="700px"
      destroyOnClose
      maskClosable={false}
      cancelText="取消"
      okText="确认"
      {...props}
      onOk={() => onSubmit()}
      confirmLoading={loading}
    >
      {visible && (
        <Form form={form} {...layout} onFinish={onFinish}>
122
          <Item label="标签" name="name" rules={[{ required: true, message: '请输入标签' }]}>
123 124
            <Input placeholder="请输入标签" allowClear />
          </Item>
125
          <Item label="IP" name="ip" rules={[{ required: true, message: '请输入IP' }]}>
126 127
            <Input placeholder="请输入IP" allowClear />
          </Item>
128 129 130
          <Item label="端口" name="port" rules={[{ required: true, message: '请输入端口' }]}>
            <Input placeholder="请输入端口号" allowClear />
          </Item>
131 132 133 134 135 136 137 138 139 140 141 142 143 144
          <Item
            label="数据库名"
            name="dbName"
            rules={[{ required: true, message: '请输入数据库名' }]}
          >
            <Input placeholder="请输入数据库名" allowClear />
          </Item>
          <Item
            label="用户名"
            name="userName"
            rules={[{ required: true, message: '请输入用户名' }]}
          >
            <Input placeholder="请输入用户名" allowClear />
          </Item>
145
          <Item label="密码" name="password" rules={[{ required: true, message: '请输入密码' }]}>
146 147 148 149 150 151 152 153
            <Input placeholder="请输入密码" allowClear />
          </Item>
        </Form>
      )}
    </Modal>
  );
};
export default AddModal;