yapi/client/containers/Group/MemberList/MemberList.js

240 lines
6.7 KiB
JavaScript
Raw Normal View History

2017-08-11 17:47:25 +08:00
import React, { Component } from 'react';
2017-08-16 17:13:14 +08:00
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
2017-08-18 17:08:37 +08:00
import { Table, Select, Button, Modal, Row, Col, message, Popconfirm } from 'antd';
2017-08-16 17:13:14 +08:00
import './MemberList.scss';
import { autobind } from 'core-decorators';
2017-08-16 21:29:01 +08:00
import { fetchGroupMemberList, fetchGroupMsg, addMember, delMember, changeMemberRole } from '../../../reducer/modules/group.js'
2017-08-16 17:13:14 +08:00
import UsernameAutoComplete from '../../../components/UsernameAutoComplete/UsernameAutoComplete.js';
const Option = Select.Option;
2017-08-11 17:47:25 +08:00
2017-08-16 17:13:14 +08:00
const arrayAddKey = (arr) => {
return arr.map((item, index) => {
return {
...item,
key: index
}
});
}
2017-08-11 17:47:25 +08:00
2017-08-16 17:13:14 +08:00
@connect(
state => {
return {
currGroup: state.group.currGroup,
uid: state.user.uid,
role: state.group.role
}
},
{
fetchGroupMemberList,
fetchGroupMsg,
2017-08-16 20:31:59 +08:00
addMember,
2017-08-16 21:29:01 +08:00
delMember,
changeMemberRole
2017-08-16 17:13:14 +08:00
}
)
2017-08-11 17:47:25 +08:00
class MemberList extends Component {
2017-08-16 17:13:14 +08:00
constructor(props) {
super(props);
this.state = {
userInfo: [],
role: '',
visible: false,
dataSource: [],
inputUid: 0,
inputRole: 'dev'
}
}
static propTypes = {
currGroup: PropTypes.object,
uid: PropTypes.number,
fetchGroupMemberList: PropTypes.func,
fetchGroupMsg: PropTypes.func,
addMember: PropTypes.func,
2017-08-16 20:31:59 +08:00
delMember: PropTypes.func,
2017-08-16 21:29:01 +08:00
changeMemberRole: PropTypes.func,
2017-08-16 17:13:14 +08:00
role: PropTypes.string
}
2017-08-16 20:31:59 +08:00
@autobind
showAddMemberModal() {
2017-08-16 17:13:14 +08:00
this.setState({
visible: true
});
}
2017-08-16 21:29:01 +08:00
// 重新获取列表
2017-08-16 20:31:59 +08:00
@autobind
reFetchList() {
this.props.fetchGroupMemberList(this.props.currGroup._id).then((res) => {
this.setState({
userInfo: arrayAddKey(res.payload.data.data),
visible: false
});
});
}
2017-08-16 21:29:01 +08:00
// 增 - 添加成员
@autobind
handleOk() {
console.log(this.props.currGroup._id, this.state.inputUid);
this.props.addMember({
id: this.props.currGroup._id,
member_uid: this.state.inputUid
}).then((res) => {
console.log(res);
if (!res.payload.data.errcode) {
message.success('添加成功!');
this.reFetchList(); // 添加成功后重新获取分组成员列表
}
});
}
// 添加成员时 选择新增成员权限
@autobind
changeNewMemberRole(value) {
return () => {
console.log(this.props.currGroup._id, value);
}
}
// 删 - 删除分组成员
2017-08-16 20:31:59 +08:00
@autobind
deleteConfirm(member_uid) {
return () => {
const id = this.props.currGroup._id;
this.props.delMember({ id, member_uid }).then((res) => {
if (!res.payload.data.errcode) {
message.success(res.payload.data.errmsg);
this.reFetchList(); // 添加成功后重新获取分组成员列表
}
});
}
}
2017-08-16 21:29:01 +08:00
// 改 - 修改成员权限
2017-08-16 17:13:14 +08:00
@autobind
2017-08-16 21:29:01 +08:00
changeUserRole(e) {
console.log(e);
const id = this.props.currGroup._id;
const role = e.split('-')[0];
const member_uid = e.split('-')[1];
this.props.changeMemberRole({ id, member_uid, role }).then((res) => {
2017-08-16 17:13:14 +08:00
if (!res.payload.data.errcode) {
2017-08-16 21:29:01 +08:00
message.success(res.payload.data.errmsg);
2017-08-16 20:31:59 +08:00
this.reFetchList(); // 添加成功后重新获取分组成员列表
2017-08-16 17:13:14 +08:00
}
});
}
2017-08-16 21:29:01 +08:00
// 关闭模态框
2017-08-16 17:13:14 +08:00
@autobind
handleCancel() {
this.setState({
visible: false
});
}
componentWillReceiveProps(nextProps) {
if (this.props.currGroup !== nextProps.currGroup) {
this.props.fetchGroupMemberList(nextProps.currGroup._id).then((res) => {
this.setState({
userInfo: arrayAddKey(res.payload.data.data)
});
});
this.props.fetchGroupMsg(nextProps.currGroup._id).then((res) => {
this.setState({
role: res.payload.data.data.role
});
})
}
}
componentDidMount() {
const currGroupId = this.props.currGroup._id;
this.props.fetchGroupMsg(currGroupId).then((res) => {
this.setState({
role: res.payload.data.data.role
});
})
this.props.fetchGroupMemberList(currGroupId).then((res) => {
this.setState({
userInfo: arrayAddKey(res.payload.data.data)
});
});
}
@autobind
onUserSelect(childState) {
console.log(childState);
this.setState({
inputUid: childState.uid
})
}
2017-08-11 17:47:25 +08:00
render() {
2017-08-16 17:13:14 +08:00
const columns = [{
title: this.props.currGroup.group_name + ' 分组成员 ('+this.state.userInfo.length + ') 人',
dataIndex: 'username',
key: 'username',
render: (text, record) => {
return (<div className="m-user">
<img src={location.protocol + '//' + location.host + '/api/user/avatar?uid=' + record.uid} className="m-user-img" />
<p className="m-user-name">{text}</p>
</div>);
}
}, {
2017-08-16 20:31:59 +08:00
title: (this.state.role === 'owner' || this.state.role === 'admin') ? <div className="btn-container"><Button className="btn" type="primary" icon="plus" onClick={this.showAddMemberModal}>添加成员</Button></div> : '',
2017-08-16 17:13:14 +08:00
key: 'action',
className: 'member-opration',
render: (text, record) => {
if (this.state.role === 'owner' || this.state.role === 'admin') {
return (
<div>
2017-08-16 21:29:01 +08:00
<Select defaultValue={record.role+'-'+record.uid} className="select" onChange={this.changeUserRole}>
<Option value={'owner-'+record.uid}>组长</Option>
<Option value={'dev-'+record.uid}>开发者</Option>
2017-08-16 17:13:14 +08:00
</Select>
2017-08-16 20:31:59 +08:00
<Popconfirm placement="topRight" title="你确定要删除吗? " onConfirm={this.deleteConfirm(record.uid)} okText="确定" cancelText="">
<Button type="danger" icon="minus" className="btn-danger" />
</Popconfirm>
2017-08-16 17:13:14 +08:00
</div>
)
} else {
return '';
}
}
}];
2017-08-11 17:47:25 +08:00
return (
<div className="m-panel">
2017-08-16 17:13:14 +08:00
<Modal
title="添加成员"
visible={this.state.visible}
onOk={this.handleOk}
onCancel={this.handleCancel}
>
<Row gutter={6} className="modal-input">
<Col span="5"><div className="label">用户名: </div></Col>
<Col span="15">
<UsernameAutoComplete callbackState={this.onUserSelect} />
</Col>
</Row>
<Row gutter={6} className="modal-input">
<Col span="5"><div className="label">权限: </div></Col>
<Col span="15">
2017-08-16 21:29:01 +08:00
<Select size="large" defaultValue="dev" className="select" onChange={this.changeNewMemberRole}>
2017-08-16 17:13:14 +08:00
<Option value="owner">组长</Option>
<Option value="dev">开发者</Option>
</Select>
</Col>
</Row>
</Modal>
<Table columns={columns} dataSource={this.state.userInfo} pagination={false} />
2017-08-11 17:47:25 +08:00
</div>
);
}
}
export default MemberList;