yapi/client/containers/Project/Interface/InterfaceList/InterfaceList.js

172 lines
4.7 KiB
JavaScript
Raw Normal View History

2017-08-20 22:13:46 +08:00
import React, { Component } from 'react'
2017-08-21 12:05:18 +08:00
import { connect } from 'react-redux';
2017-08-18 20:35:31 +08:00
import PropTypes from 'prop-types'
import axios from 'axios'
import {
2017-08-31 14:37:43 +08:00
Table, Tag, Button, Modal, message
2017-08-18 20:35:31 +08:00
} from 'antd';
2017-08-20 22:13:46 +08:00
import { formatTime } from '../../../../common.js'
2017-08-31 14:37:43 +08:00
import AddInterfaceForm from './AddInterfaceForm';
import { fetchInterfaceList} from '../../../../reducer/modules/interface.js';
2017-08-31 20:43:38 +08:00
import { Link } from 'react-router-dom';
2017-08-21 12:05:18 +08:00
@connect(
state => {
return {
curProject: state.project.currProject
}
2017-08-31 14:37:43 +08:00
},{
fetchInterfaceList
2017-08-21 12:05:18 +08:00
})
2017-08-20 22:13:46 +08:00
class InterfaceList extends Component {
constructor(props) {
2017-08-18 20:35:31 +08:00
super(props)
this.state = {
2017-08-31 14:37:43 +08:00
visible: false,
2017-08-20 22:13:46 +08:00
data: [],
2017-08-18 20:35:31 +08:00
sortedInfo: {
2017-08-20 22:13:46 +08:00
order: 'ascend',
2017-08-18 20:35:31 +08:00
columnKey: 'title'
2017-08-31 14:37:43 +08:00
},
catid: null
2017-08-18 20:35:31 +08:00
}
}
static propTypes = {
2017-08-21 12:05:18 +08:00
match: PropTypes.object,
2017-08-31 14:37:43 +08:00
curProject: PropTypes.object,
history: PropTypes.object,
fetchInterfaceList: PropTypes.func
2017-08-18 20:35:31 +08:00
}
2017-08-20 22:13:46 +08:00
handleRequest = async (props) => {
const { params } = props.match;
if (!params.actionId) {
2017-08-18 20:35:31 +08:00
let projectId = params.id;
2017-08-31 14:37:43 +08:00
this.setState({
catid: null
})
2017-08-18 20:35:31 +08:00
let r = await axios.get('/api/interface/list?project_id=' + projectId);
this.setState({
data: r.data.data
})
2017-08-20 22:13:46 +08:00
} else if (isNaN(params.actionId)) {
2017-08-18 20:35:31 +08:00
let catid = params.actionId.substr(4)
2017-08-31 14:37:43 +08:00
this.setState({ catid: +catid })
2017-08-18 20:35:31 +08:00
let r = await axios.get('/api/interface/list_cat?catid=' + catid);
this.setState({
data: r.data.data
})
}
}
2017-08-31 14:37:43 +08:00
2017-08-18 20:35:31 +08:00
handleChange = (pagination, filters, sorter) => {
this.setState({
sortedInfo: sorter
});
}
2017-08-20 22:13:46 +08:00
componentWillMount() {
2017-08-18 20:35:31 +08:00
this.actionId = this.props.match.params.actionId;
this.handleRequest(this.props)
}
2017-08-20 22:13:46 +08:00
componentWillReceiveProps(nextProps) {
2017-08-18 20:35:31 +08:00
let _actionId = nextProps.match.params.actionId;
2017-08-20 22:13:46 +08:00
if (this.actionId !== _actionId) {
this.actionId = _actionId;
2017-08-18 20:35:31 +08:00
this.handleRequest(nextProps)
}
}
2017-08-31 14:37:43 +08:00
handleAddInterface =(data)=> {
data.project_id = this.props.curProject._id;
axios.post('/api/interface/add', data).then((res) => {
if (res.data.errcode !== 0) {
return message.error(res.data.errmsg);
}
message.success('接口添加成功')
let interfaceId = res.data.data._id;
this.props.history.push("/project/" + data.project_id + "/interface/api/" + interfaceId)
this.props.fetchInterfaceList(data.project_id)
})
}
2017-08-20 22:13:46 +08:00
render() {
2017-08-18 20:35:31 +08:00
let { sortedInfo } = this.state;
sortedInfo = sortedInfo || {};
const columns = [{
title: '接口名称',
dataIndex: 'title',
key: 'title',
2017-08-20 22:13:46 +08:00
sorter: (a, b) => {
return a.title.localeCompare(b.title) === 1
},
2017-08-31 20:43:38 +08:00
sortOrder: sortedInfo.columnKey === 'title' && sortedInfo.order,
render: (text, item)=>{
return <Link to={"/project/" + item.project_id + "/interface/api/" + item._id} >{text}</Link>
}
2017-08-20 22:13:46 +08:00
}, {
2017-08-21 12:05:18 +08:00
title: '接口路径',
2017-08-18 20:35:31 +08:00
dataIndex: 'path',
2017-08-21 12:05:18 +08:00
key: 'path',
2017-08-31 14:37:43 +08:00
render: (item) => {
2017-08-21 12:05:18 +08:00
return <span>{this.props.curProject.basepath + item}</span>
}
2017-08-20 22:13:46 +08:00
}, {
2017-08-18 20:35:31 +08:00
title: '请求方式',
dataIndex: 'method',
key: 'method'
2017-08-20 22:13:46 +08:00
}, {
title: '状态',
dataIndex: 'status',
key: 'status',
render: (item) => {
return <div>{item === 'done' ?
<Tag color="#87d068">完成</Tag>
:
<Tag color="#f50">未完成</Tag>
}</div>
},
filters: [{
text: '完成',
value: 'done'
}, {
text: '未完成',
value: 'undone'
}],
onFilter: (value, record) => record.status.indexOf(value) === 0
}, {
2017-08-28 16:42:24 +08:00
title: '更新时间',
2017-08-21 12:05:18 +08:00
dataIndex: 'up_time',
key: 'up_time',
2017-08-20 22:13:46 +08:00
render: (item) => {
return <span>{formatTime(item)}</span>
}
2017-08-18 20:35:31 +08:00
}]
2017-08-20 22:13:46 +08:00
const data = this.state.data.map(item => {
2017-08-18 20:35:31 +08:00
item.key = item._id;
return item;
});
2017-08-20 22:13:46 +08:00
2017-08-18 20:35:31 +08:00
return (
2017-08-31 14:37:43 +08:00
<div style={{ padding: "15px" }}>
<h2 style={{ display: 'inline-block'}}>接口列表</h2>
<Button style={{float: "right", marginRight: '10px'}} type="primary" onClick={() => this.setState({ visible: true })}>添加接口</Button>
<Table style={{marginTop: '20px'}} pagination={false} columns={columns} onChange={this.handleChange} dataSource={data} />
<Modal
title="添加接口"
visible={this.state.visible}
onCancel={() => this.setState({ 'visible': false })}
footer={null}
>
<AddInterfaceForm catid={this.state.catid} catdata={this.props.curProject.cat} onCancel={() => this.setState({ 'visible': false })} onSubmit={this.handleAddInterface} />
</Modal>
2017-08-20 22:13:46 +08:00
</div>
2017-08-18 20:35:31 +08:00
)
}
}
export default InterfaceList