yapi/client/containers/AddInterface/ReqHeader/ReqList.js

136 lines
3.9 KiB
JavaScript
Raw Normal View History

2017-07-18 12:53:53 +08:00
import React, { Component } from 'react'
2017-07-24 19:43:13 +08:00
import { Select, Input, Icon } from 'antd'
2017-07-19 15:12:10 +08:00
import PropTypes from 'prop-types'
import { connect } from 'react-redux'
import { autobind } from 'core-decorators'
2017-08-07 19:34:34 +08:00
import {
2017-07-19 15:12:10 +08:00
reqTagValue,
reqHeaderValue,
2017-07-26 19:07:29 +08:00
deleteReqHeader,
addReqHeader
2017-08-08 10:07:55 +08:00
} from '../../../reducer/modules/addInterface.js'
2017-07-19 15:12:10 +08:00
@connect(
state => {
return {
seqGroup: state.addInterface.seqGroup,
reqTagValue: state.addInterface.reqTagValue,
reqHeaderValue: state.addInterface.reqHeaderValue
}
},
{
reqTagValue,
reqHeaderValue,
2017-07-26 19:07:29 +08:00
deleteReqHeader,
addReqHeader
2017-07-19 15:12:10 +08:00
}
)
2017-07-18 12:53:53 +08:00
class ReqList extends Component {
2017-07-19 15:12:10 +08:00
static propTypes = {
seqGroup: PropTypes.array,
reqTagValue: PropTypes.func,
reqHeaderValue: PropTypes.func,
deleteReqHeader: PropTypes.func,
2017-07-26 19:07:29 +08:00
addReqHeader: PropTypes.func,
2017-07-24 10:59:17 +08:00
_id: PropTypes.number,
dataNum: PropTypes.number,
value: PropTypes.object
2017-07-19 15:12:10 +08:00
}
2017-07-18 12:53:53 +08:00
constructor(props) {
super(props)
}
2017-07-19 19:29:18 +08:00
@autobind
2017-07-19 15:12:10 +08:00
handleChange (value) {
2017-07-24 10:59:17 +08:00
const dir = 'AddInterface/edit'
const url = location.href
2017-07-26 19:07:29 +08:00
const newObject = []
2017-07-24 10:59:17 +08:00
if (url.includes(dir)) {
const { seqGroup, value: { id } } = this.props
2017-07-26 21:10:23 +08:00
seqGroup.forEach(v => {
if (id == v.id) {
v.name = value
}
})
2017-07-26 19:07:29 +08:00
seqGroup.forEach(v => {
const {id, name, value} = v
newObject.push({id, name, value})
})
this.props.addReqHeader( newObject )
2017-07-24 10:59:17 +08:00
} else {
const { seqGroup, dataNum } = this.props
2017-07-26 21:10:23 +08:00
seqGroup.forEach(v => {
if (dataNum == v.id) {
v.name = value
}
})
seqGroup.forEach(v => {
const {id, name, value} = v
newObject.push({id, name, value})
2017-08-07 19:34:34 +08:00
})
2017-07-26 21:10:23 +08:00
this.props.addReqHeader(newObject)
2017-07-24 10:59:17 +08:00
}
2017-07-19 15:12:10 +08:00
}
2017-07-19 19:29:18 +08:00
@autobind
2017-07-19 15:12:10 +08:00
handleBlur (e) {
const value = e.target.value
2017-07-24 10:59:17 +08:00
const { seqGroup, value: { id } } = this.props
2017-07-26 19:07:29 +08:00
const newObject = []
2017-07-26 21:10:23 +08:00
seqGroup.forEach(v => {
if (id == v.id) {
v.value = value
}
2017-08-07 19:34:34 +08:00
})
2017-07-26 19:07:29 +08:00
seqGroup.forEach(v => {
const {id, name, value} = v
newObject.push({id, name, value})
})
this.props.addReqHeader(newObject)
2017-07-19 15:12:10 +08:00
}
@autobind
2017-07-24 10:59:17 +08:00
deleteReqHeader () {
2017-07-19 15:12:10 +08:00
let newSeqGroup = []
let seqGroup = this.props.seqGroup
2017-07-24 10:59:17 +08:00
let id = this.props.value.id
2017-07-24 19:43:13 +08:00
2017-07-19 15:12:10 +08:00
seqGroup.map(value => {
2017-07-24 10:59:17 +08:00
if (+id !== value.id) {
2017-07-19 15:12:10 +08:00
newSeqGroup.push(value)
}
})
this.props.deleteReqHeader(newSeqGroup)
}
2017-07-18 12:53:53 +08:00
render () {
2017-07-24 10:59:17 +08:00
const propsValue = this.props.value
2017-07-18 12:53:53 +08:00
const Option = Select.Option
2017-07-26 21:10:23 +08:00
const value = propsValue.value || ''
2017-07-26 12:31:42 +08:00
const name = propsValue.name || ''
const headers = ["Accept","Accept-Charset","Accept-Encoding","Accept-Language","Accept-Datetime","Authorization","Cache-Control","Connection","Cookie","Content-Disposition","Content-Length","Content-MD5","Content-Type","Date","Expect","From","Host","If-Match","If-Modified-Since","If-None-Match","If-Range","If-Unmodified-Since","Max-Forwards","Origin","Pragma","Proxy-Authorization","Range","Referer","TE","User-Agent","Upgrade","Via","Warning","X-Requested-With","DNT","X-Forwarded-For","X-Forwarded-Host","X-Forwarded-Proto","Front-End-Https","X-Http-Method-Override","X-ATT-DeviceId","X-Wap-Profile","Proxy-Connection","X-UIDH","X-Csrf-Token"]
const headersOptions = headers.map( (item, index) => {
return <Option key={index} value={item}>{item}</Option>
} )
2017-07-18 12:53:53 +08:00
return (
<li>
2017-07-27 20:06:49 +08:00
<em className="title">头部标签 : </em>
2017-07-28 13:50:58 +08:00
<Select value={name} style={{ width: 180 }} onChange={this.handleChange} size="large">
2017-07-26 12:31:42 +08:00
<Option value="">选择请求头</Option>
{headersOptions}
2017-07-18 12:53:53 +08:00
</Select>
2017-07-27 20:06:49 +08:00
<em className="title">头部内容 : </em>
2017-07-26 21:10:23 +08:00
<Input value={value} placeholder="Basic usage" className="req-content" size="large" onInput={this.handleBlur} />
2017-07-24 19:43:13 +08:00
<Icon className="dynamic-delete-button" type="minus-circle-o" onClick={this.deleteReqHeader} />
2017-07-18 12:53:53 +08:00
</li>
)
}
}
export default ReqList