UserManagement.jsx 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. import React, { Fragment } from 'react';
  2. import PropTypes from 'prop-types';
  3. import { withTranslation } from 'react-i18next';
  4. import PaginationWrapper from '../PaginationWrapper';
  5. import { createSubscribedElement } from '../UnstatedUtils';
  6. import { toastError } from '../../util/apiNotification';
  7. import AppContainer from '../../services/AppContainer';
  8. import AdminUsersContainer from '../../services/AdminUsersContainer';
  9. import PasswordResetModal from './Users/PasswordResetModal';
  10. import InviteUserControl from './Users/InviteUserControl';
  11. import UserTable from './Users/UserTable';
  12. class UserManagement extends React.Component {
  13. constructor(props) {
  14. super();
  15. this.state = {
  16. isNotifyCommentShow: false,
  17. };
  18. this.handlePage = this.handlePage.bind(this);
  19. this.handleChangeSearchText = this.handleChangeSearchText.bind(this);
  20. }
  21. componentWillMount() {
  22. this.handlePage(1);
  23. }
  24. async handlePage(selectedPage) {
  25. try {
  26. await this.props.adminUsersContainer.retrieveUsersByPagingNum(selectedPage);
  27. }
  28. catch (err) {
  29. toastError(err);
  30. }
  31. }
  32. /**
  33. * For checking same check box twice
  34. * @param {string} statusType
  35. */
  36. async handleClick(statusType) {
  37. const { adminUsersContainer } = this.props;
  38. if (!this.validateToggleStatus(statusType)) {
  39. return this.setState({ isNotifyCommentShow: true });
  40. }
  41. if (this.state.isNotifyCommentShow) {
  42. await this.setState({ isNotifyCommentShow: false });
  43. }
  44. adminUsersContainer.handleClick(statusType);
  45. }
  46. /**
  47. * Workaround user status check box
  48. * @param {string} statusType
  49. */
  50. validateToggleStatus(statusType) {
  51. if (this.props.adminUsersContainer.isSelected(statusType)) {
  52. return this.props.adminUsersContainer.state.selectedStatusList.size > 1;
  53. }
  54. return true;
  55. }
  56. /**
  57. * Reset button
  58. */
  59. resetButtonClickHandler() {
  60. const { adminUsersContainer } = this.props;
  61. try {
  62. adminUsersContainer.resetAllChanges();
  63. this.searchUserElement.value = '';
  64. this.state.isNotifyCommentShow = false;
  65. }
  66. catch (err) {
  67. toastError(err);
  68. }
  69. }
  70. /**
  71. * Workaround increamental search
  72. * @param {string} event
  73. */
  74. handleChangeSearchText(event) {
  75. this.props.adminUsersContainer.handleChangeSearchText(event.target.value);
  76. }
  77. render() {
  78. const { t, adminUsersContainer } = this.props;
  79. const pager = (
  80. <div className="pull-right my-3">
  81. <PaginationWrapper
  82. activePage={adminUsersContainer.state.activePage}
  83. changePage={this.handlePage}
  84. totalItemsCount={adminUsersContainer.state.totalUsers}
  85. pagingLimit={adminUsersContainer.state.pagingLimit}
  86. />
  87. </div>
  88. );
  89. const clearButton = (
  90. adminUsersContainer.state.searchText.length > 0
  91. ? (
  92. <i
  93. className="icon-close search-clear"
  94. onClick={() => {
  95. adminUsersContainer.clearSearchText();
  96. this.searchUserElement.value = '';
  97. }}
  98. />
  99. )
  100. : ''
  101. );
  102. return (
  103. <Fragment>
  104. {adminUsersContainer.state.userForPasswordResetModal != null
  105. && (
  106. <PasswordResetModal
  107. isOpen={adminUsersContainer.state.isPasswordResetModalShown}
  108. onClose={adminUsersContainer.hidePasswordResetModal}
  109. userForPasswordResetModal={adminUsersContainer.state.userForPasswordResetModal}
  110. />
  111. )}
  112. <p>
  113. <InviteUserControl />
  114. <a className="btn btn-outline-secondary ml-2" href="/admin/users/external-accounts" role="button">
  115. <i className="icon-user-follow" aria-hidden="true"></i>
  116. {t('admin:user_management.external_account')}
  117. </a>
  118. </p>
  119. <h2>{t('User_Management')}</h2>
  120. <div className="border-top border-bottom">
  121. <div className="d-flex justify-content-start align-items-center my-2">
  122. <div>
  123. <i className="icon-magnifier mr-1"></i>
  124. <span className="search-typeahead">
  125. <input
  126. type="text"
  127. ref={(searchUserElement) => { this.searchUserElement = searchUserElement }}
  128. onChange={this.handleChangeSearchText}
  129. />
  130. { clearButton }
  131. </span>
  132. </div>
  133. <div className="mx-5 form-inline">
  134. <div className="custom-control custom-checkbox custom-checkbox-primary mr-2">
  135. <input
  136. className="custom-control-input"
  137. type="checkbox"
  138. id="c1"
  139. checked={adminUsersContainer.isSelected('all')}
  140. onClick={() => { this.handleClick('all') }}
  141. />
  142. <label className="custom-control-label" htmlFor="c1">
  143. <span className="badge badge-pill badge-primary d-inline-block vt mt-1">All</span>
  144. </label>
  145. </div>
  146. <div className="custom-control custom-checkbox custom-checkbox-info mr-2">
  147. <input
  148. className="custom-control-input"
  149. type="checkbox"
  150. id="c2"
  151. checked={adminUsersContainer.isSelected('registered')}
  152. onClick={() => { this.handleClick('registered') }}
  153. />
  154. <label className="custom-control-label" htmlFor="c2">
  155. <span className="badge badge-pill badge-info d-inline-block vt mt-1">Approval Pending</span>
  156. </label>
  157. </div>
  158. <div className="custom-control custom-checkbox custom-checkbox-success mr-2">
  159. <input
  160. className="custom-control-input"
  161. type="checkbox"
  162. id="c3"
  163. checked={adminUsersContainer.isSelected('active')}
  164. onClick={() => { this.handleClick('active') }}
  165. />
  166. <label className="custom-control-label" htmlFor="c3">
  167. <span className="badge badge-pill badge-success d-inline-block vt mt-1">Active</span>
  168. </label>
  169. </div>
  170. <div className="custom-control custom-checkbox custom-checkbox-warning mr-2">
  171. <input
  172. className="custom-control-input"
  173. type="checkbox"
  174. id="c4"
  175. checked={adminUsersContainer.isSelected('suspended')}
  176. onClick={() => { this.handleClick('suspended') }}
  177. />
  178. <label className="custom-control-label" htmlFor="c4">
  179. <span className="badge badge-pill badge-warning d-inline-block vt mt-1">Suspended</span>
  180. </label>
  181. </div>
  182. <div className="custom-control custom-checkbox custom-checkbox-info">
  183. <input
  184. className="custom-control-input"
  185. type="checkbox"
  186. id="c5"
  187. checked={adminUsersContainer.isSelected('invited')}
  188. onClick={() => { this.handleClick('invited') }}
  189. />
  190. <label className="custom-control-label" htmlFor="c5">
  191. <span className="badge badge-pill badge-info d-inline-block vt mt-1">Invited</span>
  192. </label>
  193. </div>
  194. </div>
  195. <div>
  196. <button
  197. type="button"
  198. className="btn btn-outline-secondary btn-sm"
  199. onClick={() => { this.resetButtonClickHandler() }}
  200. >
  201. <span
  202. className="icon-refresh mr-1"
  203. >
  204. </span>
  205. Reset
  206. </button>
  207. </div>
  208. <div className="ml-4">
  209. {this.state.isNotifyCommentShow && <span className="text-warning">{t('admin:user_management.click_twice_same_checkbox')}</span>}
  210. </div>
  211. </div>
  212. </div>
  213. {pager}
  214. <UserTable />
  215. {pager}
  216. </Fragment>
  217. );
  218. }
  219. }
  220. UserManagement.propTypes = {
  221. t: PropTypes.func.isRequired, // i18next
  222. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  223. adminUsersContainer: PropTypes.instanceOf(AdminUsersContainer).isRequired,
  224. };
  225. const UserManagementWrapper = (props) => {
  226. return createSubscribedElement(UserManagement, props, [AppContainer, AdminUsersContainer]);
  227. };
  228. export default withTranslation()(UserManagementWrapper);