mock平台

InterfaceCaseContent.js 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. import React, { PureComponent as Component } from 'react';
  2. import { connect } from 'react-redux';
  3. import PropTypes from 'prop-types';
  4. import { withRouter } from 'react-router';
  5. import { Link } from 'react-router-dom';
  6. import axios from 'axios';
  7. import { message, Tooltip, Input } from 'antd';
  8. import { getEnv } from '../../../../reducer/modules/project';
  9. import {
  10. fetchInterfaceColList,
  11. setColData,
  12. fetchCaseData,
  13. fetchCaseList
  14. } from '../../../../reducer/modules/interfaceCol';
  15. import { Postman } from '../../../../components';
  16. import './InterfaceCaseContent.scss';
  17. @connect(
  18. state => {
  19. return {
  20. interfaceColList: state.interfaceCol.interfaceColList,
  21. currColId: state.interfaceCol.currColId,
  22. currCaseId: state.interfaceCol.currCaseId,
  23. currCase: state.interfaceCol.currCase,
  24. isShowCol: state.interfaceCol.isShowCol,
  25. currProject: state.project.currProject,
  26. projectEnv: state.project.projectEnv,
  27. curUid: state.user.uid
  28. };
  29. },
  30. {
  31. fetchInterfaceColList,
  32. fetchCaseData,
  33. setColData,
  34. fetchCaseList,
  35. getEnv
  36. }
  37. )
  38. @withRouter
  39. export default class InterfaceCaseContent extends Component {
  40. static propTypes = {
  41. match: PropTypes.object,
  42. interfaceColList: PropTypes.array,
  43. fetchInterfaceColList: PropTypes.func,
  44. fetchCaseData: PropTypes.func,
  45. setColData: PropTypes.func,
  46. fetchCaseList: PropTypes.func,
  47. history: PropTypes.object,
  48. currColId: PropTypes.number,
  49. currCaseId: PropTypes.number,
  50. currCase: PropTypes.object,
  51. isShowCol: PropTypes.bool,
  52. currProject: PropTypes.object,
  53. getEnv: PropTypes.func,
  54. projectEnv: PropTypes.object,
  55. curUid: PropTypes.number
  56. };
  57. state = {
  58. isEditingCasename: true,
  59. editCasename: ''
  60. };
  61. constructor(props) {
  62. super(props);
  63. }
  64. getColId(colList, currCaseId) {
  65. let currColId = 0;
  66. colList.forEach(col => {
  67. col.caseList.forEach(caseItem => {
  68. if (+caseItem._id === +currCaseId) {
  69. currColId = col._id;
  70. }
  71. });
  72. });
  73. return currColId;
  74. }
  75. async componentWillMount() {
  76. const result = await this.props.fetchInterfaceColList(this.props.match.params.id);
  77. let { currCaseId } = this.props;
  78. const params = this.props.match.params;
  79. const { actionId } = params;
  80. currCaseId = +actionId || +currCaseId || result.payload.data.data[0].caseList[0]._id;
  81. let currColId = this.getColId(result.payload.data.data, currCaseId);
  82. this.props.history.push('/project/' + params.id + '/interface/case/' + currCaseId);
  83. await this.props.fetchCaseData(currCaseId);
  84. this.props.setColData({ currCaseId: +currCaseId, currColId, isShowCol: false });
  85. // 获取当前case 下的环境变量
  86. await this.props.getEnv(this.props.currCase.project_id);
  87. // await this.getCurrEnv()
  88. this.setState({ editCasename: this.props.currCase.casename });
  89. }
  90. async componentWillReceiveProps(nextProps) {
  91. const oldCaseId = this.props.match.params.actionId;
  92. const newCaseId = nextProps.match.params.actionId;
  93. const { interfaceColList } = nextProps;
  94. let currColId = this.getColId(interfaceColList, newCaseId);
  95. if (oldCaseId !== newCaseId) {
  96. await this.props.fetchCaseData(newCaseId);
  97. this.props.setColData({ currCaseId: +newCaseId, currColId, isShowCol: false });
  98. await this.props.getEnv(this.props.currCase.project_id);
  99. // await this.getCurrEnv()
  100. this.setState({ editCasename: this.props.currCase.casename });
  101. }
  102. }
  103. savePostmanRef = postman => {
  104. this.postman = postman;
  105. };
  106. updateCase = async () => {
  107. const {
  108. case_env,
  109. req_params,
  110. req_query,
  111. req_headers,
  112. req_body_type,
  113. req_body_form,
  114. req_body_other,
  115. test_script,
  116. enable_script,
  117. test_res_body,
  118. test_res_header
  119. } = this.postman.state;
  120. const { editCasename: casename } = this.state;
  121. const { _id: id } = this.props.currCase;
  122. let params = {
  123. id,
  124. casename,
  125. case_env,
  126. req_params,
  127. req_query,
  128. req_headers,
  129. req_body_type,
  130. req_body_form,
  131. req_body_other,
  132. test_script,
  133. enable_script,
  134. test_res_body,
  135. test_res_header
  136. };
  137. const res = await axios.post('/api/col/up_case', params);
  138. if (this.props.currCase.casename !== casename) {
  139. this.props.fetchInterfaceColList(this.props.match.params.id);
  140. }
  141. if (res.data.errcode) {
  142. message.error(res.data.errmsg);
  143. } else {
  144. message.success('更新成功');
  145. this.props.fetchCaseData(id);
  146. }
  147. };
  148. triggerEditCasename = () => {
  149. this.setState({
  150. isEditingCasename: true,
  151. editCasename: this.props.currCase.casename
  152. });
  153. };
  154. cancelEditCasename = () => {
  155. this.setState({
  156. isEditingCasename: false,
  157. editCasename: this.props.currCase.casename
  158. });
  159. };
  160. render() {
  161. const { currCase, currProject, projectEnv } = this.props;
  162. const { isEditingCasename, editCasename } = this.state;
  163. const data = Object.assign(
  164. {},
  165. currCase,
  166. {
  167. env: projectEnv.env,
  168. pre_script: currProject.pre_script,
  169. after_script: currProject.after_script
  170. },
  171. { _id: currCase._id }
  172. );
  173. return (
  174. <div style={{ padding: '6px 0' }} className="case-content">
  175. <div className="case-title">
  176. {!isEditingCasename && (
  177. <Tooltip title="点击编辑" placement="bottom">
  178. <div className="case-name" onClick={this.triggerEditCasename}>
  179. {currCase.casename}
  180. </div>
  181. </Tooltip>
  182. )}
  183. {isEditingCasename && (
  184. <div className="edit-case-name">
  185. <Input
  186. value={editCasename}
  187. onChange={e => this.setState({ editCasename: e.target.value })}
  188. style={{ fontSize: 18 }}
  189. />
  190. </div>
  191. )}
  192. <span className="inter-link" style={{ margin: '0px 8px 0px 6px', fontSize: 12 }}>
  193. <Link
  194. className="text"
  195. to={`/project/${currCase.project_id}/interface/api/${currCase.interface_id}`}
  196. >
  197. 对应接口
  198. </Link>
  199. </span>
  200. </div>
  201. <div>
  202. {Object.keys(currCase).length > 0 && (
  203. <Postman
  204. data={data}
  205. type="case"
  206. saveTip="更新保存修改"
  207. save={this.updateCase}
  208. ref={this.savePostmanRef}
  209. interfaceId={currCase.interface_id}
  210. projectId={currCase.project_id}
  211. curUid={this.props.curUid}
  212. />
  213. )}
  214. </div>
  215. </div>
  216. );
  217. }
  218. }