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

392 lines
13 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-09-29 15:41:52 +08:00
import { fetchInterfaceColList, fetchInterfaceCaseList, setColData, fetchCaseList } from '../../../../reducer/modules/interfaceCol'
import { fetchInterfaceList } from '../../../../reducer/modules/interface.js';
2017-08-14 17:32:17 +08:00
import axios from 'axios';
2017-09-27 20:36:17 +08:00
// import { Input, Icon, Button, Modal, message, Tooltip, Tree, Dropdown, Menu, Form } from 'antd';
import ImportInterface from './ImportInterface'
import { Input, Icon, Button, Modal, message, Tooltip, Tree, Form } from 'antd';
2017-08-14 17:32:17 +08:00
2017-08-17 12:19:16 +08:00
const TreeNode = Tree.TreeNode;
2017-08-22 16:50:15 +08:00
const FormItem = Form.Item;
2017-08-22 18:05:20 +08:00
const confirm = Modal.confirm;
2017-08-14 17:32:17 +08:00
2017-08-17 16:10:34 +08:00
import './InterfaceColMenu.scss'
2017-08-22 16:50:15 +08:00
const ColModalForm = Form.create()((props) => {
const { visible, onCancel, onCreate, form, title } = props;
const { getFieldDecorator } = form;
return (
<Modal
visible={visible}
title={title}
onCancel={onCancel}
onOk={onCreate}
>
<Form layout="vertical">
<FormItem label="集合名">
{getFieldDecorator('colName', {
rules: [{ required: true, message: '请输入集合命名!' }]
})(
<Input />
)}
</FormItem>
<FormItem label="简介">
{getFieldDecorator('colDesc')(<Input type="textarea" />)}
</FormItem>
</Form>
</Modal>
)
});
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,
list: state.inter.list
2017-08-14 17:32:17 +08:00
}
},
{
fetchInterfaceColList,
2017-08-17 20:24:07 +08:00
fetchInterfaceCaseList,
fetchInterfaceList,
2017-09-29 15:41:52 +08:00
fetchCaseList,
2017-08-17 20:24:07 +08:00
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,
fetchInterfaceList: PropTypes.func,
2017-09-29 15:41:52 +08:00
fetchCaseList: 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,
list: PropTypes.array
2017-08-14 17:32:17 +08:00
}
state = {
2017-08-22 16:50:15 +08:00
expandedKeys: [],
colModalType: '',
colModalVisible: false,
2017-09-04 11:52:49 +08:00
editColId: 0,
filterValue: '',
2017-09-29 14:24:07 +08:00
importInterVisible: false,
importInterIds: [],
importColId: 0
2017-08-14 17:32:17 +08:00
}
2017-08-11 21:10:54 +08:00
constructor(props) {
super(props)
}
2017-08-18 17:31:48 +08:00
async componentWillMount() {
const { isShowCol, currColId, currCaseId } = this.props;
const action = isShowCol ? 'col' : 'case';
const actionId = isShowCol ? currColId : currCaseId;
this.setState({expandedKeys: [action+'_'+actionId]})
}
2017-08-14 17:32:17 +08:00
2017-08-18 17:31:48 +08:00
async componentWillReceiveProps(nextProps) {
2017-08-18 21:11:09 +08:00
const { currColId } = nextProps;
2017-08-18 17:56:26 +08:00
let expandedKeys = this.state.expandedKeys;
2017-08-18 21:11:09 +08:00
if (expandedKeys.indexOf('col_'+currColId) === -1) {
expandedKeys = expandedKeys.concat(['col_'+currColId])
2017-08-18 17:56:26 +08:00
}
this.setState({expandedKeys})
2017-08-18 17:31:48 +08:00
}
2017-08-17 16:10:34 +08:00
2017-09-29 14:24:07 +08:00
addorEditCol = async () => {
2017-08-22 16:50:15 +08:00
const { colName: name, colDesc: desc } = this.form.getFieldsValue();
const { colModalType, editColId: col_id } = this.state;
const project_id = this.props.match.params.id;
let res = {};
if (colModalType === 'add') {
res = await axios.post('/api/col/add_col', { name, desc, project_id })
} else if (colModalType === 'edit') {
res = await axios.post('/api/col/up_col', { name, desc, col_id })
}
2017-08-14 17:32:17 +08:00
if (!res.data.errcode) {
this.setState({
2017-08-22 16:50:15 +08:00
colModalVisible: false
2017-08-14 17:32:17 +08:00
});
2017-08-22 16:50:15 +08:00
message.success(colModalType === 'edit' ? '修改集合成功' : '添加集合成功');
2017-08-14 17:32:17 +08:00
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 21:11:09 +08:00
onExpand = (keys) => {
this.setState({expandedKeys: keys})
}
2017-08-18 11:33:37 +08:00
onSelect = (keys) => {
2017-08-18 17:31:48 +08:00
if (keys.length) {
const type = keys[0].split('_')[0];
const id = keys[0].split('_')[1];
const project_id = this.props.match.params.id
if (type === 'col') {
this.props.setColData({
isShowCol: true,
currColId: +id
})
this.props.history.push('/project/' + project_id + '/interface/col/' + id)
} else {
this.props.setColData({
isShowCol: false,
currCaseId: +id
})
this.props.history.push('/project/' + project_id + '/interface/case/' + id)
}
2017-08-14 17:32:17 +08:00
}
}
2017-08-22 18:05:20 +08:00
showDelColConfirm = (colId) => {
let that = this;
confirm({
title: '您确认删除此测试集合',
content: '温馨提示:该操作会删除该集合下所有测试用例,用例删除后无法恢复',
async onOk() {
const res = await axios.get('/api/col/del_col?col_id=' + colId)
if (!res.data.errcode) {
message.success('删除集合成功');
await that.props.fetchInterfaceColList(that.props.match.params.id);
} else {
message.error(res.data.errmsg);
}
2017-08-22 19:57:39 +08:00
}
});
}
showDelCaseConfirm = (caseId) => {
let that = this;
2017-09-30 20:34:54 +08:00
const params = this.props.match.params;
2017-08-22 19:57:39 +08:00
confirm({
title: '您确认删除此测试用例',
content: '温馨提示:用例删除后无法恢复',
async onOk() {
const res = await axios.get('/api/col/del_case?caseid=' + caseId)
if (!res.data.errcode) {
message.success('删除用例成功');
2017-09-30 20:34:54 +08:00
// 如果删除当前选中 case切换路由到集合
if (+caseId === +that.props.currCaseId) {
that.props.history.push('/project/' + params.id + '/interface/col/')
} else {
that.props.fetchInterfaceColList(that.props.match.params.id);
}
2017-08-22 19:57:39 +08:00
} else {
message.error(res.data.errmsg);
}
}
2017-08-22 18:05:20 +08:00
});
}
2017-08-22 16:50:15 +08:00
showColModal = (type, col) => {
const editCol = type === 'edit' ? {colName: col.name, colDesc: col.desc} : {colName: '', colDesc: ''};
this.setState({
colModalVisible: true,
colModalType: type || 'add',
2017-08-22 18:05:20 +08:00
editColId: col && col._id
2017-08-22 16:50:15 +08:00
})
this.form.setFieldsValue(editCol)
}
saveFormRef = (form) => {
this.form = form;
}
2017-08-14 17:32:17 +08:00
selectInterface = (importInterIds) => {
2017-09-29 14:51:28 +08:00
// console.log(importInterIds)
this.setState({ importInterIds })
}
2017-09-29 14:24:07 +08:00
showImportInterfaceModal = async (colId) => {
const projectId = this.props.match.params.id;
2017-09-29 14:24:07 +08:00
await this.props.fetchInterfaceList(projectId)
this.setState({ importInterVisible: true, importColId: colId })
}
2017-09-29 15:41:52 +08:00
handleImportOk = async () => {
const project_id = this.props.match.params.id;
const { importColId, importInterIds } = this.state;
const res = await axios.post('/api/col/add_case_list', {
interface_list: importInterIds,
col_id: importColId,
project_id
})
if (!res.data.errcode) {
this.setState({ importInterVisible: false })
message.success('导入集合成功');
await this.props.fetchInterfaceColList(project_id);
// if (this.props.isShowCol) {
// await this.props.fetchCaseList(this.props.currColId);
// }
} else {
message.error(res.data.errmsg);
}
2017-09-29 14:24:07 +08:00
}
handleImportCancel = () => {
this.setState({ importInterVisible: false })
}
2017-09-04 11:52:49 +08:00
filterCol = (e) => {
const value = e.target.value;
this.setState({filterValue: value})
}
2017-10-12 20:45:34 +08:00
onDrop = async (e) => {
const projectId = this.props.match.params.id;
const dropColIndex = e.node.props.pos.split('-')[1];
const dropColId = this.props.interfaceColList[dropColIndex]._id;
const id = e.dragNode.props.eventKey;
const dragColIndex = e.dragNode.props.pos.split('-')[1];
const dragColId = this.props.interfaceColList[dragColIndex]._id;
if (id.indexOf('col') === -1 && dropColId !== dragColId) {
await axios.post('/api/col/up_case', {id: id.split('_')[1], col_id: dropColId});
this.props.fetchInterfaceColList(projectId);
}
}
2017-08-11 21:10:54 +08:00
render() {
const { currColId, currCaseId, isShowCol } = this.props;
2017-09-29 14:24:07 +08:00
const { colModalType, colModalVisible, filterValue, importInterVisible } = this.state;
2017-08-22 16:50:15 +08:00
// const menu = (col) => {
// return (
// <Menu>
// <Menu.Item>
// <span onClick={() => this.showColModal('edit', col)}>修改集合</span>
// </Menu.Item>
// <Menu.Item>
// <span onClick={() => {
// this.showDelColConfirm(col._id)
// }}>删除集合</span>
// </Menu.Item>
2017-09-27 20:36:17 +08:00
// <Menu.Item>
// <span onClick={() => this.showImportInterface(col._id)}>导入接口</span>
// </Menu.Item>
// </Menu>
// )
// };
2017-08-18 11:33:37 +08:00
2017-09-24 10:31:43 +08:00
let isFilterCat = false;
2017-10-20 10:16:43 +08:00
// console.log();
// let caseList = this.props.interfaceColList.caseList;
// if(caseList&&caseList.length>1){
// caseList = caseList.sort((a,b)=>{
// return a.index-b.index;
// });
// this.props.interfaceColList.caseList = caseList;
// }
2017-08-14 17:32:17 +08:00
return (
<div>
<div className="interface-filter">
2017-09-20 21:20:20 +08:00
<Input placeholder="搜索测试集合" onChange={this.filterCol} />
2017-08-14 17:32:17 +08:00
<Tooltip placement="bottom" title="添加集合">
2017-09-20 21:20:20 +08:00
<Button type="primary" style={{ marginLeft: "16px" }} onClick={() => this.showColModal('add')} className="btn-filter" >添加集合</Button>
2017-08-14 17:32:17 +08:00
</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
2017-08-18 21:11:09 +08:00
onExpand={this.onExpand}
2017-10-12 20:45:34 +08:00
draggable
onDrop={this.onDrop}
2017-08-14 17:32:17 +08:00
>
{
2017-09-24 10:31:43 +08:00
this.props.interfaceColList.filter(col =>{
if(col.name.indexOf(filterValue) !== -1){
isFilterCat = true;
return true;
}
isFilterCat = false;
2017-09-24 10:31:43 +08:00
let caseList = col.caseList.filter(item=>{
return item.casename.indexOf(filterValue) !== -1
})
return caseList.length > 0;
}).map((col) => (
2017-08-17 12:19:16 +08:00
<TreeNode
2017-08-17 20:24:07 +08:00
key={'col_' + col._id}
2017-08-22 16:50:15 +08:00
title={
<div className="menu-title">
<span><Icon type="folder-open" style={{marginRight: 5}} /><span>{col.name}</span></span>
<div className="btns">
<Tooltip title="删除集合">
<Icon type='delete' className="interface-delete-icon" onClick={(e) => {e.stopPropagation();this.showDelColConfirm(col._id)}} />
</Tooltip>
<Tooltip title="编辑集合">
<Icon type='edit' className="interface-delete-icon" onClick={(e) => {e.stopPropagation();this.showColModal('edit', col)}} />
</Tooltip>
2017-09-30 11:00:40 +08:00
<Tooltip title="导入接口">
<Icon type='plus' className="interface-delete-icon" onClick={(e) => {e.stopPropagation();this.showImportInterfaceModal(col._id)}} />
</Tooltip>
</div>
{/*<Dropdown overlay={menu(col)} trigger={['click']} onClick={e => e.stopPropagation()}>
2017-09-19 17:59:36 +08:00
<Icon className="opts-icon" type='ellipsis'/>
</Dropdown>*/}
2017-08-22 16:50:15 +08:00
</div>
}
2017-08-14 17:32:17 +08:00
>
{
2017-09-24 10:31:43 +08:00
col.caseList && col.caseList.filter((item)=>{
if(isFilterCat){
return true;
}
return item.casename.indexOf(filterValue) !== -1
2017-10-20 10:16:43 +08:00
}).sort((a,b)=>{
return a.index-b.index;
2017-09-24 10:31:43 +08:00
}).map((interfaceCase) => (
2017-08-17 12:19:16 +08:00
<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-22 16:50:15 +08:00
title={
<div className="menu-title" title={interfaceCase.casename}>
2017-10-25 14:39:07 +08:00
<span className="casename">{interfaceCase.casename}</span>
<Tooltip title="删除用例">
<Icon type='delete' className="case-delete-icon" onClick={(e) => { e.stopPropagation();this.showDelCaseConfirm(interfaceCase._id) }} />
</Tooltip>
2017-08-22 16:50:15 +08:00
</div>
}
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
</TreeNode>
2017-08-14 17:32:17 +08:00
))
}
2017-08-17 12:19:16 +08:00
</Tree>
2017-08-22 16:50:15 +08:00
<ColModalForm
ref={this.saveFormRef}
type={colModalType}
visible={colModalVisible}
onCancel={() => { this.setState({ colModalVisible: false }) }}
onCreate={this.addorEditCol}
></ColModalForm>
2017-09-29 14:24:07 +08:00
<Modal
title="导入接口到集合"
visible={importInterVisible}
onOk={this.handleImportOk}
onCancel={this.handleImportCancel}
width={800}
>
2017-09-29 14:45:48 +08:00
<ImportInterface onChange={this.selectInterface} list={this.props.list} />
2017-09-29 14:24:07 +08:00
</Modal>
2017-08-14 17:32:17 +08:00
</div>
)
2017-08-11 21:10:54 +08:00
}
}