UserGroupUserTable.jsx 4.5 KB

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