mock平台

UpDateModal.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. import React, { PureComponent as Component } from 'react';
  2. import PropTypes from 'prop-types';
  3. import { connect } from 'react-redux';
  4. import { Modal, Form, Input, Icon, Tooltip, Select, message, Button, Row, Col } from 'antd';
  5. import {
  6. updateProject,
  7. fetchProjectList,
  8. delProject,
  9. changeUpdateModal,
  10. changeTableLoading
  11. } from '../../../reducer/modules/project';
  12. const { TextArea } = Input;
  13. const FormItem = Form.Item;
  14. const Option = Select.Option;
  15. import './ProjectList.scss';
  16. // layout
  17. const formItemLayout = {
  18. labelCol: {
  19. xs: { span: 24 },
  20. sm: { span: 6 }
  21. },
  22. wrapperCol: {
  23. xs: { span: 24 },
  24. sm: { span: 14 }
  25. }
  26. };
  27. const formItemLayoutWithOutLabel = {
  28. wrapperCol: {
  29. xs: { span: 24, offset: 0 },
  30. sm: { span: 20, offset: 6 }
  31. }
  32. };
  33. let uuid = 0;
  34. @connect(
  35. state => {
  36. return {
  37. projectList: state.project.projectList,
  38. isUpdateModalShow: state.project.isUpdateModalShow,
  39. handleUpdateIndex: state.project.handleUpdateIndex,
  40. tableLoading: state.project.tableLoading,
  41. currGroup: state.group.currGroup
  42. };
  43. },
  44. {
  45. fetchProjectList,
  46. updateProject,
  47. delProject,
  48. changeUpdateModal,
  49. changeTableLoading
  50. }
  51. )
  52. class UpDateModal extends Component {
  53. constructor(props) {
  54. super(props);
  55. this.state = {
  56. protocol: 'http://',
  57. envProtocolChange: 'http://'
  58. };
  59. }
  60. static propTypes = {
  61. form: PropTypes.object,
  62. fetchProjectList: PropTypes.func,
  63. updateProject: PropTypes.func,
  64. delProject: PropTypes.func,
  65. changeUpdateModal: PropTypes.func,
  66. changeTableLoading: PropTypes.func,
  67. projectList: PropTypes.array,
  68. currGroup: PropTypes.object,
  69. isUpdateModalShow: PropTypes.bool,
  70. handleUpdateIndex: PropTypes.number
  71. };
  72. // 修改线上域名的协议类型 (http/https)
  73. protocolChange = value => {
  74. this.setState({
  75. protocol: value
  76. });
  77. };
  78. handleCancel = () => {
  79. this.props.form.resetFields();
  80. this.props.changeUpdateModal(false, -1);
  81. };
  82. // 确认修改
  83. handleOk = e => {
  84. e.preventDefault();
  85. const {
  86. form,
  87. updateProject,
  88. changeUpdateModal,
  89. currGroup,
  90. projectList,
  91. handleUpdateIndex,
  92. fetchProjectList,
  93. changeTableLoading
  94. } = this.props;
  95. form.validateFields((err, values) => {
  96. if (!err) {
  97. // console.log(projectList[handleUpdateIndex]);
  98. let assignValue = Object.assign(projectList[handleUpdateIndex], values);
  99. values.protocol = this.state.protocol.split(':')[0];
  100. assignValue.env = assignValue.envs.map((item, index) => {
  101. return {
  102. name: values['envs-name-' + index],
  103. domain: values['envs-protocol-' + index] + values['envs-domain-' + index]
  104. };
  105. });
  106. // console.log(assignValue);
  107. changeTableLoading(true);
  108. updateProject(assignValue)
  109. .then(res => {
  110. if (res.payload.data.errcode == 0) {
  111. changeUpdateModal(false, -1);
  112. message.success('修改成功! ');
  113. fetchProjectList(currGroup._id).then(() => {
  114. changeTableLoading(false);
  115. });
  116. } else {
  117. changeTableLoading(false);
  118. message.error(res.payload.data.errmsg);
  119. }
  120. })
  121. .catch(() => {
  122. changeTableLoading(false);
  123. });
  124. form.resetFields();
  125. }
  126. });
  127. };
  128. // 项目的修改操作 - 删除一项环境配置
  129. remove = id => {
  130. const { form } = this.props;
  131. // can use data-binding to get
  132. const envs = form.getFieldValue('envs');
  133. // We need at least one passenger
  134. if (envs.length === 0) {
  135. return;
  136. }
  137. // can use data-binding to set
  138. form.setFieldsValue({
  139. envs: envs.filter(key => {
  140. const realKey = key._id ? key._id : key;
  141. return realKey !== id;
  142. })
  143. });
  144. };
  145. // 项目的修改操作 - 添加一项环境配置
  146. add = () => {
  147. uuid++;
  148. const { form } = this.props;
  149. // can use data-binding to get
  150. const envs = form.getFieldValue('envs');
  151. const nextKeys = envs.concat(uuid);
  152. // can use data-binding to set
  153. // important! notify form to detect changes
  154. form.setFieldsValue({
  155. envs: nextKeys
  156. });
  157. };
  158. render() {
  159. const { getFieldDecorator, getFieldValue } = this.props.form;
  160. // const that = this;
  161. const { isUpdateModalShow, projectList, handleUpdateIndex } = this.props;
  162. let initFormValues = {};
  163. let envMessage = [];
  164. // 如果列表存在且用户点击修改按钮时,设置表单默认值
  165. if (projectList.length !== 0 && handleUpdateIndex !== -1) {
  166. // console.log(projectList[handleUpdateIndex]);
  167. const { name, basepath, desc, env } = projectList[handleUpdateIndex];
  168. initFormValues = { name, basepath, desc, env };
  169. if (env.length !== 0) {
  170. envMessage = env;
  171. }
  172. initFormValues.prd_host = projectList[handleUpdateIndex].prd_host;
  173. initFormValues.prd_protocol = projectList[handleUpdateIndex].protocol + '://';
  174. }
  175. getFieldDecorator('envs', { initialValue: envMessage });
  176. const envs = getFieldValue('envs');
  177. const formItems = envs.map((k, index) => {
  178. const secondIndex = 'next' + index; // 为保证key的唯一性
  179. return (
  180. <Row key={index} type="flex" justify="space-between" align={index === 0 ? 'middle' : 'top'}>
  181. <Col span={10} offset={2}>
  182. <FormItem label={index === 0 ? <span>环境名称</span> : ''} required={false} key={index}>
  183. {getFieldDecorator(`envs-name-${index}`, {
  184. validateTrigger: ['onChange', 'onBlur'],
  185. initialValue: envMessage.length !== 0 ? k.name : '',
  186. rules: [
  187. {
  188. required: false,
  189. whitespace: true,
  190. validator(rule, value, callback) {
  191. if (value) {
  192. if (value.length === 0) {
  193. callback('请输入环境域名');
  194. } else if (!/\S/.test(value)) {
  195. callback('请输入环境域名');
  196. } else if (/prd/.test(value)) {
  197. callback('环境域名不能是"prd"');
  198. } else {
  199. return callback();
  200. }
  201. } else {
  202. callback('请输入环境域名');
  203. }
  204. }
  205. }
  206. ]
  207. })(<Input placeholder="请输入环境名称" style={{ width: '90%', marginRight: 8 }} />)}
  208. </FormItem>
  209. </Col>
  210. <Col span={10}>
  211. <FormItem
  212. label={index === 0 ? <span>环境域名</span> : ''}
  213. required={false}
  214. key={secondIndex}
  215. >
  216. {getFieldDecorator(`envs-domain-${index}`, {
  217. validateTrigger: ['onChange', 'onBlur'],
  218. initialValue: envMessage.length !== 0 && k.domain ? k.domain.split('//')[1] : '',
  219. rules: [
  220. {
  221. required: false,
  222. whitespace: true,
  223. message: '请输入环境域名',
  224. validator(rule, value, callback) {
  225. if (value) {
  226. if (value.length === 0) {
  227. callback('请输入环境域名');
  228. } else if (!/\S/.test(value)) {
  229. callback('请输入环境域名');
  230. } else {
  231. return callback();
  232. }
  233. } else {
  234. callback('请输入环境域名');
  235. }
  236. }
  237. }
  238. ]
  239. })(
  240. <Input
  241. placeholder="请输入环境域名"
  242. style={{ width: '90%', marginRight: 8 }}
  243. addonBefore={getFieldDecorator(`envs-protocol-${index}`, {
  244. initialValue:
  245. envMessage.length !== 0 && k.domain
  246. ? k.domain.split('//')[0] + '//'
  247. : 'http://',
  248. rules: [
  249. {
  250. required: true
  251. }
  252. ]
  253. })(
  254. <Select>
  255. <Option value="http://">{'http://'}</Option>
  256. <Option value="https://">{'https://'}</Option>
  257. </Select>
  258. )}
  259. />
  260. )}
  261. </FormItem>
  262. </Col>
  263. <Col span={2}>
  264. {/* 新增的项中,只有最后一项有删除按钮 */}
  265. {(envs.length > 0 && k._id) || envs.length == index + 1 ? (
  266. <Icon
  267. className="dynamic-delete-button"
  268. type="minus-circle-o"
  269. onClick={() => {
  270. return this.remove(k._id ? k._id : k);
  271. }}
  272. />
  273. ) : null}
  274. </Col>
  275. </Row>
  276. );
  277. });
  278. return (
  279. <Modal
  280. title="修改项目"
  281. visible={isUpdateModalShow}
  282. onOk={this.handleOk}
  283. onCancel={this.handleCancel}
  284. >
  285. <Form>
  286. <FormItem {...formItemLayout} label="项目名称">
  287. {getFieldDecorator('name', {
  288. initialValue: initFormValues.name,
  289. rules: [
  290. {
  291. required: true,
  292. message: '请输入项目名称!'
  293. }
  294. ]
  295. })(<Input />)}
  296. </FormItem>
  297. <FormItem
  298. {...formItemLayout}
  299. label={
  300. <span>
  301. 线上域名&nbsp;
  302. <Tooltip title="将根据配置的线上域名访问mock数据">
  303. <Icon type="question-circle-o" />
  304. </Tooltip>
  305. </span>
  306. }
  307. >
  308. {getFieldDecorator('prd_host', {
  309. initialValue: initFormValues.prd_host,
  310. rules: [
  311. {
  312. required: true,
  313. message: '请输入项目线上域名!'
  314. }
  315. ]
  316. })(
  317. <Input
  318. addonBefore={
  319. <Select defaultValue={initFormValues.prd_protocol} onChange={this.protocolChange}>
  320. <Option value="http://">{'http://'}</Option>
  321. <Option value="https://">{'https://'}</Option>
  322. </Select>
  323. }
  324. />
  325. )}
  326. </FormItem>
  327. <FormItem
  328. {...formItemLayout}
  329. label={
  330. <span>
  331. 基本路径&nbsp;
  332. <Tooltip title="基本路径为空表示根路径">
  333. <Icon type="question-circle-o" />
  334. </Tooltip>
  335. </span>
  336. }
  337. >
  338. {getFieldDecorator('basepath', {
  339. initialValue: initFormValues.basepath,
  340. rules: [
  341. {
  342. required: false,
  343. message: '请输入项目基本路径! '
  344. }
  345. ]
  346. })(<Input />)}
  347. </FormItem>
  348. <FormItem {...formItemLayout} label="描述">
  349. {getFieldDecorator('desc', {
  350. initialValue: initFormValues.desc,
  351. rules: [
  352. {
  353. required: false,
  354. message: '请输入描述!'
  355. }
  356. ]
  357. })(<TextArea rows={4} />)}
  358. </FormItem>
  359. {formItems}
  360. <FormItem {...formItemLayoutWithOutLabel}>
  361. <Button type="dashed" onClick={this.add} style={{ width: '60%' }}>
  362. <Icon type="plus" /> 添加环境配置
  363. </Button>
  364. </FormItem>
  365. </Form>
  366. </Modal>
  367. );
  368. }
  369. }
  370. export default Form.create()(UpDateModal);