mock平台

MemberList.js 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. import React, { PureComponent as Component } from 'react';
  2. import PropTypes from 'prop-types';
  3. import { connect } from 'react-redux';
  4. import { Table, Select, Button, Modal, Row, Col, message, Popconfirm } from 'antd';
  5. import { Link } from 'react-router-dom';
  6. import './MemberList.scss';
  7. import { autobind } from 'core-decorators';
  8. import {
  9. fetchGroupMemberList,
  10. fetchGroupMsg,
  11. addMember,
  12. delMember,
  13. changeMemberRole
  14. } from '../../../reducer/modules/group.js';
  15. import ErrMsg from '../../../components/ErrMsg/ErrMsg.js';
  16. import UsernameAutoComplete from '../../../components/UsernameAutoComplete/UsernameAutoComplete.js';
  17. const Option = Select.Option;
  18. function arrayAddKey(arr) {
  19. return arr.map((item, index) => {
  20. return {
  21. ...item,
  22. key: index
  23. };
  24. });
  25. }
  26. @connect(
  27. state => {
  28. return {
  29. currGroup: state.group.currGroup,
  30. uid: state.user.uid,
  31. role: state.group.role
  32. };
  33. },
  34. {
  35. fetchGroupMemberList,
  36. fetchGroupMsg,
  37. addMember,
  38. delMember,
  39. changeMemberRole
  40. }
  41. )
  42. class MemberList extends Component {
  43. constructor(props) {
  44. super(props);
  45. this.state = {
  46. userInfo: [],
  47. role: '',
  48. visible: false,
  49. dataSource: [],
  50. inputUids: [],
  51. inputRole: 'dev'
  52. };
  53. }
  54. static propTypes = {
  55. currGroup: PropTypes.object,
  56. uid: PropTypes.number,
  57. fetchGroupMemberList: PropTypes.func,
  58. fetchGroupMsg: PropTypes.func,
  59. addMember: PropTypes.func,
  60. delMember: PropTypes.func,
  61. changeMemberRole: PropTypes.func,
  62. role: PropTypes.string
  63. };
  64. showAddMemberModal = () => {
  65. this.setState({
  66. visible: true
  67. });
  68. };
  69. // 重新获取列表
  70. reFetchList = () => {
  71. this.props.fetchGroupMemberList(this.props.currGroup._id).then(res => {
  72. this.setState({
  73. userInfo: arrayAddKey(res.payload.data.data),
  74. visible: false
  75. });
  76. });
  77. };
  78. // 增 - 添加成员
  79. handleOk = () => {
  80. this.props
  81. .addMember({
  82. id: this.props.currGroup._id,
  83. member_uids: this.state.inputUids,
  84. role: this.state.inputRole
  85. })
  86. .then(res => {
  87. if (!res.payload.data.errcode) {
  88. const { add_members, exist_members } = res.payload.data.data;
  89. const addLength = add_members.length;
  90. const existLength = exist_members.length;
  91. this.setState({
  92. inputRole: 'dev',
  93. inputUids: []
  94. });
  95. message.success(`添加成功! 已成功添加 ${addLength} 人,其中 ${existLength} 人已存在`);
  96. this.reFetchList(); // 添加成功后重新获取分组成员列表
  97. }
  98. });
  99. };
  100. // 添加成员时 选择新增成员权限
  101. changeNewMemberRole = value => {
  102. this.setState({
  103. inputRole: value
  104. });
  105. };
  106. // 删 - 删除分组成员
  107. deleteConfirm = member_uid => {
  108. return () => {
  109. const id = this.props.currGroup._id;
  110. this.props.delMember({ id, member_uid }).then(res => {
  111. if (!res.payload.data.errcode) {
  112. message.success(res.payload.data.errmsg);
  113. this.reFetchList(); // 添加成功后重新获取分组成员列表
  114. }
  115. });
  116. };
  117. };
  118. // 改 - 修改成员权限
  119. changeUserRole = e => {
  120. const id = this.props.currGroup._id;
  121. const role = e.split('-')[0];
  122. const member_uid = e.split('-')[1];
  123. this.props.changeMemberRole({ id, member_uid, role }).then(res => {
  124. if (!res.payload.data.errcode) {
  125. message.success(res.payload.data.errmsg);
  126. this.reFetchList(); // 添加成功后重新获取分组成员列表
  127. }
  128. });
  129. };
  130. // 关闭模态框
  131. handleCancel = () => {
  132. this.setState({
  133. visible: false
  134. });
  135. };
  136. componentWillReceiveProps(nextProps) {
  137. if (this._groupId !== this._groupId) {
  138. return null;
  139. }
  140. if (this.props.currGroup._id !== nextProps.currGroup._id) {
  141. this.props.fetchGroupMemberList(nextProps.currGroup._id).then(res => {
  142. this.setState({
  143. userInfo: arrayAddKey(res.payload.data.data)
  144. });
  145. });
  146. this.props.fetchGroupMsg(nextProps.currGroup._id).then(res => {
  147. this.setState({
  148. role: res.payload.data.data.role
  149. });
  150. });
  151. }
  152. }
  153. componentDidMount() {
  154. const currGroupId = (this._groupId = this.props.currGroup._id);
  155. this.props.fetchGroupMsg(currGroupId).then(res => {
  156. this.setState({
  157. role: res.payload.data.data.role
  158. });
  159. });
  160. this.props.fetchGroupMemberList(currGroupId).then(res => {
  161. this.setState({
  162. userInfo: arrayAddKey(res.payload.data.data)
  163. });
  164. });
  165. }
  166. @autobind
  167. onUserSelect(uids) {
  168. this.setState({
  169. inputUids: uids
  170. });
  171. }
  172. render() {
  173. const columns = [
  174. {
  175. title:
  176. this.props.currGroup.group_name + ' 分组成员 (' + this.state.userInfo.length + ') 人',
  177. dataIndex: 'username',
  178. key: 'username',
  179. render: (text, record) => {
  180. return (
  181. <div className="m-user">
  182. <Link to={`/user/profile/${record.uid}`}>
  183. <img
  184. src={
  185. location.protocol + '//' + location.host + '/api/user/avatar?uid=' + record.uid
  186. }
  187. className="m-user-img"
  188. />
  189. </Link>
  190. <Link to={`/user/profile/${record.uid}`}>
  191. <p className="m-user-name">{text}</p>
  192. </Link>
  193. </div>
  194. );
  195. }
  196. },
  197. {
  198. title:
  199. this.state.role === 'owner' || this.state.role === 'admin' ? (
  200. <div className="btn-container">
  201. <Button className="btn" type="primary" onClick={this.showAddMemberModal}>
  202. 添加成员
  203. </Button>
  204. </div>
  205. ) : (
  206. ''
  207. ),
  208. key: 'action',
  209. className: 'member-opration',
  210. render: (text, record) => {
  211. if (this.state.role === 'owner' || this.state.role === 'admin') {
  212. return (
  213. <div>
  214. <Select
  215. value={record.role + '-' + record.uid}
  216. className="select"
  217. onChange={this.changeUserRole}
  218. >
  219. <Option value={'owner-' + record.uid}>组长</Option>
  220. <Option value={'dev-' + record.uid}>开发者</Option>
  221. <Option value={'guest-' + record.uid}>访客</Option>
  222. </Select>
  223. <Popconfirm
  224. placement="topRight"
  225. title="你确定要删除吗? "
  226. onConfirm={this.deleteConfirm(record.uid)}
  227. okText="确定"
  228. cancelText=""
  229. >
  230. <Button type="danger" icon="delete" className="btn-danger" />
  231. {/* <Icon type="delete" className="btn-danger"/> */}
  232. </Popconfirm>
  233. </div>
  234. );
  235. } else {
  236. // 非管理员可以看到权限 但无法修改
  237. if (record.role === 'owner') {
  238. return '组长';
  239. } else if (record.role === 'dev') {
  240. return '开发者';
  241. } else if (record.role === 'guest') {
  242. return '访客';
  243. } else {
  244. return '';
  245. }
  246. }
  247. }
  248. }
  249. ];
  250. let userinfo = this.state.userInfo;
  251. let ownerinfo = [];
  252. let devinfo = [];
  253. let guestinfo = [];
  254. for (let i = 0; i < userinfo.length; i++) {
  255. if (userinfo[i].role === 'owner') {
  256. ownerinfo.push(userinfo[i]);
  257. }
  258. if (userinfo[i].role === 'dev') {
  259. devinfo.push(userinfo[i]);
  260. }
  261. if (userinfo[i].role === 'guest') {
  262. guestinfo.push(userinfo[i]);
  263. }
  264. }
  265. userinfo = [...ownerinfo, ...devinfo, ...guestinfo];
  266. return (
  267. <div className="m-panel">
  268. {this.state.visible ? (
  269. <Modal
  270. title="添加成员"
  271. visible={this.state.visible}
  272. onOk={this.handleOk}
  273. onCancel={this.handleCancel}
  274. >
  275. <Row gutter={6} className="modal-input">
  276. <Col span="5">
  277. <div className="label usernamelabel">用户名: </div>
  278. </Col>
  279. <Col span="15">
  280. <UsernameAutoComplete callbackState={this.onUserSelect} />
  281. </Col>
  282. </Row>
  283. <Row gutter={6} className="modal-input">
  284. <Col span="5">
  285. <div className="label usernameauth">权限: </div>
  286. </Col>
  287. <Col span="15">
  288. <Select defaultValue="dev" className="select" onChange={this.changeNewMemberRole}>
  289. <Option value="owner">组长</Option>
  290. <Option value="dev">开发者</Option>
  291. <Option value="guest">访客</Option>
  292. </Select>
  293. </Col>
  294. </Row>
  295. </Modal>
  296. ) : (
  297. ''
  298. )}
  299. <Table
  300. columns={columns}
  301. dataSource={userinfo}
  302. pagination={false}
  303. locale={{ emptyText: <ErrMsg type="noMemberInGroup" /> }}
  304. />
  305. </div>
  306. );
  307. }
  308. }
  309. export default MemberList;