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

308 lines
8.8 KiB
JavaScript
Raw Normal View History

2017-07-19 20:59:59 +08:00
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import { Modal, Form, Input, Icon, Tooltip, Select, message, Button } from 'antd';
import { updateProject, fetchProjectList, delProject, changeUpdateModal, changeTableLoading } from '../../../actions/project';
2017-07-19 20:59:59 +08:00
const { TextArea } = Input;
const FormItem = Form.Item;
const Option = Select.Option;
import './ProjectList.scss'
// layout
2017-07-19 20:59:59 +08:00
const formItemLayout = {
labelCol: {
xs: { span: 24 },
sm: { span: 6 }
},
wrapperCol: {
xs: { span: 24 },
sm: { span: 14 }
}
};
const formItemLayoutWithOutLabel = {
wrapperCol: {
xs: { span: 24, offset: 0 },
sm: { span: 20, offset: 4 }
}
};
let uuid = 0;
2017-07-19 20:59:59 +08:00
@connect(
state => {
return {
projectList: state.project.projectList,
isUpdateModalShow: state.project.isUpdateModalShow,
handleUpdateIndex: state.project.handleUpdateIndex,
tableLoading: state.project.tableLoading,
2017-07-19 20:59:59 +08:00
currGroup: state.group.currGroup
}
},
{
fetchProjectList,
updateProject,
delProject,
changeUpdateModal,
changeTableLoading
2017-07-19 20:59:59 +08:00
}
)
class UpDateModal extends Component {
constructor(props) {
super(props);
this.state = {
protocol: 'http:\/\/'
}
}
static propTypes = {
form: PropTypes.object,
fetchProjectList: PropTypes.func,
updateProject: PropTypes.func,
delProject: PropTypes.func,
changeUpdateModal: PropTypes.func,
changeTableLoading: PropTypes.func,
2017-07-19 20:59:59 +08:00
projectList: PropTypes.array,
currGroup: PropTypes.object,
isUpdateModalShow: PropTypes.bool,
handleUpdateIndex: PropTypes.number
}
// 修改线上域名的协议类型 (http/https)
protocolChange = (value) => {
this.setState({
protocol: value
})
}
handleCancel = () => {
this.props.form.resetFields();
this.props.changeUpdateModal(false, -1);
}
// 确认修改
2017-07-19 20:59:59 +08:00
handleOk = (e) => {
e.preventDefault();
const { form, updateProject, changeUpdateModal, currGroup, projectList, handleUpdateIndex, fetchProjectList, changeTableLoading } = this.props;
2017-07-19 20:59:59 +08:00
form.validateFields((err, values) => {
if (!err) {
console.log(projectList[handleUpdateIndex]);
2017-07-19 20:59:59 +08:00
let assignValue = Object.assign(projectList[handleUpdateIndex], values);
2017-07-20 17:53:28 +08:00
values.protocol = this.state.protocol.split(':')[0];
assignValue.env = assignValue.envs.map((item) => {
console.log(assignValue);
const arr = assignValue['envs-'+item].split(',');
if (arr.length === 2) {
return {
host: arr[0],
name: arr[1]
}
}
});
console.log(assignValue);
2017-07-19 20:59:59 +08:00
changeTableLoading(true);
2017-07-19 20:59:59 +08:00
updateProject(assignValue).then((res) => {
if (res.payload.data.errcode == 0) {
changeUpdateModal(false, -1);
message.success('修改成功! ');
fetchProjectList(currGroup._id).then((res) => {
console.log(res);
changeTableLoading(false);
2017-07-19 20:59:59 +08:00
});
} else {
changeTableLoading(false);
2017-07-19 20:59:59 +08:00
message.error(res.payload.data.errmsg);
}
}).catch((err) => {
console.log(err);
changeTableLoading(false);
2017-07-19 20:59:59 +08:00
});
form.resetFields();
}
});
}
remove = (k) => {
const { form } = this.props;
// can use data-binding to get
const envs = form.getFieldValue('envs');
// We need at least one passenger
if (envs.length === 1) {
return;
}
// can use data-binding to set
form.setFieldsValue({
envs: envs.filter(key => key !== k)
});
}
add = () => {
uuid++;
const { form } = this.props;
// can use data-binding to get
const envs = form.getFieldValue('envs');
const nextKeys = envs.concat(uuid);
// can use data-binding to set
// important! notify form to detect changes
form.setFieldsValue({
envs: nextKeys
});
}
handleSubmit = (e) => {
e.preventDefault();
this.props.form.validateFields((err, values) => {
if (!err) {
console.log('Received values of form: ', values);
}
});
}
2017-07-19 20:59:59 +08:00
render() {
const { getFieldDecorator, getFieldValue } = this.props.form;
2017-07-19 20:59:59 +08:00
// const that = this;
const { isUpdateModalShow, projectList, handleUpdateIndex } = this.props;
let initFormValues = {};
let envMessage = [];
2017-07-19 20:59:59 +08:00
// 如果列表存在且用户点击修改按钮时,设置表单默认值
if (projectList.length !== 0 && handleUpdateIndex !== -1 ) {
// console.log(projectList[handleUpdateIndex]);
const { name, basepath, desc, env } = projectList[handleUpdateIndex];
initFormValues = { name, basepath, desc, env };
if (env) {
envMessage = env.map((item) => {
return item.host + ',' + item.name;
})
}
2017-07-20 20:24:06 +08:00
initFormValues.prd_host = projectList[handleUpdateIndex].prd_host;
initFormValues.prd_protocol = projectList[handleUpdateIndex].protocol + '\/\/';
console.log(projectList);
2017-07-19 20:59:59 +08:00
}
getFieldDecorator('envs', { initialValue: envMessage });
const envs = getFieldValue('envs');
const formItems = envs.map((k, index) => {
return (
<FormItem
{...(index === 0 ? formItemLayout : formItemLayoutWithOutLabel)}
label={index === 0 ? (
<span>环境配置&nbsp;
<Tooltip title="依次输入环境域名(host)与环境名称,以英文逗号分隔">
<Icon type="question-circle-o" />
</Tooltip>
</span>) : ''}
required={false}
key={k}
>
{getFieldDecorator(`envs-${k}`, {
validateTrigger: ['onChange', 'onBlur'],
initialValue: envMessage.length !== 0 ? k : '',
rules: [{
required: false,
whitespace: true,
message: "请输入环境配置,放弃配置请清空输入框"
}]
})(
<Input placeholder="请输入环境配置" style={{ width: '60%', marginRight: 8 }} />
)}
{envs.length > 1 ? (
<Icon
className="dynamic-delete-button"
type="minus-circle-o"
disabled={envs.length === 1}
onClick={() => this.remove(k)}
/>
) : null}
</FormItem>
);
});
2017-07-19 20:59:59 +08:00
return (
<Modal
title="修改项目"
visible={isUpdateModalShow}
onOk={this.handleOk}
onCancel={this.handleCancel}
>
<Form>
<FormItem
{...formItemLayout}
label="项目名称"
>
{getFieldDecorator('name', {
initialValue: initFormValues.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', {
initialValue: initFormValues.prd_host,
rules: [{
2017-07-20 17:08:29 +08:00
required: true, message: '请输入项目线上域名!'
2017-07-19 20:59:59 +08:00
}]
})(
<Input addonBefore={(
<Select defaultValue={initFormValues.prd_protocol} onChange={this.protocolChange}>
<Option value="http://">{'http:\/\/'}</Option>
<Option value="https://">{'https:\/\/'}</Option>
</Select>)} />
)}
</FormItem>
<FormItem
{...formItemLayout}
label="基本路径"
>
{getFieldDecorator('basepath', {
initialValue: initFormValues.basepath,
rules: [{
2017-07-20 17:08:29 +08:00
required: true, message: '请输入项目基本路径! '
2017-07-19 20:59:59 +08:00
}]
})(
<Input />
)}
</FormItem>
<FormItem
{...formItemLayout}
label="描述"
>
{getFieldDecorator('desc', {
initialValue: initFormValues.desc,
rules: [{
required: true, message: '请输入描述!'
}]
})(
<TextArea rows={4} />
)}
</FormItem>
{formItems}
<FormItem {...formItemLayoutWithOutLabel}>
<Button type="dashed" onClick={this.add} style={{ width: '60%' }}>
<Icon type="plus" /> 添加环境配置
</Button>
</FormItem>
2017-07-19 20:59:59 +08:00
</Form>
</Modal>
);
}
}
export default Form.create()(UpDateModal);