yapi/client/containers/Interface/InterfaceMode/InterfaceMode.js

168 lines
3.7 KiB
JavaScript
Raw Normal View History

2017-07-13 20:09:04 +08:00
import React, { Component } from 'react'
2017-07-28 15:12:55 +08:00
import { Modal, Button, AutoComplete } from 'antd'
2017-07-13 20:09:04 +08:00
import PropTypes from 'prop-types'
2017-07-27 16:48:30 +08:00
import axios from 'axios'
import { autobind } from 'core-decorators'
2017-07-13 20:09:04 +08:00
import ModeTag from './ModeTags'
class InterfaceMode extends Component {
static propTypes = {
modalVisible: PropTypes.bool,
2017-07-27 16:48:30 +08:00
closeProjectMember: PropTypes.func,
2017-07-28 15:12:55 +08:00
memberList: PropTypes.array,
dataSource: PropTypes.array,
inputValue: PropTypes.string
2017-07-13 20:09:04 +08:00
}
constructor(props) {
super(props)
2017-07-27 16:48:30 +08:00
this.state = {
memberList: [],
userName: ''
}
2017-07-13 20:09:04 +08:00
}
componentDidMount () {
2017-07-27 16:48:30 +08:00
this.getMemberList()
}
2017-07-13 20:09:04 +08:00
2017-07-27 16:48:30 +08:00
@autobind
getMemberList () {
const params = {
id: this.getInterfaceId()
}
axios.get('/api/project/get_member_list', { params })
2017-07-27 16:48:30 +08:00
.then(data => {
this.setState({
'memberList': data.data.data
})
})
.catch(err => {
console.log(err)
})
}
2017-07-28 15:12:55 +08:00
@autobind
onSelect (userName) {
console.log(userName)
this.setState({
userName
})
}
2017-07-27 16:48:30 +08:00
@autobind
closeMember (id) {
const params = {
member_uid: id,
id: this.getInterfaceId()
}
2017-07-28 17:30:56 +08:00
axios.post('/api/project/del_member', params)
2017-07-27 16:48:30 +08:00
.then(() => {
this.getMemberList()
})
.catch(err => {
console.log(err)
})
}
@autobind
addNewUser () {
const { userName } = this.state
const params = { q: userName}
axios.get('/api/user/search', { params })
2017-07-27 16:48:30 +08:00
.then(data => {
const member_uid = data.data.data[0].uid
const params = {id: this.getInterfaceId(), member_uid}
axios.post('/api/project/add_member', params)
2017-07-27 16:48:30 +08:00
.then( () => {
this.getMemberList()
})
.catch (err => {
console.log(err)
})
})
.catch (err => {
console.log(err)
})
}
@autobind
2017-07-28 15:12:55 +08:00
handleSearch (value) {
2017-07-27 16:48:30 +08:00
this.setState({
2017-07-28 15:12:55 +08:00
userName: value
2017-07-27 16:48:30 +08:00
})
2017-07-28 15:12:55 +08:00
const params = { q: value}
axios.get('/api/user/search', { params })
2017-07-28 15:12:55 +08:00
.then(data => {
const userList = []
data = data.data.data
if (data) {
data.forEach( v => userList.push(v.username) )
this.setState({
dataSource: userList
})
2017-07-28 17:30:56 +08:00
}
2017-07-28 15:12:55 +08:00
})
.catch (err => {
console.log(err)
})
2017-07-13 20:09:04 +08:00
}
handleOk (closeProjectMember) {
closeProjectMember()
}
handleCancel (closeProjectMember) {
closeProjectMember()
}
2017-07-27 16:48:30 +08:00
getInterfaceId () {
2017-07-28 17:30:56 +08:00
const reg = /project\/(\d+)/g
2017-07-27 16:48:30 +08:00
const url = location.href
url.match(reg)
return RegExp.$1
}
2017-07-13 20:09:04 +08:00
render() {
const { modalVisible, closeProjectMember } = this.props
const handleOk = this.handleOk.bind(this, closeProjectMember)
const handleCancel = this.handleCancel.bind(this, closeProjectMember)
2017-07-28 15:12:55 +08:00
const { memberList, dataSource } = this.state
2017-07-13 20:09:04 +08:00
return (
<Modal
title="项目成员管理"
onOk={ handleOk }
visible={ modalVisible }
onCancel={ handleCancel }
className="interface-mode-box"
>
<div className="add-user">
2017-07-28 15:12:55 +08:00
<AutoComplete
dataSource={dataSource}
style={{ width: 350 }}
onSelect={this.onSelect}
onSearch={this.handleSearch}
placeholder="input here"
/>
2017-07-28 17:30:56 +08:00
<Button onClick={this.addNewUser}> </Button>
2017-07-13 20:09:04 +08:00
</div>
2017-07-27 16:48:30 +08:00
<article className="users">
<p>项目成员</p>
<div className="tags">
{
memberList.map((value, key) => {
return <ModeTag key={key} value={value} closeMember={this.closeMember} getMemberList={this.getMemberList} />
})
}
</div>
</article>
2017-07-13 20:09:04 +08:00
</Modal>
)
}
}
export default InterfaceMode