yapi/client/containers/AddProject/AddProject.js

192 lines
5.4 KiB
JavaScript
Raw Normal View History

2017-08-10 18:26:35 +08:00
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
2017-08-10 21:23:47 +08:00
import { Button, Form, Input, Icon, Tooltip, Select, message, Row, Col, Radio } from 'antd';
import { addProject } from '../../reducer/modules/project.js';
import { fetchGroupList } from '../../reducer/modules/group.js'
2017-08-10 18:26:35 +08:00
import { autobind } from 'core-decorators';
const { TextArea } = Input;
const FormItem = Form.Item;
const Option = Select.Option;
2017-08-10 21:23:47 +08:00
const RadioGroup = Radio.Group;
2017-08-23 15:25:35 +08:00
import { pickRandomProperty } from '../../common';
import constants from '../../constants/variable.js';
2017-08-23 10:52:12 +08:00
import { withRouter } from 'react-router';
import './Addproject.scss';
2017-08-10 18:26:35 +08:00
const formItemLayout = {
labelCol: {
2017-08-10 21:23:47 +08:00
lg: { span: 3 },
2017-08-10 18:26:35 +08:00
xs: { span: 24 },
sm: { span: 6 }
},
wrapperCol: {
2017-08-10 21:23:47 +08:00
lg: { span: 21 },
2017-08-10 18:26:35 +08:00
xs: { span: 24 },
sm: { span: 14 }
2017-08-10 21:23:47 +08:00
},
className: 'form-item'
2017-08-10 18:26:35 +08:00
};
@connect(
state => {
return {
2017-08-11 16:10:49 +08:00
groupList: state.group.groupList
2017-08-10 18:26:35 +08:00
}
},
{
2017-08-11 16:10:49 +08:00
fetchGroupList,
addProject
2017-08-10 18:26:35 +08:00
}
)
2017-08-23 10:52:12 +08:00
@withRouter
2017-08-10 18:26:35 +08:00
class ProjectList extends Component {
constructor(props) {
super(props);
this.state = {
2017-08-11 16:10:49 +08:00
groupList: []
2017-08-10 18:26:35 +08:00
}
}
static propTypes = {
2017-08-11 16:10:49 +08:00
groupList: PropTypes.array,
2017-08-10 18:26:35 +08:00
form: PropTypes.object,
addProject: PropTypes.func,
2017-08-23 10:52:12 +08:00
history: PropTypes.object,
2017-08-11 16:10:49 +08:00
fetchGroupList: PropTypes.func
2017-08-10 18:26:35 +08:00
}
// 确认添加项目
@autobind
handleOk(e) {
2017-08-11 16:10:49 +08:00
const { form, addProject } = this.props;
2017-08-10 18:26:35 +08:00
e.preventDefault();
form.validateFields((err, values) => {
if (!err) {
values.group_id = values.group.split(':')[0];
values.group_name = values.group.split(':')[1];
2017-08-23 15:25:35 +08:00
values.icon = constants.PROJECT_ICON[0];
values.color = pickRandomProperty(constants.PROJECT_COLOR);
delete values.group;
2017-08-10 18:26:35 +08:00
addProject(values).then((res) => {
if (res.payload.data.errcode == 0) {
form.resetFields();
message.success('创建成功! ');
2017-08-23 10:52:12 +08:00
this.props.history.push('/project/' + res.payload.data.data._id + '/interface/api');
2017-08-10 18:26:35 +08:00
}
}).catch(() => {
});
}
});
}
2017-08-11 16:10:49 +08:00
async componentWillMount() {
await this.props.fetchGroupList();
this.setState({groupList: this.props.groupList});
2017-08-10 18:26:35 +08:00
}
render() {
const { getFieldDecorator } = this.props.form;
return (
<div className="g-row m-container">
<Form>
<FormItem
{...formItemLayout}
label="项目名称"
>
{getFieldDecorator('name', {
rules: [{
required: true, message: '请输入项目名称!'
}]
})(
<Input />
)}
</FormItem>
2017-08-10 21:23:47 +08:00
<FormItem
{...formItemLayout}
label="所属分组"
>
{getFieldDecorator('group', {
initialValue: this.state.groupList.length > 0? this.state.groupList[0]._id.toString() + ':' + this.state.groupList[0].group_name : null ,
2017-08-10 21:23:47 +08:00
rules: [{
required: true, message: '请选择项目所属的分组!'
}]
})(
<Select>
{this.state.groupList.map((item, index) => <Option value={item._id.toString() + ':' + this.state.groupList[0].group_name} key={index}>{item.group_name}</Option>)}
2017-08-10 21:23:47 +08:00
</Select>
)}
</FormItem>
<hr className="breakline" />
2017-08-10 18:26:35 +08:00
<FormItem
{...formItemLayout}
label={(
<span>
基本路径&nbsp;
2017-08-15 12:08:59 +08:00
<Tooltip title="接口基本路径,为空是根路径">
2017-08-10 18:26:35 +08:00
<Icon type="question-circle-o" />
</Tooltip>
</span>
)}
>
{getFieldDecorator('basepath', {
rules: [{
required: false, message: '请输入项目基本路径'
}]
})(
<Input />
)}
</FormItem>
<FormItem
{...formItemLayout}
label="描述"
>
{getFieldDecorator('desc', {
rules: [{
required: false, message: '请输入描述!'
}]
})(
<TextArea rows={4} />
)}
</FormItem>
2017-08-10 21:23:47 +08:00
<FormItem
{...formItemLayout}
label="权限"
>
2017-08-11 16:10:49 +08:00
{getFieldDecorator('project_type', {
2017-08-10 21:23:47 +08:00
rules: [{
required: true
}],
2017-08-11 16:10:49 +08:00
initialValue: 'private'
2017-08-10 21:23:47 +08:00
})(
<RadioGroup>
2017-08-11 16:10:49 +08:00
<Radio value="private" className="radio">
2017-08-10 21:23:47 +08:00
<Icon type="lock" />私有<br /><span className="radio-desc">只有组长和项目开发者可以索引并查看项目信息</span>
</Radio>
<br />
2017-08-11 16:10:49 +08:00
<Radio value="public" className="radio">
2017-08-10 21:23:47 +08:00
<Icon type="unlock" />公开<br /><span className="radio-desc">任何人都可以索引并查看项目信息</span>
</Radio>
</RadioGroup>
)}
</FormItem>
2017-08-10 18:26:35 +08:00
</Form>
2017-08-10 21:23:47 +08:00
<Row>
<Col sm={{ offset: 6 }} lg={{ offset: 3 }}>
<Button className="m-btn" icon="plus" type="primary"
onClick={this.handleOk}
>创建项目</Button>
</Col>
</Row>
2017-08-10 18:26:35 +08:00
</div>
);
}
}
export default Form.create()(ProjectList);