yapi/client/containers/AddInterface/AddInterface.js

217 lines
5.1 KiB
JavaScript
Raw Normal View History

2017-07-18 12:53:53 +08:00
import './AddInterface.scss'
import React, { Component } from 'react'
2017-07-20 14:48:29 +08:00
import PropTypes from 'prop-types'
import axios from 'axios'
import { connect } from 'react-redux'
import { autobind } from 'core-decorators'
2017-07-20 16:34:46 +08:00
import { Button, Tabs } from 'antd'
2017-07-18 12:53:53 +08:00
import ReqMethod from './ReqMethod/ReqMethod.js'
import ReqHeader from './ReqHeader/ReqHeader.js'
import ReqParams from './ReqParams/ReqParams.js'
import ResParams from './ResParams/ResParams.js'
import Result from './Result/Result.js'
2017-07-24 10:59:17 +08:00
import {
saveForms,
getResParams,
getReqParams,
addReqHeader,
pushInputValue,
pushInterfaceName
} from '../../actions/addInterface.js'
2017-07-18 12:53:53 +08:00
2017-07-20 14:48:29 +08:00
@connect(
state => {
return {
2017-07-20 16:57:21 +08:00
reqParams: state.addInterface.reqParams,
2017-07-20 19:26:04 +08:00
resParams: state.addInterface.resParams,
method: state.addInterface.method,
url: state.addInterface.url,
2017-07-24 10:59:17 +08:00
seqGroup: state.addInterface.seqGroup,
2017-07-20 19:26:04 +08:00
interfaceName: state.addInterface.interfaceName
2017-07-20 14:48:29 +08:00
}
},
{
2017-07-24 10:59:17 +08:00
saveForms,
getReqParams,
getResParams,
addReqHeader,
pushInputValue,
pushInterfaceName
2017-07-20 14:48:29 +08:00
}
)
2017-07-18 12:53:53 +08:00
2017-07-20 14:48:29 +08:00
class AddInterface extends Component {
static propTypes = {
2017-07-24 10:59:17 +08:00
url: PropTypes.string,
method: PropTypes.string,
2017-07-20 14:48:29 +08:00
reqParams: PropTypes.string,
2017-07-20 19:26:04 +08:00
resParams: PropTypes.string,
2017-07-24 10:59:17 +08:00
seqGroup: PropTypes.array,
2017-07-20 19:26:04 +08:00
interfaceName: PropTypes.string,
2017-07-24 10:59:17 +08:00
saveForms: PropTypes.func,
addReqHeader: PropTypes.func,
getReqParams: PropTypes.func,
getResParams: PropTypes.func,
pushInputValue: PropTypes.func,
pushInterfaceName: PropTypes.func
2017-07-20 14:48:29 +08:00
}
2017-07-18 12:53:53 +08:00
constructor (props) {
super(props)
2017-07-24 10:59:17 +08:00
this.state = {
isLoading: ''
}
}
componentDidMount () {
const ifTrue = this.verificationURL()
let interfaceId = undefined
if (ifTrue) {
interfaceId = this.getInterfaceId()
this.initInterfaceData(interfaceId)
} else {
const props = this.props
props.pushInputValue('')
props.pushInterfaceName('')
props.getReqParams('')
props.getResParams('')
props.addReqHeader([
{
id: 0,
name: '',
value: ''
}
])
}
}
getInterfaceId () {
const value = location.hash.match(/\d+/g)
return value ? value[0] : ''
}
verificationURL () {
const dir = 'AddInterface/edit'
const url = location.href
if (url.includes(dir)) {
return true
}
}
editState (data) {
const props = this.props
const { path, title, req_params_other, res_body, req_headers} = data
props.pushInputValue(path)
props.pushInterfaceName(title)
props.getReqParams(req_params_other)
props.getResParams(res_body)
props.addReqHeader(req_headers)
}
2017-07-24 19:43:13 +08:00
editState2 () {
const props = this.props
props.pushInputValue(props.url)
// props.pushInterfaceName(title)
// props.getReqParams(req_params_other)
props.getResParams(props.resParams)
// props.addReqHeader(req_headers)
}
2017-07-24 10:59:17 +08:00
initInterfaceData (interfaceId) {
const params = { id: interfaceId }
axios.get('/interface/get', {params: params})
.then(result => {
result = result.data.data
result.req_headers.map((value, key) => {
value.id = key
return value
})
this.editState(result)
})
.catch(e => {
console.log(e)
})
}
setLoading (boolean) {
this.setState({
isLoading: boolean ? 'is-loading' : ''
})
}
routerPage () {
const origin = location.origin
const pathname = location.pathname
location.href = `${origin}${pathname}#/interface`
2017-07-18 12:53:53 +08:00
}
2017-07-20 14:48:29 +08:00
@autobind
2017-07-18 12:53:53 +08:00
saveForms () {
2017-07-24 10:59:17 +08:00
let postURL = undefined
const { interfaceName, url, seqGroup, reqParams, resParams } = this.props
const ifTrue = this.verificationURL()
const interfaceId = this.getInterfaceId()
2017-07-20 14:48:29 +08:00
const params = {
2017-07-20 19:26:04 +08:00
title: interfaceName,
path: url,
2017-07-20 14:48:29 +08:00
method: 'POST',
2017-07-24 10:59:17 +08:00
req_headers: seqGroup,
2017-07-20 19:26:04 +08:00
project_id: 558,
req_params_type: 'json',
req_params_other: reqParams,
res_body_type: 'json',
res_body: resParams
2017-07-20 14:48:29 +08:00
}
2017-07-24 10:59:17 +08:00
if (ifTrue) {
params.id = interfaceId
postURL = '/interface/up'
} else {
postURL = '/interface/add'
}
this.setLoading(true)
axios.post(postURL, params)
2017-07-24 19:43:13 +08:00
.then(data => {
console.log(data)
2017-07-24 10:59:17 +08:00
this.setLoading()
2017-07-24 19:43:13 +08:00
this.editState2()
// this.routerPage()
2017-07-20 14:48:29 +08:00
})
.catch(e => {
console.log(e)
})
2017-07-18 12:53:53 +08:00
}
render () {
2017-07-20 16:34:46 +08:00
const TabPane = Tabs.TabPane
2017-07-24 10:59:17 +08:00
const isLoading = this.state.isLoading
2017-07-20 16:34:46 +08:00
2017-07-18 12:53:53 +08:00
return (
2017-07-20 16:38:50 +08:00
<section className="add-interface-box">
<div className="content">
<Tabs defaultActiveKey="1">
<TabPane tab="接口详情" key="1">
2017-07-20 16:34:46 +08:00
<Button type="primary" className="save" onClick={this.saveForms}>保存</Button>
<ReqMethod />
<ReqHeader />
2017-07-24 10:59:17 +08:00
<ReqParams data={this.props} />
2017-07-20 16:34:46 +08:00
<ResParams />
<Result />
2017-07-20 16:38:50 +08:00
</TabPane>
<TabPane tab="Mock" key="2">mock</TabPane>
<TabPane tab="测试" key="3">测试</TabPane>
</Tabs>
</div>
2017-07-24 10:59:17 +08:00
<div className={`loading ${isLoading}`}></div>
2017-07-20 16:38:50 +08:00
</section>
2017-07-18 12:53:53 +08:00
)
}
}
export default AddInterface