2021-07-13 13:16:31 -07:00
|
|
|
import React from 'react';
|
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import ImmutablePureComponent from 'react-immutable-pure-component';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { debounce } from 'lodash';
|
|
|
|
import { fetchUsers } from 'soapbox/actions/admin';
|
|
|
|
import { FormattedMessage } from 'react-intl';
|
|
|
|
import AccountContainer from 'soapbox/containers/account_container';
|
|
|
|
import Column from 'soapbox/features/ui/components/column';
|
|
|
|
import ScrollableList from 'soapbox/components/scrollable_list';
|
2021-07-13 15:01:31 -07:00
|
|
|
import { Set as ImmutableSet, OrderedSet as ImmutableOrderedSet, is } from 'immutable';
|
2021-07-13 13:16:31 -07:00
|
|
|
|
2021-07-13 15:01:31 -07:00
|
|
|
export default @connect()
|
2021-07-13 13:16:31 -07:00
|
|
|
class UserIndex extends ImmutablePureComponent {
|
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
dispatch: PropTypes.func.isRequired,
|
|
|
|
};
|
|
|
|
|
2021-07-13 15:01:31 -07:00
|
|
|
state = {
|
|
|
|
isLoading: true,
|
|
|
|
filters: ImmutableSet(['local', 'active']),
|
|
|
|
accountIds: ImmutableOrderedSet(),
|
|
|
|
total: Infinity,
|
|
|
|
page: 0,
|
|
|
|
}
|
|
|
|
|
|
|
|
clearState = () => {
|
|
|
|
this.setState({
|
|
|
|
isLoading: true,
|
|
|
|
page: 0,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fetchNextPage = () => {
|
|
|
|
const nextPage = this.state.page + 1;
|
2021-07-13 15:27:11 -07:00
|
|
|
const filters = this.state.filters.toJS();
|
2021-07-13 15:01:31 -07:00
|
|
|
|
2021-07-13 15:27:11 -07:00
|
|
|
this.props.dispatch(fetchUsers(filters, nextPage))
|
2021-07-13 15:01:31 -07:00
|
|
|
.then(({ users, count }) => {
|
|
|
|
const newIds = users.map(user => user.id);
|
|
|
|
|
|
|
|
this.setState({
|
|
|
|
isLoading: false,
|
|
|
|
accountIds: this.state.accountIds.union(newIds),
|
|
|
|
total: count,
|
|
|
|
page: nextPage,
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(() => {});
|
|
|
|
}
|
|
|
|
|
2021-07-13 13:16:31 -07:00
|
|
|
componentDidMount() {
|
2021-07-13 15:01:31 -07:00
|
|
|
this.fetchNextPage();
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate(prevProps, prevState) {
|
|
|
|
if (!is(this.state.filters, prevState.filters) || !is(this.state.q, prevState.q)) {
|
|
|
|
this.clearState();
|
|
|
|
this.fetchNextPage();
|
|
|
|
}
|
2021-07-13 13:16:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
handleLoadMore = debounce(() => {
|
2021-07-13 15:01:31 -07:00
|
|
|
this.fetchNextPage();
|
|
|
|
}, 2000, { leading: true });
|
2021-07-13 13:16:31 -07:00
|
|
|
|
|
|
|
render() {
|
2021-07-13 15:01:31 -07:00
|
|
|
const { accountIds, isLoading } = this.state;
|
|
|
|
const hasMore = accountIds.count() < this.state.total;
|
2021-07-13 13:16:31 -07:00
|
|
|
|
2021-07-13 15:01:31 -07:00
|
|
|
const showLoading = isLoading && accountIds.isEmpty();
|
2021-07-13 13:16:31 -07:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Column>
|
|
|
|
<ScrollableList
|
|
|
|
scrollKey='user-index'
|
|
|
|
hasMore={hasMore}
|
2021-07-13 15:01:31 -07:00
|
|
|
isLoading={isLoading}
|
|
|
|
showLoading={showLoading}
|
2021-07-13 13:16:31 -07:00
|
|
|
onLoadMore={this.handleLoadMore}
|
|
|
|
emptyMessage={<FormattedMessage id='admin.user_index.empty' defaultMessage='No users found.' />}
|
|
|
|
>
|
|
|
|
{accountIds.map(id =>
|
|
|
|
<AccountContainer key={id} id={id} withNote={false} />,
|
|
|
|
)}
|
|
|
|
</ScrollableList>
|
|
|
|
</Column>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|