yapi/client/containers/ProjectGroups/ProjectList/index.js

254 lines
6.2 KiB
JavaScript
Raw Normal View History

2017-07-14 18:02:01 +08:00
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
2017-07-18 21:11:32 +08:00
import { Table, Button, Modal, Form, Input, Icon, Tooltip, Select, Popconfirm } from 'antd';
import { addProject, fetchProjectList } from '../../../actions/project';
2017-07-14 16:54:20 +08:00
const { TextArea } = Input;
const FormItem = Form.Item;
const Option = Select.Option;
import './ProjectList.scss'
2017-07-18 21:11:32 +08:00
const confirm = (e) => {
console.log(e);
}
2017-07-12 10:17:57 +08:00
const columns = [{
title: '项目名称',
2017-07-12 10:17:57 +08:00
dataIndex: 'name',
key: 'name',
2017-07-12 14:18:17 +08:00
render: text => <a href="#">{text}</a>
2017-07-12 10:17:57 +08:00
}, {
title: '创建人',
dataIndex: 'owner',
key: 'owner'
2017-07-12 10:17:57 +08:00
}, {
title: '创建时间',
dataIndex: 'add_time',
key: 'add_time'
}, {
title: '操作',
2017-07-12 10:17:57 +08:00
key: 'action',
render: () => (
<span>
<a href="#">修改</a>
<span className="ant-divider" />
2017-07-18 21:11:32 +08:00
<Popconfirm title="你确定要删除项目吗?" onConfirm={confirm} okText="删除" cancelText="取消">
<a href="#">删除</a>
</Popconfirm>
2017-07-12 10:17:57 +08:00
</span>
2017-07-12 14:18:17 +08:00
)
2017-07-12 10:17:57 +08:00
}];
2017-07-14 16:54:20 +08:00
const formItemLayout = {
labelCol: {
xs: { span: 24 },
sm: { span: 6 }
},
wrapperCol: {
xs: { span: 24 },
sm: { span: 14 }
}
};
2017-07-14 18:02:01 +08:00
@connect(
state => {
return {
projectList: state.project.projectList,
2017-07-18 14:58:57 +08:00
currGroup: state.group.currGroup
2017-07-14 18:02:01 +08:00
}
},
{
fetchProjectList,
2017-07-14 18:02:01 +08:00
addProject
}
)
2017-07-14 16:54:20 +08:00
class ProjectList extends Component {
constructor(props) {
super(props);
this.state = {
visible: false,
tabelLoading: true,
protocol: 'http:\/\/',
projectData: []
2017-07-14 16:54:20 +08:00
}
}
static propTypes = {
2017-07-14 18:02:01 +08:00
form: PropTypes.object,
fetchProjectList: PropTypes.func,
addProject: PropTypes.func,
projectList: PropTypes.array,
currGroup: PropTypes.object
2017-07-14 16:54:20 +08:00
}
addProject = () => {
this.setState({
visible: true
});
}
handleOk = (e) => {
e.preventDefault();
this.props.form.validateFields((err, values) => {
if (!err) {
values.prd_host = this.state.protocol + values.prd_host;
2017-07-18 14:58:57 +08:00
// 获取当前分组id传入values
values.group_id = this.props.currGroup._id;
2017-07-18 14:58:57 +08:00
2017-07-14 16:54:20 +08:00
console.log('Received values of form: ', values);
this.setState({
visible: false,
tabelLoading: true
});
this.props.addProject(values).then((res) => {
console.log(res);
// 添加项目成功后再次请求列表
this.props.fetchProjectList({
group_id: this.props.currGroup._id
}).then((res) => {
this.setState({
tabelLoading: false
});
console.log(117,res);
});
}).catch((err) => {
console.log(err);
this.setState({
tabelLoading: false
});
2017-07-14 16:54:20 +08:00
});
2017-07-18 14:58:57 +08:00
this.props.form.resetFields();
2017-07-14 16:54:20 +08:00
}
});
}
// 取消修改
2017-07-14 16:54:20 +08:00
handleCancel = () => {
this.props.form.resetFields();
this.setState({
visible: false
});
}
// 修改线上域名的协议类型 (http/https)
protocolChange = (value) => {
this.setState({
protocol: value
})
2017-07-14 16:54:20 +08:00
}
componentWillReceiveProps(nextProps){
// 切换分组
if (this.props.currGroup !== nextProps.currGroup) {
const param = {
group_id: nextProps.currGroup._id
};
this.props.fetchProjectList(param).then((res) => {
this.setState({
tabelLoading: false
});
console.log(res);
});
}
// 切换项目列表
if (this.props.projectList !== nextProps.projectList) {
// console.log(nextProps.projectList);
const data = nextProps.projectList.map((item, index) => {
item.key = index;
return item;
});
this.setState({
projectData: data
});
}
}
2017-07-12 10:17:57 +08:00
render() {
2017-07-14 16:54:20 +08:00
const { getFieldDecorator } = this.props.form;
2017-07-12 10:17:57 +08:00
return (
2017-07-14 16:54:20 +08:00
<div>
<Modal
title="创建项目"
visible={this.state.visible}
onOk={this.handleOk}
onCancel={this.handleCancel}
>
<Form>
2017-07-14 16:54:20 +08:00
<FormItem
{...formItemLayout}
label="项目名称"
>
{getFieldDecorator('name', {
rules: [{
required: true, message: '请输入项目名称!'
}]
})(
<Input />
)}
</FormItem>
<FormItem
{...formItemLayout}
label={(
<span>
线上域名&nbsp;
<Tooltip title="将根据配置的线上域名访问mock数据">
<Icon type="question-circle-o" />
</Tooltip>
</span>
)}
>
{getFieldDecorator('prd_host', {
rules: [{
required: true, message: '请输入项目线上域名!'
}]
})(
<Input addonBefore={(
<Select defaultValue="http://" onChange={this.protocolChange}>
<Option value="http://">{'http:\/\/'}</Option>
<Option value="https://">{'https:\/\/'}</Option>
</Select>)} />
2017-07-14 16:54:20 +08:00
)}
</FormItem>
<FormItem
{...formItemLayout}
label="URL"
>
{getFieldDecorator('basepath', {
rules: [{
required: true, message: '请输入项目基本路径!'
}]
})(
<Input />
)}
</FormItem>
<FormItem
{...formItemLayout}
label="描述"
>
{getFieldDecorator('desc', {
rules: [{
required: true, message: '请输入描述!'
}]
})(
<TextArea rows={4} />
)}
</FormItem>
</Form>
</Modal>
<Table
loading={this.state.tabelLoading}
2017-07-14 16:54:20 +08:00
columns={columns}
dataSource={this.state.projectData}
2017-07-14 16:54:20 +08:00
title={() => <Button type="primary" onClick={this.addProject}>创建项目</Button>}
/>
</div>
2017-07-12 10:17:57 +08:00
);
}
}
2017-07-14 16:54:20 +08:00
export default Form.create()(ProjectList);