index.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import React from 'react'
  2. import { Form, Input, Row, Col, Switch, Button, Modal, message } from 'antd'
  3. import AceEditor from 'react-ace'
  4. import { create, test } from '../service'
  5. import 'ace-builds/src-noconflict/mode-java'
  6. import 'ace-builds/src-noconflict/theme-monokai'
  7. const formItemLayout = {
  8. layout: 'vertical'
  9. }
  10. @Form.create()
  11. class Add extends React.PureComponent {
  12. state = {
  13. content: '',
  14. showTest: false, // 是否展示测试内容
  15. testContent: '', // 测试数据
  16. testResult: '' // 测试结果
  17. }
  18. create = () => {
  19. this.props.form.validateFields((err, values) => {
  20. if (!err) {
  21. create({
  22. ...values,
  23. createUser: '马喆诚', // 创建人
  24. updateUser: '马喆诚' // 最后修改人
  25. }).then(res => {
  26. if (res.code === 0) {
  27. this.setState({ ...values })
  28. Modal.success({
  29. title: '提交成功',
  30. content: '策略代码编译正常,点击“测试按钮”可以继续测试策略逻辑'
  31. })
  32. }
  33. })
  34. }
  35. })
  36. }
  37. test =() => {
  38. if (!this.state.name) {
  39. message.warn('请先提交策略内容!')
  40. return false
  41. }
  42. test({
  43. name: this.state.name,
  44. content: this.state.testContent
  45. }).then(res => {
  46. if (res.code === 0) {
  47. this.setState({ testResult: res.data })
  48. }
  49. })
  50. }
  51. render () {
  52. const { getFieldDecorator } = this.props.form
  53. return (
  54. <div>
  55. <h1>新增策略</h1>
  56. <Row>
  57. <Col span={12}>
  58. <Form {...formItemLayout}>
  59. <Form.Item label="策略名称">
  60. {getFieldDecorator('name')(<Input placeholder="全英文或英文带下划线" />)}
  61. </Form.Item>
  62. <Form.Item label="策略描述">
  63. {getFieldDecorator('description')(<Input.TextArea placeholder="多行输入" />)}
  64. </Form.Item>
  65. <Form.Item label="是否启用">
  66. {getFieldDecorator('enabled')(<Switch />)}
  67. </Form.Item>
  68. <Form.Item label="策略逻辑开发">
  69. {getFieldDecorator('content')(
  70. <AceEditor
  71. style={{ width: this.state.showTest ? '100%' : '200%' }}
  72. mode="java"
  73. theme="monokai"
  74. name="content"
  75. onChange={(value) => this.setState({ content: value })}
  76. fontSize={14}
  77. showPrintMargin={true}
  78. showGutter={true}
  79. highlightActiveLine={true}
  80. value={''}
  81. setOptions={{
  82. enableBasicAutocompletion: true,
  83. enableLiveAutocompletion: true,
  84. enableSnippets: true,
  85. showLineNumbers: true,
  86. tabSize: 2
  87. }}
  88. />
  89. )}
  90. </Form.Item>
  91. <Form.Item>
  92. <Button type="primary" onClick={this.create}>提交</Button>&emsp;
  93. <Button onClick={() => this.setState({ showTest: true })}>测试</Button>
  94. </Form.Item>
  95. </Form>
  96. </Col>
  97. <Col span={12}>
  98. <div style={{ paddingLeft: '20px', display: this.state.showTest ? 'block' : 'none' }}>
  99. <Form.Item label="输入数据">
  100. <AceEditor
  101. mode="json"
  102. theme="monokai"
  103. onChange={(value) => this.setState({ testContent: value })}
  104. fontSize={14}
  105. showPrintMargin={true}
  106. showGutter={true}
  107. highlightActiveLine={true}
  108. value={this.state.testContent}
  109. height="300px"
  110. setOptions={{
  111. enableBasicAutocompletion: true,
  112. enableLiveAutocompletion: true,
  113. enableSnippets: true,
  114. showLineNumbers: true,
  115. tabSize: 2
  116. }}
  117. />
  118. </Form.Item>
  119. <Button type="primary" onClick={this.test}>执行测试</Button>&emsp;
  120. <Form.Item label="输出结果">
  121. <AceEditor
  122. mode="json"
  123. theme="monokai"
  124. fontSize={14}
  125. showPrintMargin={true}
  126. showGutter={true}
  127. highlightActiveLine={true}
  128. value={this.state.testResult}
  129. setOptions={{
  130. enableBasicAutocompletion: true,
  131. enableLiveAutocompletion: true,
  132. enableSnippets: true,
  133. showLineNumbers: true,
  134. tabSize: 2
  135. }}
  136. />
  137. </Form.Item>
  138. </div>
  139. </Col>
  140. </Row>
  141. </div>
  142. )
  143. }
  144. }
  145. export default Add