UserTable.jsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 UserPicture from '../../User/UserPicture';
  6. import UserMenu from './UserMenu';
  7. import { createSubscribedElement } from '../../UnstatedUtils';
  8. import AppContainer from '../../../services/AppContainer';
  9. import AdminUsersContainer from '../../../services/AdminUsersContainer';
  10. class UserTable extends React.Component {
  11. constructor(props) {
  12. super(props);
  13. this.state = {
  14. };
  15. this.getUserStatusLabel = this.getUserStatusLabel.bind(this);
  16. }
  17. /**
  18. * user.statusをみてステータスのラベルを返す
  19. * @param {string} userStatus
  20. * @return ステータスラベル
  21. */
  22. getUserStatusLabel(userStatus) {
  23. let additionalClassName;
  24. let text;
  25. switch (userStatus) {
  26. case 1:
  27. additionalClassName = 'label-info';
  28. text = 'Approval Pending';
  29. break;
  30. case 2:
  31. additionalClassName = 'label-success';
  32. text = 'Active';
  33. break;
  34. case 3:
  35. additionalClassName = 'label-warning';
  36. text = 'Suspended';
  37. break;
  38. case 4:
  39. additionalClassName = 'label-danger';
  40. text = 'Deleted';
  41. break;
  42. case 5:
  43. additionalClassName = 'label-info';
  44. text = 'Invited';
  45. break;
  46. }
  47. return (
  48. <span className={`label ${additionalClassName}`}>
  49. {text}
  50. </span>
  51. );
  52. }
  53. render() {
  54. const { t, adminUsersContainer } = this.props;
  55. return (
  56. <Fragment>
  57. <table className="table table-default table-bordered table-user-list">
  58. <thead>
  59. <tr>
  60. <th width="100px">#</th>
  61. <th>{ t('status') }</th>
  62. <th><code>username</code></th>
  63. <th>{ t('Name') }</th>
  64. <th>{ t('Email') }</th>
  65. <th width="100px">{ t('Created') }</th>
  66. <th width="150px">{ t('Last_Login') }</th>
  67. <th width="70px"></th>
  68. </tr>
  69. </thead>
  70. <tbody>
  71. {adminUsersContainer.state.users.map((user) => {
  72. return (
  73. <tr key={user._id}>
  74. <td>
  75. <UserPicture user={user} className="picture img-circle" />
  76. {user.admin && <span className="label label-inverse label-admin ml-2">{ t('administrator') }</span>}
  77. </td>
  78. <td>{this.getUserStatusLabel(user.status)}</td>
  79. <td>
  80. <strong>{user.username}</strong>
  81. </td>
  82. <td>{user.name}</td>
  83. <td>{user.email}</td>
  84. <td>{dateFnsFormat(new Date(user.createdAt), 'yyyy-MM-dd')}</td>
  85. <td>
  86. { user.lastLoginAt && <span>{dateFnsFormat(new Date(user.lastLoginAt), 'yyyy-MM-dd HH:mm')}</span> }
  87. </td>
  88. <td>
  89. <UserMenu user={user} />
  90. </td>
  91. </tr>
  92. );
  93. })}
  94. </tbody>
  95. </table>
  96. </Fragment>
  97. );
  98. }
  99. }
  100. const UserTableWrapper = (props) => {
  101. return createSubscribedElement(UserTable, props, [AppContainer, AdminUsersContainer]);
  102. };
  103. UserTable.propTypes = {
  104. t: PropTypes.func.isRequired, // i18next
  105. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  106. adminUsersContainer: PropTypes.instanceOf(AdminUsersContainer).isRequired,
  107. };
  108. export default withTranslation()(UserTableWrapper);