mock平台

index.js 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. import React, { Component } from 'react';
  2. import PropTypes from 'prop-types';
  3. import './index.scss';
  4. import { Icon, Layout, Tooltip, message, Row, Popconfirm } from 'antd';
  5. const { Content, Sider } = Layout;
  6. import ProjectEnvContent from './ProjectEnvContent.js';
  7. import { connect } from 'react-redux';
  8. import { updateEnv, getProject, getEnv } from '../../../../reducer/modules/project';
  9. import EasyDragSort from '../../../../components/EasyDragSort/EasyDragSort.js';
  10. @connect(
  11. state => {
  12. return {
  13. projectMsg: state.project.currProject
  14. };
  15. },
  16. {
  17. updateEnv,
  18. getProject,
  19. getEnv
  20. }
  21. )
  22. class ProjectEnv extends Component {
  23. static propTypes = {
  24. projectId: PropTypes.number,
  25. updateEnv: PropTypes.func,
  26. getProject: PropTypes.func,
  27. projectMsg: PropTypes.object,
  28. onOk: PropTypes.func,
  29. getEnv: PropTypes.func
  30. };
  31. constructor(props) {
  32. super(props);
  33. this.state = {
  34. env: [],
  35. _id: null,
  36. currentEnvMsg: {},
  37. delIcon: null,
  38. currentKey: -2
  39. };
  40. }
  41. initState(curdata, id) {
  42. let newValue = {};
  43. newValue['env'] = [].concat(curdata);
  44. newValue['_id'] = id;
  45. this.setState({
  46. ...this.state,
  47. ...newValue
  48. });
  49. }
  50. async componentWillMount() {
  51. this._isMounted = true;
  52. await this.props.getProject(this.props.projectId);
  53. const { env, _id } = this.props.projectMsg;
  54. this.initState(env, _id);
  55. this.handleClick(0, env[0]);
  56. }
  57. componentWillUnmount() {
  58. this._isMounted = false;
  59. }
  60. handleClick = (key, data) => {
  61. this.setState({
  62. currentEnvMsg: data,
  63. currentKey: key
  64. });
  65. };
  66. // 增加环境变量项
  67. addParams = (name, data) => {
  68. let newValue = {};
  69. data = { name: '新环境', domain: '', header: [] };
  70. newValue[name] = [].concat(data, this.state[name]);
  71. this.setState(newValue);
  72. this.handleClick(0, data);
  73. };
  74. // 删除提示信息
  75. showConfirm(key, name) {
  76. let assignValue = this.delParams(key, name);
  77. this.onSave(assignValue);
  78. }
  79. // 删除环境变量项
  80. delParams = (key, name) => {
  81. let curValue = this.state.env;
  82. let newValue = {};
  83. newValue[name] = curValue.filter((val, index) => {
  84. return index !== key;
  85. });
  86. this.setState(newValue);
  87. this.handleClick(0, newValue[name][0]);
  88. newValue['_id'] = this.state._id;
  89. return newValue;
  90. };
  91. enterItem = key => {
  92. this.setState({ delIcon: key });
  93. };
  94. // 保存设置
  95. async onSave(assignValue) {
  96. await this.props
  97. .updateEnv(assignValue)
  98. .then(res => {
  99. if (res.payload.data.errcode == 0) {
  100. this.props.getProject(this.props.projectId);
  101. this.props.getEnv(this.props.projectId);
  102. message.success('修改成功! ');
  103. if(this._isMounted) {
  104. this.setState({ ...assignValue });
  105. }
  106. }
  107. })
  108. .catch(() => {
  109. message.error('环境设置不成功 ');
  110. });
  111. }
  112. // 提交保存信息
  113. onSubmit = (value, index) => {
  114. let assignValue = {};
  115. assignValue['env'] = [].concat(this.state.env);
  116. assignValue['env'].splice(index, 1, value['env']);
  117. assignValue['_id'] = this.state._id;
  118. this.onSave(assignValue);
  119. this.props.onOk && this.props.onOk(assignValue['env'], index);
  120. };
  121. // 动态修改环境名称
  122. handleInputChange = (value, currentKey) => {
  123. let newValue = [].concat(this.state.env);
  124. newValue[currentKey].name = value || '新环境';
  125. this.setState({ env: newValue });
  126. };
  127. // 侧边栏拖拽
  128. handleDragMove = name => {
  129. return (data, from, to) => {
  130. let newValue = {
  131. [name]: data
  132. };
  133. this.setState(newValue);
  134. newValue['_id'] = this.state._id;
  135. this.handleClick(to, newValue[name][to]);
  136. this.onSave(newValue);
  137. };
  138. };
  139. render() {
  140. const { env, currentKey } = this.state;
  141. const envSettingItems = env.map((item, index) => {
  142. return (
  143. <Row
  144. key={index}
  145. className={'menu-item ' + (index === currentKey ? 'menu-item-checked' : '')}
  146. onClick={() => this.handleClick(index, item)}
  147. onMouseEnter={() => this.enterItem(index)}
  148. >
  149. <span className="env-icon-style">
  150. <span className="env-name" style={{ color: item.name === '新环境' && '#2395f1' }}>
  151. {item.name}
  152. </span>
  153. <Popconfirm
  154. title="您确认删除此环境变量?"
  155. onConfirm={e => {
  156. e.stopPropagation();
  157. this.showConfirm(index, 'env');
  158. }}
  159. okText="确定"
  160. cancelText="取消"
  161. >
  162. <Icon
  163. type="delete"
  164. className="interface-delete-icon"
  165. style={{
  166. display: this.state.delIcon == index && env.length - 1 !== 0 ? 'block' : 'none'
  167. }}
  168. />
  169. </Popconfirm>
  170. </span>
  171. </Row>
  172. );
  173. });
  174. return (
  175. <div className="m-env-panel">
  176. <Layout className="project-env">
  177. <Sider width={195} style={{ background: '#fff' }}>
  178. <div style={{ height: '100%', borderRight: 0 }}>
  179. <Row className="first-menu-item menu-item">
  180. <div className="env-icon-style">
  181. <h3>
  182. 环境列表&nbsp;<Tooltip placement="top" title="在这里添加项目的环境配置">
  183. <Icon type="question-circle-o" />
  184. </Tooltip>
  185. </h3>
  186. <Tooltip title="添加环境变量">
  187. <Icon type="plus" onClick={() => this.addParams('env')} />
  188. </Tooltip>
  189. </div>
  190. </Row>
  191. <EasyDragSort data={() => env} onChange={this.handleDragMove('env')}>
  192. {envSettingItems}
  193. </EasyDragSort>
  194. </div>
  195. </Sider>
  196. <Layout className="env-content">
  197. <Content style={{ background: '#fff', padding: 24, margin: 0, minHeight: 280 }}>
  198. <ProjectEnvContent
  199. projectMsg={this.state.currentEnvMsg}
  200. onSubmit={e => this.onSubmit(e, currentKey)}
  201. handleEnvInput={e => this.handleInputChange(e, currentKey)}
  202. />
  203. </Content>
  204. </Layout>
  205. </Layout>
  206. </div>
  207. );
  208. }
  209. }
  210. export default ProjectEnv;