importModal.jsx 3.39 KB
Newer Older
1 2 3 4 5
/* eslint-disable react-hooks/rules-of-hooks */
/*
 * @Description:
 * @Author: leizhe
 * @Date: 2022-04-11 18:12:39
6
 * @LastEditTime: 2022-05-20 15:25:04
7 8 9
 * @LastEditors: leizhe
 */
import React, { useEffect, useState } from 'react';
10 11 12 13 14 15 16 17 18 19 20 21
import {
  Modal,
  Form,
  Input,
  notification,
  message,
  Button,
  Col,
  Row,
  Upload,
  Popconfirm,
} from 'antd';
22 23 24 25 26 27
import { ImportDataDictionary } from '@/services/dataCenter/api';

const importModal = props => {
  const { visible, onCancel, callBackSubmit = () => {} } = props;
  const [addForm] = Form.useForm();
  const [file, setFile] = useState('');
28
  const [load, setLoad] = useState(false); // 提交加载
29 30

  useEffect(() => {
31
    setLoad(false);
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
    addForm.resetFields();
  }, [visible]);

  const aa = {
    beforeUpload: file => {
      console.log(file);
      addForm.setFieldsValue({
        ModelFile: file.name,
      });
      setFile(file);
      console.log(file);
      return false;
    },
  };

  const importData = () => {
    addForm.validateFields().then(validate => {
      if (validate) {
50
        setLoad(true);
51 52 53
        const formData = new FormData();
        formData.append('file', file);
        ImportDataDictionary(formData).then(res => {
54
          setLoad(false);
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
          if (res.code === 0) {
            callBackSubmit();
            addForm.resetFields();
            notification.success({
              message: '提示',
              duration: 3,
              description: '导入成功',
            });
          } else {
            notification.error({
              message: '提示',
              duration: 3,
              description: res.msg,
            });
          }
        });
      }
    });
  };
74 75 76

  const title1 = (
    <>
77 78
      <span>覆盖导入数据</span>
      <span style={{ color: 'rgb(24, 144, 255)' }}>(不支持导入从老运维导出的数据)</span>
79 80
    </>
  );
81 82
  return (
    <Modal
83
      title={title1}
84 85 86
      visible={visible}
      onCancel={onCancel}
      destroyOnClose
87
      // onOk={importData}
88 89 90
      afterClose={() => {
        addForm.resetFields();
      }}
91
      // confirmLoading={load}
92
      maskClosable={false}
93 94 95 96 97 98 99 100 101 102 103 104
      footer={[
        <Button key="back" onClick={onCancel}>
          取消
        </Button>,
        <Popconfirm
          placement="topLeft"
          title="将覆盖原有数据是否继续导入"
          onConfirm={importData}
          okText="确认"
          key="submit"
          cancelText="取消"
        >
105 106 107
          <Button type="primary" loading={load}>
            确认
          </Button>
108 109 110 111
        </Popconfirm>,
      ]}
      // okText="确认"
      // cancelText="取消"
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
    >
      <Form form={addForm} labelCol={{ span: 5 }}>
        <Row>
          <Col span={17}>
            <Form.Item
              label="模型文件"
              name="ModelFile"
              labelCol={{ span: 7 }}
              rules={[
                {
                  required: true,
                  message: '请选择文件',
                },
              ]}
            >
              <Input placeholder="仅支持.xls, .xlsx格式" />
            </Form.Item>
          </Col>
          <Col span={7}>
            <Form.Item>
              <Upload {...aa} accept=".xls, .xlsx">
                <Button style={{ width: '130px' }}>上传文件</Button>
              </Upload>
            </Form.Item>
          </Col>
        </Row>
      </Form>
    </Modal>
  );
};
export default importModal;