AddTopology.tsx 2.59 KB
Newer Older
DarkForst's avatar
DarkForst committed
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 105 106
import React, {useEffect} from 'react';
import { useRequest } from 'umi';
import {Form, Modal, Button} from 'antd'
import {createSubmitForm} from "@/utils/createForm";

import {fetchAddSystemTree} from '@/services/device'

type AddTopologyProps = {
  visible: boolean;
  handleClose: () => void;
  type?: 'add' | 'edit';
  editInfo: API.Store;
  systemId: number;
  parentId?: string | number;
  onRefresh?: () => void;
  level?: 'eq' | 'child';
  selectId?: number | string
}

const formTailLayout = {
  labelCol: { span: 7 },
  wrapperCol: { span: 15 },
};

const AddTopology: React.FC<AddTopologyProps> = (props) => {
  const { visible, handleClose, type, systemId, parentId, editInfo, onRefresh, level, selectId} = props;

  const seleId = typeof selectId === 'string' ? 0 : selectId
  /**
   * @表单提交
   */

  const systemNodePatch = useRequest(fetchAddSystemTree, {
    manual: true,
    onSuccess: () => {
      handleClose();
      onRefresh && onRefresh()
    }
  })

  const [form] = Form.useForm();
  const handleSubmit = (e: any) => {
    e.preventDefault();
    form.validateFields().then(values => {
      const payload = {...values, systemId, parentId: level === 'eq' ? parentId : seleId};
      if(type === 'edit') payload.id = selectId
      console.log(payload)
      systemNodePatch.run(payload)
    }).catch((err) => {
      console.log(err)
    });
  };
  /* 关闭Modal */
  const closeModal = () => {
    handleClose();
    form.resetFields()
  }

  /**
   * @页面数据
   */

  const allFormData = [
    {label: '节点名称', name: 'name', type: 'input', require: true},
  ];


  useEffect(() => {
    if (visible && type === 'edit') {
      form.setFieldsValue({
        name: editInfo?.name
      })
    }else {
      form.resetFields()
    }
  }, [visible])

  return (
    <div>
      <Modal
        width={450}
        title={type === 'add' ? '添加节点' : '编辑节点'}
        visible={ visible }
        onCancel={ closeModal }
        centered
        footer={null}
        getContainer={false}
        // forceRender
      >
        <Form form={form} {...formTailLayout} onFinish={ handleSubmit }>
          {allFormData?.length > 0 && allFormData.map(item => createSubmitForm(item, 'topology'))}
          <div style={{ marginTop: 32, textAlign: 'right' }}>
            <Button style={{marginRight: 8 }} onClick={closeModal}>
              取消
            </Button>
            <Button type="primary" loading={systemNodePatch.loading} onClick={handleSubmit}>
              保存
            </Button>
          </div>
        </Form>
      </Modal>
    </div>
  );
};

export default AddTopology