blob: a543205ccdc15454d95747719fb187def97fe954 [file] [log] [blame]
import React, { Component } from "react";
import { Modal, Form, Select, Input, Switch, Button, message } from "antd";
import { connect } from "dva";
import styles from "../index.less";
const FormItem = Form.Item;
const { Option } = Select;
@connect(({ global }) => ({
platform: global.platform
}))
class AddModal extends Component {
constructor(props) {
super(props);
const ruleConditions = props.ruleConditions || [
{
paramType: "header",
operator: "=",
paramName: "",
paramValue: ""
}
];
this.state = {
ruleConditions
};
}
checkConditions = () => {
let { ruleConditions } = this.state;
let result = true;
if (ruleConditions) {
ruleConditions.forEach((item, index) => {
const { paramType, operator, paramName, paramValue } = item;
if (!paramType || !operator || !paramValue) {
message.destroy();
message.error(`第${index + 1}行条件不完整`);
result = false;
}
if (paramType === "uri" || paramType === "host" || paramType === "ip") {
// aaa
} else {
// eslint-disable-next-line no-lonely-if
if (!paramName) {
message.destroy();
message.error(`第${index + 1}行条件不完整`);
result = false;
}
}
});
} else {
message.destroy();
message.error(`条件不完整`);
result = false;
}
return result;
};
handleSubmit = e => {
e.preventDefault();
const { form, handleOk } = this.props;
const { ruleConditions } = this.state;
form.validateFieldsAndScroll((err, values) => {
const {
name,
matchMode,
loged,
enabled,
handle = ""
} = values;
if (!err) {
const submit = this.checkConditions();
if (submit) {
handleOk({
name,
matchMode,
handle,
loged,
enabled,
sort: Number(values.sort),
ruleConditions
});
}
}
});
};
handleAdd = () => {
let { ruleConditions } = this.state;
ruleConditions.push({
paramType: "header",
operator: "=",
paramName: "",
paramValue: ""
});
this.setState({ ruleConditions });
};
handleDelete = index => {
let { ruleConditions } = this.state;
if (ruleConditions && ruleConditions.length > 1) {
ruleConditions.splice(index, 1);
} else {
message.destroy();
message.error("至少有一个条件");
}
this.setState({ ruleConditions });
};
conditionChange = (index, name, value) => {
let { ruleConditions } = this.state;
ruleConditions[index][name] = value;
this.setState({ ruleConditions });
if (name === "paramType") {
let key = `paramTypeValueEn${index}`
if (value === "uri" || value === "host" || value === "ip") {
this.setState({ [key]: true });
} else {
this.setState({ [key]: false });
}
}
};
render() {
let {
onCancel,
form,
platform,
name = "",
matchMode = "",
handle,
loged = true,
enabled = true,
sort = ""
} = this.props;
const { ruleConditions } = this.state;
let { matchModeEnums, operatorEnums, paramTypeEnums } = platform;
if (operatorEnums) {
operatorEnums = operatorEnums.filter(item => {
return item.support === true;
});
}
if (paramTypeEnums) {
paramTypeEnums = paramTypeEnums.filter(item => {
return item.support === true;
});
}
const { getFieldDecorator } = form;
const formItemLayout = {
labelCol: {
sm: { span: 3 }
},
wrapperCol: {
sm: { span: 21 }
}
};
const formCheckLayout = {
labelCol: {
sm: { span: 18 }
},
wrapperCol: {
sm: { span: 4 }
}
};
return (
<Modal
width={800}
centered
title="规则"
visible
okText="确定"
cancelText="取消"
onOk={this.handleSubmit}
onCancel={onCancel}
>
<Form onSubmit={this.handleSubmit} className="login-form">
<FormItem label="名称" {...formItemLayout}>
{getFieldDecorator("name", {
rules: [{ required: true, message: "请输入名称" }],
initialValue: name
})(<Input placeholder="名称" />)}
</FormItem>
<FormItem label="匹配方式" {...formItemLayout}>
{getFieldDecorator("matchMode", {
rules: [{ required: true, message: "请选择匹配方式" }],
initialValue: matchMode
})(
<Select>
{matchModeEnums.map(item => {
return (
<Option key={item.code} value={item.code}>
{item.name}
</Option>
);
})}
</Select>
)}
</FormItem>
<div className={styles.ruleConditions}>
<h3 className={styles.header}>
<strong>*</strong>条件:
</h3>
<div className={styles.content}>
{ruleConditions.map((item, index) => {
return (
<ul key={index}>
<li>
<Select
onChange={value => {
this.conditionChange(index, "paramType", value);
}}
value={item.paramType}
style={{ width: 110 }}
>
{paramTypeEnums.map(type => {
return (
<Option key={type.name} value={type.name}>
{type.name}
</Option>
);
})}
</Select>
</li>
<li>
<Input
disabled={this.state[`paramTypeValueEn${index}`]}
onChange={e => {
this.conditionChange(
index,
"paramName",
e.target.value
);
}}
value={item.paramName}
style={{ width: 110 }}
/>
</li>
<li>
<Select
onChange={value => {
this.conditionChange(index, "operator", value);
}}
value={item.operator}
style={{ width: 110 }}
>
{operatorEnums.map(opearte => {
return (
<Option key={opearte.name} value={opearte.name}>
{opearte.name}
</Option>
);
})}
</Select>
</li>
<li>
<Input
onChange={e => {
this.conditionChange(
index,
"paramValue",
e.target.value
);
}}
value={item.paramValue}
style={{ width: 110 }}
/>
</li>
<li>
<Button
type="danger"
onClick={() => {
this.handleDelete(index);
}}
>
删除
</Button>
</li>
</ul>
);
})}
</div>
<div>
<Button onClick={this.handleAdd} type="primary">
新增
</Button>
</div>
</div>
<FormItem label="处理" {...formItemLayout}>
{getFieldDecorator("handle", {
initialValue: handle,
})(<Input placeholder="处理" />)}
</FormItem>
<div className={styles.layout}>
<FormItem
style={{ margin: "0 30px" }}
{...formCheckLayout}
label="打印日志"
>
{getFieldDecorator("loged", {
initialValue: loged,
valuePropName: "checked",
rules: [{ required: true }]
})(<Switch />)}
</FormItem>
<FormItem {...formCheckLayout} label="是否开启">
{getFieldDecorator("enabled", {
initialValue: enabled,
valuePropName: "checked",
rules: [{ required: true }]
})(<Switch />)}
</FormItem>
</div>
<FormItem label="执行顺序" {...formItemLayout}>
{getFieldDecorator("sort", {
initialValue: sort,
rules: [
{
required: true,
message: "请输入1-100数字"
},
{
pattern: /^([1-9][0-9]{0,1}|100)$/,
message: "请输入1-100数字"
}
]
})(<Input placeholder="可以填写1-100之间的数字标志执行先后顺序" />)}
</FormItem>
</Form>
</Modal>
);
}
}
export default Form.create()(AddModal);