AddConfigForm.jsx 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /**
  2. * @Classname AddConfigForm
  3. * @Description TODO
  4. * @Date 2019/8/22 11:45
  5. * @Created by Administrator
  6. * 编辑或者添加微服务配置属性
  7. */
  8. import React from 'react';
  9. import * as PropTypes from 'prop-types';
  10. import { connectAlita } from 'redux-alita';
  11. import { AutoComplete, Button, Form, Input, message, Modal, Select } from 'antd';
  12. import TuonSelect from '../../../components/widget/TuonSelect';
  13. class AddConfigForm extends React.Component {
  14. static propTypes = {
  15. onCancel: PropTypes.func,
  16. visible: PropTypes.bool,
  17. config: PropTypes.object,
  18. onRefresh: PropTypes.func,
  19. fData:PropTypes.object
  20. };
  21. static defaultProps = {
  22. config: {
  23. id: undefined,
  24. key: undefined,
  25. value: undefined,
  26. application: undefined,//暂时定如果为空,则是通用的
  27. profile: 'dev',
  28. label: 'master',
  29. desc: undefined,
  30. },
  31. fData:{data:{}}
  32. };
  33. static getDerivedStateFromProps(nextProps, prevState) {
  34. let { visible, config } = nextProps;
  35. return {
  36. visible, config: config || { profile: 'dev', label: 'master' },
  37. };
  38. }
  39. constructor(props) {
  40. super(props);
  41. this.state = {
  42. visible: false,
  43. config: {},
  44. };
  45. }
  46. _queryLabelList(keywords) {
  47. if (!keywords) return;
  48. let params = { keywords, pageSize: 6 };
  49. this.props.setAlitaState({ funcName: 'searchLabel', params });
  50. }
  51. handleAddConfig(values) {
  52. this.props.setAlitaState({ funcName: 'configAddConfig', params: values })
  53. .then(resp => {
  54. message.success('添加成功!');
  55. this.props.onRefresh && this.props.onRefresh();
  56. });
  57. }
  58. handleEditConfig(values) {
  59. let params = { id: this.state.config.id, data: values };
  60. this.props.setAlitaState({ funcName: 'configEditConfig', params })
  61. .then(() => {
  62. message.success('修改成功!');
  63. this.props.onCancel && this.props.onCancel();
  64. this.props.onRefresh && this.props.onRefresh();
  65. });
  66. }
  67. _onModalOkPress(e) {
  68. e.preventDefault();
  69. let _this = this;
  70. this.props.form.validateFieldsAndScroll((err, values) => {
  71. if (!err) {
  72. if (_this.state.config.id) {
  73. _this.handleEditConfig(values);
  74. } else {
  75. _this.handleAddConfig(values);
  76. }
  77. } else {
  78. console.log('validateFields error=>', err);
  79. }
  80. });
  81. }
  82. _onModalCancel() {
  83. this.props.onCancel && this.props.onCancel();
  84. this.props.form.resetFields();
  85. }
  86. render() {
  87. const { getFieldDecorator } = this.props.form;
  88. let { visible } = this.state;
  89. const { data: labelPage, isFetching: isSearching } = this.props.searchLabel || {};
  90. let isSubmitting = false;
  91. if (this.props.configAddConfig && this.props.configAddConfig.isFetching) {
  92. isSubmitting = true;
  93. }
  94. if (this.props.configEditConfig && this.props.configEditConfig.isFetching) {
  95. isSubmitting = true;
  96. }
  97. let labelList = [];
  98. if (labelPage && labelPage.records) {
  99. labelList = labelPage.records;
  100. }
  101. let config = this.state.config;
  102. let {cApps,cLabels,cProfiles} =this.props.fData.data;
  103. return (<Modal title={config.id ? '编辑配置项' : '新增配置项'}
  104. onCancel={this._onModalCancel.bind(this)}
  105. onOk={this._onModalOkPress.bind(this)}
  106. footer={[
  107. <Button key="back" htmlType="button" onClick={this._onModalCancel.bind(this)}>取消</Button>,
  108. <Button key="submit" htmlType="button" type="primary"
  109. onClick={this._onModalOkPress.bind(this)}
  110. loading={isSubmitting}
  111. >提交</Button>,
  112. ]}
  113. visible={visible}
  114. >
  115. <Form>
  116. <Form.Item>
  117. {getFieldDecorator('application', { initialValue: config.application })(
  118. <TuonSelect placeholder="请选择应用系统" data={cApps}/>)}
  119. </Form.Item>
  120. <Form.Item>
  121. {getFieldDecorator('profile', { initialValue: config.profile })(
  122. <TuonSelect placeholder="请选择环境" data={cProfiles}/>)}
  123. </Form.Item>
  124. <Form.Item>
  125. {getFieldDecorator('label', { initialValue: config.label })(
  126. <TuonSelect placeholder="请选择分支" data={cLabels}/>)}
  127. </Form.Item>
  128. <Form.Item>
  129. {getFieldDecorator('key',
  130. {
  131. initialValue: config.key,
  132. rules: [{ required: true, message: '请输入配置项' }],
  133. })(<AutoComplete
  134. dataSource={labelList}
  135. loading={isSearching}
  136. placeholder="请输入配置项"
  137. onSearch={this._queryLabelList.bind(this)}>
  138. {labelList.map(item => <AutoComplete.Option key={item.id}
  139. value={item.path}>{item.path}</AutoComplete.Option>)}
  140. </AutoComplete>,
  141. )}
  142. </Form.Item>
  143. <Form.Item>
  144. {getFieldDecorator('value', { initialValue: config.value })(<Input
  145. placeholder="请输入值"
  146. />)}
  147. </Form.Item>
  148. <Form.Item>
  149. {getFieldDecorator('desc', { initialValue: config.desc })(<Input
  150. placeholder="输入配置简介"
  151. />)}
  152. </Form.Item>
  153. </Form>
  154. </Modal>);
  155. }
  156. }
  157. export default connectAlita(['searchLabel', 'configAddConfig', 'configEditConfig','fData'])(Form.create({ name: 'AddConfigForm' })(AddConfigForm));