import React, { PureComponent as Component } from 'react';
import { connect } from 'react-redux';
import PropTypes from 'prop-types';
import axios from 'axios';
import { Table, Button, Modal, message, Tooltip, Select, Icon } from 'antd';
import AddInterfaceForm from './AddInterfaceForm';
import {
fetchInterfaceListMenu,
fetchInterfaceList,
fetchInterfaceCatList
} from '../../../../reducer/modules/interface.js';
import { getProject } from '../../../../reducer/modules/project.js';
import { Link } from 'react-router-dom';
import variable from '../../../../constants/variable';
import './Edit.scss';
import Label from '../../../../components/Label/Label.js';
const Option = Select.Option;
const limit = 20;
@connect(
state => {
return {
curData: state.inter.curdata,
curProject: state.project.currProject,
catList: state.inter.list,
totalTableList: state.inter.totalTableList,
catTableList: state.inter.catTableList,
totalCount: state.inter.totalCount,
count: state.inter.count
};
},
{
fetchInterfaceListMenu,
fetchInterfaceList,
fetchInterfaceCatList,
getProject
}
)
class InterfaceList extends Component {
constructor(props) {
super(props);
this.state = {
visible: false,
data: [],
filteredInfo: {},
catid: null,
total: null,
current: 1
};
}
static propTypes = {
curData: PropTypes.object,
catList: PropTypes.array,
match: PropTypes.object,
curProject: PropTypes.object,
history: PropTypes.object,
fetchInterfaceListMenu: PropTypes.func,
fetchInterfaceList: PropTypes.func,
fetchInterfaceCatList: PropTypes.func,
totalTableList: PropTypes.array,
catTableList: PropTypes.array,
totalCount: PropTypes.number,
count: PropTypes.number,
getProject: PropTypes.func
};
handleRequest = async props => {
const { params } = props.match;
if (!params.actionId) {
let projectId = params.id;
this.setState({
catid: null
});
let option = {
page: this.state.current,
limit,
project_id: projectId,
status: this.state.filteredInfo.status,
tag: this.state.filteredInfo.tag
};
await this.props.fetchInterfaceList(option);
} else if (isNaN(params.actionId)) {
let catid = params.actionId.substr(4);
this.setState({catid: +catid});
let option = {
page: this.state.current,
limit,
catid,
status: this.state.filteredInfo.status,
tag: this.state.filteredInfo.tag
};
await this.props.fetchInterfaceCatList(option);
}
};
// 更新分类简介
handleChangeInterfaceCat = (desc, name) => {
let params = {
catid: this.state.catid,
name: name,
desc: desc
};
axios.post('/api/interface/up_cat', params).then(async res => {
if (res.data.errcode !== 0) {
return message.error(res.data.errmsg);
}
let project_id = this.props.match.params.id;
await this.props.getProject(project_id);
await this.props.fetchInterfaceListMenu(project_id);
message.success('接口集合简介更新成功');
});
};
handleChange = (pagination, filters, sorter) => {
this.setState({
current: pagination.current || 1,
sortedInfo: sorter,
filteredInfo: filters
}, () => this.handleRequest(this.props));
};
UNSAFE_componentWillMount() {
this.actionId = this.props.match.params.actionId;
this.handleRequest(this.props);
}
UNSAFE_componentWillReceiveProps(nextProps) {
let _actionId = nextProps.match.params.actionId;
if (this.actionId !== _actionId) {
this.actionId = _actionId;
this.setState(
{
current: 1
},
() => this.handleRequest(nextProps)
);
}
}
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.fetchInterfaceListMenu(data.project_id);
});
};
changeInterfaceCat = async (id, catid) => {
const params = {
id: id,
catid
};
let result = await axios.post('/api/interface/up', params);
if (result.data.errcode === 0) {
message.success('修改成功');
this.handleRequest(this.props);
this.props.fetchInterfaceListMenu(this.props.curProject._id);
} else {
message.error(result.data.errmsg);
}
};
changeInterfaceStatus = async value => {
const params = {
id: value.split('-')[0],
status: value.split('-')[1]
};
let result = await axios.post('/api/interface/up', params);
if (result.data.errcode === 0) {
message.success('修改成功');
this.handleRequest(this.props);
} else {
message.error(result.data.errmsg);
}
};
//page change will be processed in handleChange by pagination
// changePage = current => {
// if (this.state.current !== current) {
// this.setState(
// {
// current: current
// },
// () => this.handleRequest(this.props)
// );
// }
// };
render() {
let tag = this.props.curProject.tag;
let tagFilter = tag.map(item => {
return {text: item.name, value: item.name};
});
const columns = [
{
title: '接口名称',
dataIndex: 'title',
key: 'title',
width: 30,
render: (text, item) => {
return (