UserGroupUserTable.jsx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import React, { Fragment } from 'react';
  2. import PropTypes from 'prop-types';
  3. import { withTranslation } from 'react-i18next';
  4. import dateFnsFormat from 'date-fns/format';
  5. import UserGroupUserModal from './UserGroupUserModal';
  6. import UserPicture from '../../User/UserPicture';
  7. import { createSubscribedElement } from '../../UnstatedUtils';
  8. import AppContainer from '../../../services/AppContainer';
  9. import { toastSuccess, toastError } from '../../../util/apiNotification';
  10. class UserGroupUserTable extends React.Component {
  11. constructor(props) {
  12. super(props);
  13. this.state = {
  14. userGroupRelations: props.userGroupRelations,
  15. notRelatedUsers: props.notRelatedUsers,
  16. isUserGroupUserModalOpen: false,
  17. };
  18. this.xss = window.xss;
  19. this.removeUser = this.removeUser.bind(this);
  20. this.openUserGroupUserModal = this.openUserGroupUserModal.bind(this);
  21. this.closeUserGroupUserModal = this.closeUserGroupUserModal.bind(this);
  22. this.addUser = this.addUser.bind(this);
  23. }
  24. async removeUser(username) {
  25. try {
  26. const res = await this.props.appContainer.apiv3.delete(`/user-groups/${this.props.userGroup._id}/users/${username}`);
  27. this.setState((prevState) => {
  28. return {
  29. userGroupRelations: prevState.userGroupRelations.filter((u) => { return u._id !== res.data.userGroupRelation._id }),
  30. notRelatedUsers: [...prevState.notRelatedUsers, res.data.user],
  31. };
  32. });
  33. toastSuccess(`Removed "${username}" from "${this.xss.process(this.props.userGroup.name)}"`);
  34. }
  35. catch (err) {
  36. toastError(new Error(`Unable to remove "${this.xss.process(username)}" from "${this.xss.process(this.props.userGroup.name)}"`));
  37. }
  38. }
  39. openUserGroupUserModal() {
  40. this.setState({ isUserGroupUserModalOpen: true });
  41. }
  42. closeUserGroupUserModal() {
  43. this.setState({ isUserGroupUserModalOpen: false });
  44. }
  45. addUser(user, userGroup, userGroupRelation) {
  46. this.setState((prevState) => {
  47. return {
  48. userGroupRelations: [...prevState.userGroupRelations, userGroupRelation],
  49. notRelatedUsers: prevState.notRelatedUsers.filter((u) => { return u._id !== user._id }),
  50. };
  51. });
  52. }
  53. render() {
  54. const { t } = this.props;
  55. return (
  56. <Fragment>
  57. <legend className="m-t-20">{ t('User List') }</legend>
  58. <table className="table table-bordered table-user-list">
  59. <thead>
  60. <tr>
  61. <th width="100px">#</th>
  62. <th>
  63. { t('User') }
  64. </th>
  65. <th>{ t('Name') }</th>
  66. <th width="100px">{ t('Created') }</th>
  67. <th width="160px">{ t('Last Login')}</th>
  68. <th width="70px"></th>
  69. </tr>
  70. </thead>
  71. <tbody>
  72. {this.state.userGroupRelations.map((sRelation) => {
  73. const { relatedUser } = sRelation;
  74. return (
  75. <tr key={sRelation._id}>
  76. <td>
  77. <UserPicture user={relatedUser} className="picture img-circle" />
  78. </td>
  79. <td>
  80. <strong>{relatedUser.username}</strong>
  81. </td>
  82. <td>{relatedUser.name}</td>
  83. <td>{relatedUser.createdAt ? dateFnsFormat(new Date(relatedUser.createdAt), 'yyyy-MM-dd') : ''}</td>
  84. <td>{relatedUser.lastLoginAt ? dateFnsFormat(new Date(relatedUser.lastLoginAt), 'yyyy-MM-dd HH:mm:ss') : ''}</td>
  85. <td>
  86. <div className="btn-group admin-user-menu">
  87. <button type="button" className="btn btn-default btn-sm dropdown-toggle" data-toggle="dropdown">
  88. <i className="icon-settings"></i> <span className="caret"></span>
  89. </button>
  90. <ul className="dropdown-menu" role="menu">
  91. <li>
  92. <a onClick={() => { return this.removeUser(relatedUser.username) }}>
  93. <i className="icon-fw icon-user-unfollow"></i> { t('user_group_management.remove_from_group')}
  94. </a>
  95. </li>
  96. </ul>
  97. </div>
  98. </td>
  99. </tr>
  100. );
  101. })}
  102. <tr>
  103. <td></td>
  104. <td className="text-center">
  105. <button className="btn btn-default" type="button" onClick={this.openUserGroupUserModal}>
  106. <i className="ti-plus"></i>
  107. </button>
  108. </td>
  109. <td></td>
  110. <td></td>
  111. <td></td>
  112. <td></td>
  113. </tr>
  114. </tbody>
  115. </table>
  116. <UserGroupUserModal
  117. show={this.state.isUserGroupUserModalOpen}
  118. onClose={this.closeUserGroupUserModal}
  119. onAdd={this.addUser}
  120. notRelatedUsers={this.state.notRelatedUsers}
  121. userGroup={this.props.userGroup}
  122. />
  123. </Fragment>
  124. );
  125. }
  126. }
  127. UserGroupUserTable.propTypes = {
  128. t: PropTypes.func.isRequired, // i18next
  129. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  130. userGroupRelations: PropTypes.arrayOf(PropTypes.object).isRequired,
  131. notRelatedUsers: PropTypes.arrayOf(PropTypes.object).isRequired,
  132. userGroup: PropTypes.object.isRequired,
  133. };
  134. /**
  135. * Wrapper component for using unstated
  136. */
  137. const UserGroupUserTableWrapper = (props) => {
  138. return createSubscribedElement(UserGroupUserTable, props, [AppContainer]);
  139. };
  140. export default withTranslation()(UserGroupUserTableWrapper);