yapi/client/containers/Project/AddProject/AddProject.js

218 lines
5.7 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';
2017-08-11 16:10:49 +08:00
import { addProject } from '../../../reducer/modules/project';
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-10 18:26:35 +08:00
import './Addproject.scss'
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-11 16:10:49 +08:00
2017-08-10 18:26:35 +08:00
class ProjectList extends Component {
constructor(props) {
super(props);
this.state = {
protocol: 'http:\/\/',
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-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) => {
2017-08-11 16:10:49 +08:00
console.log(values);
2017-08-10 18:26:35 +08:00
if (!err) {
values.protocol = this.state.protocol.split(':')[0];
addProject(values).then((res) => {
if (res.payload.data.errcode == 0) {
form.resetFields();
message.success('创建成功! ');
}
}).catch(() => {
});
}
});
}
// 修改线上域名的协议类型 (http/https)
@autobind
protocolChange(value) {
this.setState({
protocol: value
})
}
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_id', {
rules: [{
required: true, message: '请选择项目所属的分组!'
}]
})(
<Select>
2017-08-11 16:10:49 +08:00
{this.state.groupList.map((item, index) => <Option value={item._id.toString()} 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;
<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>)} />
)}
</FormItem>
<FormItem
{...formItemLayout}
label={(
<span>
基本路径&nbsp;
<Tooltip title="基本路径为空是根路径">
<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);