UserGroupUserTable.jsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { withTranslation } from 'react-i18next';
  4. import dateFnsFormat from 'date-fns/format';
  5. import { UserPicture } from '@growi/ui';
  6. import { withUnstatedContainers } from '../../UnstatedUtils';
  7. import AppContainer from '~/client/services/AppContainer';
  8. import AdminUserGroupDetailContainer from '~/client/services/AdminUserGroupDetailContainer';
  9. import { toastSuccess, toastError } from '~/client/util/apiNotification';
  10. class UserGroupUserTable extends React.Component {
  11. constructor(props) {
  12. super(props);
  13. this.xss = window.xss;
  14. this.removeUser = this.removeUser.bind(this);
  15. }
  16. async removeUser(username) {
  17. try {
  18. await this.props.adminUserGroupDetailContainer.removeUserByUsername(username);
  19. toastSuccess(`Removed "${this.xss.process(username)}" from "${this.xss.process(this.props.adminUserGroupDetailContainer.state.userGroup.name)}"`);
  20. }
  21. catch (err) {
  22. // eslint-disable-next-line max-len
  23. toastError(new Error(`Unable to remove "${this.xss.process(username)}" from "${this.xss.process(this.props.adminUserGroupDetailContainer.state.userGroup.name)}"`));
  24. }
  25. }
  26. render() {
  27. const { t, adminUserGroupDetailContainer } = this.props;
  28. return (
  29. <table className="table table-bordered table-user-list">
  30. <thead>
  31. <tr>
  32. <th width="100px">#</th>
  33. <th>
  34. {t('username')}
  35. </th>
  36. <th>{t('Name')}</th>
  37. <th width="100px">{t('Created')}</th>
  38. <th width="160px">{t('Last_Login')}</th>
  39. <th width="70px"></th>
  40. </tr>
  41. </thead>
  42. <tbody>
  43. {adminUserGroupDetailContainer.state.userGroupRelations.map((sRelation) => {
  44. const { relatedUser } = sRelation;
  45. return (
  46. <tr key={sRelation._id}>
  47. <td>
  48. <UserPicture user={relatedUser} className="picture rounded-circle" />
  49. </td>
  50. <td>
  51. <strong>{relatedUser.username}</strong>
  52. </td>
  53. <td>{relatedUser.name}</td>
  54. <td>{relatedUser.createdAt ? dateFnsFormat(new Date(relatedUser.createdAt), 'yyyy-MM-dd') : ''}</td>
  55. <td>{relatedUser.lastLoginAt ? dateFnsFormat(new Date(relatedUser.lastLoginAt), 'yyyy-MM-dd HH:mm:ss') : ''}</td>
  56. <td>
  57. <div className="btn-group admin-user-menu">
  58. <button
  59. type="button"
  60. id={`admin-group-menu-button-${relatedUser._id}`}
  61. className="btn btn-outline-secondary btn-sm dropdown-toggle"
  62. data-toggle="dropdown"
  63. >
  64. <i className="icon-settings"></i>
  65. </button>
  66. <div className="dropdown-menu" role="menu" aria-labelledby={`admin-group-menu-button-${relatedUser._id}`}>
  67. <button
  68. className="dropdown-item"
  69. type="button"
  70. onClick={() => {
  71. return this.removeUser(relatedUser.username);
  72. }}
  73. >
  74. <i className="icon-fw icon-user-unfollow"></i> {t('admin:user_group_management.remove_from_group')}
  75. </button>
  76. </div>
  77. </div>
  78. </td>
  79. </tr>
  80. );
  81. })}
  82. <tr>
  83. <td></td>
  84. <td className="text-center">
  85. <button className="btn btn-outline-secondary" type="button" onClick={adminUserGroupDetailContainer.openUserGroupUserModal}>
  86. <i className="ti-plus"></i>
  87. </button>
  88. </td>
  89. <td></td>
  90. <td></td>
  91. <td></td>
  92. <td></td>
  93. </tr>
  94. </tbody>
  95. </table>
  96. );
  97. }
  98. }
  99. UserGroupUserTable.propTypes = {
  100. t: PropTypes.func.isRequired, // i18next
  101. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  102. adminUserGroupDetailContainer: PropTypes.instanceOf(AdminUserGroupDetailContainer).isRequired,
  103. };
  104. /**
  105. * Wrapper component for using unstated
  106. */
  107. const UserGroupUserTableWrapper = withUnstatedContainers(UserGroupUserTable, [AppContainer, AdminUserGroupDetailContainer]);
  108. export default withTranslation()(UserGroupUserTableWrapper);