yapi/client/containers/Project/Interface/InterfaceCol/InterfaceCaseContent.js

201 lines
6.1 KiB
JavaScript
Raw Normal View History

2017-08-18 11:33:37 +08:00
import React, { Component } from 'react'
import { connect } from 'react-redux';
import PropTypes from 'prop-types'
import { withRouter } from 'react-router'
2017-08-31 15:18:37 +08:00
import { Link } from 'react-router-dom'
2017-08-24 11:11:38 +08:00
import axios from 'axios'
2017-09-19 15:24:43 +08:00
import { message, Tooltip, Input } from 'antd'
import { fetchInterfaceColList, setColData, fetchCaseData, fetchCaseList } from '../../../../reducer/modules/interfaceCol'
2017-08-22 09:41:20 +08:00
import { Postman } from '../../../../components'
2017-08-18 11:33:37 +08:00
2017-09-19 15:24:43 +08:00
import './InterfaceCaseContent.scss'
2017-08-18 11:33:37 +08:00
@connect(
state => {
return {
interfaceColList: state.interfaceCol.interfaceColList,
currColId: state.interfaceCol.currColId,
currCaseId: state.interfaceCol.currCaseId,
2017-08-18 17:31:48 +08:00
currCase: state.interfaceCol.currCase,
2017-08-22 09:41:20 +08:00
isShowCol: state.interfaceCol.isShowCol,
currProject: state.project.currProject
2017-08-18 11:33:37 +08:00
}
},
{
fetchInterfaceColList,
2017-08-18 17:31:48 +08:00
fetchCaseData,
2017-09-19 15:24:43 +08:00
setColData,
fetchCaseList
2017-08-18 11:33:37 +08:00
}
)
@withRouter
export default class InterfaceCaseContent extends Component {
static propTypes = {
match: PropTypes.object,
interfaceColList: PropTypes.array,
fetchInterfaceColList: PropTypes.func,
2017-08-18 17:31:48 +08:00
fetchCaseData: PropTypes.func,
2017-08-18 11:33:37 +08:00
setColData: PropTypes.func,
2017-09-19 15:24:43 +08:00
fetchCaseList: PropTypes.func,
2017-08-18 11:33:37 +08:00
history: PropTypes.object,
currColId: PropTypes.number,
currCaseId: PropTypes.number,
2017-08-18 17:31:48 +08:00
currCase: PropTypes.object,
2017-08-22 09:41:20 +08:00
isShowCol: PropTypes.bool,
currProject: PropTypes.object
2017-08-18 11:33:37 +08:00
}
2017-09-19 15:24:43 +08:00
state = {
isEditingCasename: true,
editCasename: ''
}
2017-08-18 11:33:37 +08:00
constructor(props) {
super(props)
}
2017-08-18 21:11:09 +08:00
getColId(colList, currCaseId) {
let currColId = 0;
colList.forEach(col => {
col.caseList.forEach(caseItem => {
if (+caseItem._id === currCaseId) {
currColId = col._id;
}
})
})
return currColId;
}
2017-08-18 11:33:37 +08:00
async componentWillMount() {
const result = await this.props.fetchInterfaceColList(this.props.match.params.id)
2017-08-18 17:31:48 +08:00
let { currCaseId } = this.props;
2017-08-18 11:33:37 +08:00
const params = this.props.match.params;
const { actionId } = params;
currCaseId = +actionId || +currCaseId || result.payload.data.data[0].caseList[0]._id;
2017-08-18 21:11:09 +08:00
let currColId = this.getColId(result.payload.data.data, currCaseId);
2017-08-18 17:31:48 +08:00
this.props.history.push('/project/' + params.id + '/interface/case/' + currCaseId)
2017-09-19 15:24:43 +08:00
await this.props.fetchCaseData(currCaseId)
2017-08-18 21:11:09 +08:00
this.props.setColData({currCaseId: +currCaseId, currColId, isShowCol: false})
2017-09-19 15:24:43 +08:00
this.setState({editCasename: this.props.currCase.casename})
2017-08-18 17:31:48 +08:00
}
2017-09-19 15:24:43 +08:00
async componentWillReceiveProps(nextProps) {
2017-08-18 17:31:48 +08:00
const oldCaseId = this.props.match.params.actionId
const newCaseId = nextProps.match.params.actionId
if (oldCaseId !== newCaseId) {
2017-08-18 21:11:09 +08:00
let currColId = this.getColId(this.props.interfaceColList, newCaseId);
2017-09-19 15:24:43 +08:00
await this.props.fetchCaseData(newCaseId);
2017-08-18 21:11:09 +08:00
this.props.setColData({currCaseId: +newCaseId, currColId, isShowCol: false})
2017-09-19 15:24:43 +08:00
this.setState({editCasename: this.props.currCase.casename})
2017-08-18 11:33:37 +08:00
}
}
2017-08-24 11:11:38 +08:00
savePostmanRef = (postman) => {
this.postman = postman;
}
updateCase = async () => {
2017-09-20 11:45:18 +08:00
2017-08-24 11:11:38 +08:00
const {
2017-08-29 10:08:55 +08:00
caseEnv: case_env,
2017-08-24 11:11:38 +08:00
pathname: path,
method,
pathParam: req_params,
query: req_query,
headers: req_headers,
bodyType: req_body_type,
bodyForm: req_body_form,
bodyOther: req_body_other
} = this.postman.state;
2017-09-20 11:45:18 +08:00
2017-09-19 15:24:43 +08:00
const {editCasename: casename} = this.state;
const {_id: id} = this.props.currCase;
2017-09-20 11:45:18 +08:00
let params = {
2017-08-24 11:11:38 +08:00
id,
casename,
2017-08-29 10:08:55 +08:00
case_env,
2017-08-24 11:11:38 +08:00
path,
method,
req_params,
req_query,
req_headers,
req_body_type,
req_body_form,
req_body_other
2017-09-20 11:45:18 +08:00
};
if(this.postman.state.test_status !== 'error'){
params.test_res_body = this.postman.state.res;
params.test_report = this.postman.state.validRes;
params.test_status = this.postman.state.test_status;
params.test_res_header = this.postman.state.resHeader;
}
if(params.test_res_body && typeof params.test_res_body === 'object'){
params.test_res_body = JSON.stringify(params.test_res_body, null, ' ');
}
const res = await axios.post('/api/col/up_case', params);
2017-09-19 15:24:43 +08:00
if (this.props.currCase.casename !== casename) {
this.props.fetchInterfaceColList(this.props.match.params.id);
}
2017-08-24 11:11:38 +08:00
if (res.data.errcode) {
message.error(res.data.errmsg)
} else {
message.success('更新成功')
2017-08-29 10:20:57 +08:00
this.props.fetchCaseData(id);
2017-08-24 11:11:38 +08:00
}
2017-08-24 10:44:02 +08:00
}
2017-08-23 15:50:07 +08:00
2017-09-19 15:24:43 +08:00
triggerEditCasename = () => {
this.setState({
isEditingCasename: true,
editCasename: this.props.currCase.casename
})
}
cancelEditCasename = () => {
this.setState({
isEditingCasename: false,
editCasename: this.props.currCase.casename
})
}
2017-08-18 11:33:37 +08:00
render() {
2017-08-23 11:41:46 +08:00
const { currCase, currProject } = this.props;
2017-09-19 15:24:43 +08:00
const { isEditingCasename, editCasename } = this.state;
2017-08-23 14:59:24 +08:00
const data = Object.assign({}, currCase, currProject, {_id: currCase._id});
2017-08-18 17:31:48 +08:00
return (
2017-09-19 15:24:43 +08:00
<div style={{padding: '6px 0'}} className="case-content">
<div className="case-title">
{!isEditingCasename && <Tooltip title="点击编辑" placement="bottom"><div className="case-name" onClick={this.triggerEditCasename}>
{currCase.casename}
</div></Tooltip>}
{isEditingCasename && <div className="edit-case-name">
<Input value={editCasename} onChange={e => this.setState({editCasename: e.target.value})} style={{fontSize: 18}} />
{/*<Button
title="Enter"
onClick={this.saveCasename}
type="primary"
style={{ marginLeft: 8 }}
>保存</Button>
<Button
title="Esc"
onClick={this.cancelEditCasename}
type="primary"
style={{ marginLeft: 8 }}
>取消</Button>*/}
</div>}
<span className="inter-link" style={{margin: '0px 8px 0px 6px', fontSize: 12}}>
<Link className="text" to={`/project/${currProject._id}/interface/api/${currCase.interface_id}`}>对应接口</Link>
2017-08-31 15:18:37 +08:00
</span>
2017-09-19 15:24:43 +08:00
</div>
2017-08-22 09:41:20 +08:00
<div>
2017-08-24 11:11:38 +08:00
<Postman data={data} type="case" saveTip="更新保存修改" save={this.updateCase} ref={this.savePostmanRef} />
2017-08-22 09:41:20 +08:00
</div>
2017-08-18 17:31:48 +08:00
</div>
)
2017-08-18 11:33:37 +08:00
}
}