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

173 lines
5.3 KiB
JavaScript
Raw Normal View History

2017-08-11 21:10:54 +08:00
import React, { Component } from 'react'
2017-08-14 17:32:17 +08:00
import { connect } from 'react-redux';
import { withRouter } from 'react-router'
import PropTypes from 'prop-types'
2017-08-17 20:24:07 +08:00
import { fetchInterfaceColList, fetchInterfaceCaseList, setColData } from '../../../../reducer/modules/interfaceCol'
2017-08-14 17:32:17 +08:00
import { autobind } from 'core-decorators';
import axios from 'axios';
2017-08-17 12:19:16 +08:00
import { Input, Icon, Tag, Modal, Row, Col, message, Tooltip, Tree } from 'antd';
2017-08-14 17:32:17 +08:00
2017-08-17 12:19:16 +08:00
const TextArea = Input.TextArea;
const TreeNode = Tree.TreeNode;
2017-08-14 17:32:17 +08:00
2017-08-17 16:10:34 +08:00
import './InterfaceColMenu.scss'
2017-08-14 17:32:17 +08:00
@connect(
state => {
return {
2017-08-17 20:24:07 +08:00
interfaceColList: state.interfaceCol.interfaceColList,
currColId: state.interfaceCol.currColId,
currCaseId: state.interfaceCol.currCaseId,
isShowCol: state.interfaceCol.isShowCol
2017-08-14 17:32:17 +08:00
}
},
{
fetchInterfaceColList,
2017-08-17 20:24:07 +08:00
fetchInterfaceCaseList,
setColData
2017-08-14 17:32:17 +08:00
}
)
@withRouter
2017-08-11 21:10:54 +08:00
export default class InterfaceColMenu extends Component {
2017-08-14 17:32:17 +08:00
static propTypes = {
match: PropTypes.object,
interfaceColList: PropTypes.array,
fetchInterfaceColList: PropTypes.func,
2017-08-17 16:10:34 +08:00
fetchInterfaceCaseList: PropTypes.func,
2017-08-17 20:24:07 +08:00
setColData: PropTypes.func,
history: PropTypes.object,
currColId: PropTypes.number,
currCaseId: PropTypes.number,
isShowCol: PropTypes.bool
2017-08-14 17:32:17 +08:00
}
state = {
addColModalVisible: false,
addColName: '',
2017-08-18 11:33:37 +08:00
addColDesc: '',
expandedKeys: []
2017-08-14 17:32:17 +08:00
}
2017-08-11 21:10:54 +08:00
constructor(props) {
super(props)
}
2017-08-17 20:24:07 +08:00
// async componentWillMount() {
// const result = await this.props.fetchInterfaceColList(this.props.match.params.id)
// let params = this.props.match.params;
// if(!params.actionId){
// this.props.history.push('/project/'+params.id + '/interface/col/' + result.payload.data.data[0]._id)
// }
// }
2017-08-14 17:32:17 +08:00
2017-08-17 16:10:34 +08:00
// async componentWillReceiveProps(nextProps) {
// const result = await nextProps.fetchInterfaceColList(nextProps.match.params.id)
// let params = nextProps.match.params;
// if(!params.actionId){
// nextProps.history.replace('/project/'+params.id + '/interface/col/' + result.payload.data.data[0]._id)
// }
// }
2017-08-14 17:32:17 +08:00
@autobind
async addCol() {
const { addColName: name, addColDesc: desc } = this.state;
const project_id = this.props.match.params.id
const res = await axios.post('/api/col/add_col', { name, desc, project_id })
if (!res.data.errcode) {
this.setState({
addColModalVisible: false
});
message.success('添加集合成功');
await this.props.fetchInterfaceColList(project_id);
2017-08-17 10:17:33 +08:00
} else {
message.error(res.data.errmsg);
2017-08-14 17:32:17 +08:00
}
}
2017-08-18 11:33:37 +08:00
onSelect = (keys) => {
const type = keys[0].split('_')[0];
const id = keys[0].split('_')[1];
2017-08-17 20:24:07 +08:00
if (type === 'col') {
this.props.setColData({
isShowCol: true,
2017-08-18 11:33:37 +08:00
currColId: +id
2017-08-17 20:24:07 +08:00
})
} else {
this.props.setColData({
isShowCol: false,
2017-08-18 11:33:37 +08:00
currCaseId: +id
2017-08-17 20:24:07 +08:00
})
2017-08-14 17:32:17 +08:00
}
}
2017-08-11 21:10:54 +08:00
render() {
2017-08-17 20:24:07 +08:00
const { currColId, currCaseId, isShowCol } = this.props;
2017-08-18 11:33:37 +08:00
2017-08-14 17:32:17 +08:00
return (
<div>
<div className="interface-filter">
<Input placeholder="Filter by name" style={{ width: "70%" }} />
<Tooltip placement="bottom" title="添加集合">
<Tag color="#108ee9" style={{ marginLeft: "15px" }} onClick={() => this.setState({addColModalVisible: true})} ><Icon type="plus" /></Tag>
</Tooltip>
</div>
2017-08-17 12:19:16 +08:00
<Tree
2017-08-17 16:10:34 +08:00
className="col-list-tree"
2017-08-18 11:33:37 +08:00
expandedKeys={this.state.expandedKeys}
selectedKeys={[isShowCol ? 'col_'+currColId : 'case_'+currCaseId]}
2017-08-17 12:19:16 +08:00
onSelect={this.onSelect}
2017-08-18 11:33:37 +08:00
autoExpandParent
onExpand={keys => this.setState({expandedKeys: keys})}
2017-08-14 17:32:17 +08:00
>
{
2017-08-17 12:19:16 +08:00
this.props.interfaceColList.map((col) => (
<TreeNode
2017-08-17 20:24:07 +08:00
key={'col_' + col._id}
2017-08-18 12:00:21 +08:00
title={<span><Icon type="folder-open" style={{marginRight: 5}} /><span>{col.name}</span></span>}
2017-08-14 17:32:17 +08:00
>
{
2017-08-17 12:19:16 +08:00
col.caseList && col.caseList.map((interfaceCase) => (
<TreeNode
2017-08-17 16:10:34 +08:00
style={{width: '100%'}}
2017-08-17 20:24:07 +08:00
key={'case_' + interfaceCase._id}
2017-08-17 12:19:16 +08:00
title={interfaceCase.casename}
></TreeNode>
2017-08-14 17:32:17 +08:00
))
}
2017-08-17 12:19:16 +08:00
</TreeNode>
2017-08-14 17:32:17 +08:00
))
}
2017-08-17 12:19:16 +08:00
</Tree>
2017-08-14 17:32:17 +08:00
<Modal
title="添加集合"
visible={this.state.addColModalVisible}
onOk={this.addCol}
onCancel={() => { this.setState({ addColModalVisible: false }) }}
className="add-col-modal"
>
<Row gutter={6} className="modal-input">
<Col span="5"><div className="label">集合名</div></Col>
<Col span="15">
<Input
placeholder="请输入集合名称"
value={this.state.addColName}
onChange={e => this.setState({addColName: e.target.value})}></Input>
</Col>
</Row>
<Row gutter={6} className="modal-input">
<Col span="5"><div className="label">简介</div></Col>
<Col span="15">
<TextArea
rows={3}
placeholder="请输入集合描述"
value={this.state.addColDesc}
onChange={e => this.setState({addColDesc: e.target.value})}></TextArea>
</Col>
</Row>
</Modal>
</div>
)
2017-08-11 21:10:54 +08:00
}
}