king
2021-10-14 e41a64966b7832baffe96c21d1ea77ef6adb2905
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
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import { is, fromJS } from 'immutable'
import { Modal, Button, notification } from 'antd'
 
import SettingForm from './settingform'
import './index.scss'
 
class BaseScript extends Component {
  static propTpyes = {
    config: PropTypes.object,
    updateConfig: PropTypes.func
  }
 
  state = {
    visible: false,
    baseSql: null
  }
 
  shouldComponentUpdate (nextProps, nextState) {
    return !is(fromJS(this.state), fromJS(nextState))
  }
 
  trigger = () => {
    const { config } = this.props
 
    this.setState({
      baseSql: config.baseSql || {enable: true, sql: ''},
      visible: true
    })
  }
 
  submit = () => {
    let config = fromJS(this.props.config).toJS()
 
    this.settingRef.handleConfirm().then(res => {
      if (res.enable && (!res.sql || /^\s+$/ig.test(res.sql))) {
        notification.warning({
          top: 92,
          message: '启用基础SQL时,请完善sql语句。',
          duration: 5
        })
        return
      }
 
      config.baseSql = res
      this.props.updateConfig(config)
      this.setState({ visible: false })
    })
  }
 
  render() {
    const { visible, baseSql } = this.state
 
    return (
      <div style={{display: 'inline-block'}}>
        <Button className="mk-border-danger" icon="database" onClick={this.trigger}>基础SQL</Button>
        <Modal
          title="基础SQL"
          wrapClassName="base-script-modal"
          visible={visible}
          width={800}
          maskClosable={false}
          onOk={this.submit}
          onCancel={() => { this.setState({ visible: false })}}
          destroyOnClose
        >
          <SettingForm baseSql={baseSql} wrappedComponentRef={(inst) => this.settingRef = inst}/>
        </Modal>
      </div>
    )
  }
}
 
export default BaseScript