// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved. // See License.txt for license information. import UserList from 'components/user_list.jsx'; import * as Utils from 'utils/utils.jsx'; import $ from 'jquery'; import React from 'react'; import ReactDOM from 'react-dom'; import {FormattedMessage} from 'react-intl'; const NEXT_BUTTON_TIMEOUT = 500; export default class SearchableUserList extends React.Component { constructor(props) { super(props); this.nextPage = this.nextPage.bind(this); this.previousPage = this.previousPage.bind(this); this.doSearch = this.doSearch.bind(this); this.nextTimeoutId = 0; this.state = { page: 0, search: false, nextDisabled: false }; } componentDidMount() { if (this.props.focusOnMount) { this.refs.filter.focus(); } } componentDidUpdate(prevProps, prevState) { if (this.state.page !== prevState.page) { $(ReactDOM.findDOMNode(this.refs.userList)).scrollTop(0); } } componentWillUnmount() { clearTimeout(this.nextTimeoutId); } nextPage(e) { e.preventDefault(); this.setState({page: this.state.page + 1, nextDisabled: true}); this.nextTimeoutId = setTimeout(() => this.setState({nextDisabled: false}), NEXT_BUTTON_TIMEOUT); this.props.nextPage(this.state.page + 1); } previousPage(e) { e.preventDefault(); this.setState({page: this.state.page - 1}); } doSearch() { const term = this.refs.filter.value; this.props.search(term); if (term === '') { this.setState({page: 0, search: false}); } else { this.setState({search: true}); } } render() { let nextButton; let previousButton; let usersToDisplay; let count; if (this.props.users == null) { usersToDisplay = this.props.users; } else if (this.state.search || this.props.users == null) { usersToDisplay = this.props.users; if (this.props.total) { count = ( ); } } else { const pageStart = this.state.page * this.props.usersPerPage; const pageEnd = pageStart + this.props.usersPerPage; usersToDisplay = this.props.users.slice(pageStart, pageEnd); if (usersToDisplay.length >= this.props.usersPerPage) { nextButton = ( ); } if (this.state.page > 0) { previousButton = ( ); } if (this.props.total) { const startCount = this.state.page * this.props.usersPerPage; const endCount = startCount + usersToDisplay.length; count = ( ); } } return (
{count}
{previousButton} {nextButton}
); } } SearchableUserList.defaultProps = { users: [], usersPerPage: 50, //eslint-disable-line no-magic-numbers extraInfo: {}, actions: [], actionProps: {}, actionUserProps: {}, showTeamToggle: false, focusOnMount: false }; SearchableUserList.propTypes = { users: React.PropTypes.arrayOf(React.PropTypes.object), usersPerPage: React.PropTypes.number, total: React.PropTypes.number, extraInfo: React.PropTypes.object, nextPage: React.PropTypes.func.isRequired, search: React.PropTypes.func.isRequired, actions: React.PropTypes.arrayOf(React.PropTypes.func), actionProps: React.PropTypes.object, actionUserProps: React.PropTypes.object, focusOnMount: React.PropTypes.bool.isRequired };