import React, { Fragment } from 'react'; import PropTypes from 'prop-types'; import { withTranslation } from 'react-i18next'; import PaginationWrapper from '../PaginationWrapper'; import { withUnstatedContainers } from '../UnstatedUtils'; import { toastError } from '~/client/util/apiNotification'; import AppContainer from '~/client/services/AppContainer'; import AdminUsersContainer from '~/client/services/AdminUsersContainer'; import PasswordResetModal from './Users/PasswordResetModal'; import InviteUserControl from './Users/InviteUserControl'; import UserTable from './Users/UserTable'; class UserManagement extends React.Component { constructor(props) { super(); this.state = { isNotifyCommentShow: false, }; this.handlePage = this.handlePage.bind(this); this.handleChangeSearchText = this.handleChangeSearchText.bind(this); } componentWillMount() { this.handlePage(1); } async handlePage(selectedPage) { try { await this.props.adminUsersContainer.retrieveUsersByPagingNum(selectedPage); } catch (err) { toastError(err); } } /** * For checking same check box twice * @param {string} statusType */ async handleClick(statusType) { const { adminUsersContainer } = this.props; if (!this.validateToggleStatus(statusType)) { return this.setState({ isNotifyCommentShow: true }); } if (this.state.isNotifyCommentShow) { await this.setState({ isNotifyCommentShow: false }); } adminUsersContainer.handleClick(statusType); } /** * Workaround user status check box * @param {string} statusType */ validateToggleStatus(statusType) { if (this.props.adminUsersContainer.isSelected(statusType)) { return this.props.adminUsersContainer.state.selectedStatusList.size > 1; } return true; } /** * Reset button */ resetButtonClickHandler() { const { adminUsersContainer } = this.props; try { adminUsersContainer.resetAllChanges(); this.searchUserElement.value = ''; this.state.isNotifyCommentShow = false; } catch (err) { toastError(err); } } /** * Workaround increamental search * @param {string} event */ handleChangeSearchText(event) { this.props.adminUsersContainer.handleChangeSearchText(event.target.value); } renderCheckbox(status, statusLabel, statusColor) { return (
{ this.handleClick(status) }} />
); } render() { const { t, adminUsersContainer } = this.props; const pager = (
); const clearButton = ( adminUsersContainer.state.searchText.length > 0 ? ( { adminUsersContainer.clearSearchText(); this.searchUserElement.value = ''; }} /> ) : '' ); return (
{adminUsersContainer.state.userForPasswordResetModal != null && ( )}

{t('admin:user_management.external_account')}

{t('User_Management')}

{ this.searchUserElement = searchUserElement }} onChange={this.handleChangeSearchText} /> { clearButton }
{this.renderCheckbox('all', 'All', 'secondary')} {this.renderCheckbox('registered', 'Approval Pending', 'info')} {this.renderCheckbox('active', 'Active', 'success')} {this.renderCheckbox('suspended', 'Suspended', 'warning')} {this.renderCheckbox('invited', 'Invited', 'pink')}
{ this.state.isNotifyCommentShow && {t('admin:user_management.click_twice_same_checkbox')} }
{pager} {pager}
); } } UserManagement.propTypes = { t: PropTypes.func.isRequired, // i18next appContainer: PropTypes.instanceOf(AppContainer).isRequired, adminUsersContainer: PropTypes.instanceOf(AdminUsersContainer).isRequired, }; const UserManagementWrapper = withUnstatedContainers(UserManagement, [AppContainer, AdminUsersContainer]); export default withTranslation()(UserManagementWrapper);