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,
|
2021-07-13 16:49:41 -07:00
|
|
|
pageSize: 50,
|
2021-07-13 15:01:31 -07:00
|
|
|
page: 0,
|
2021-07-13 18:14:15 -07:00
|
|
|
query: '',
|
2021-07-13 15:01:31 -07:00
|
|
|
}
|
|
|
|
|
2021-07-13 18:14:15 -07:00
|
|
|
clearState = callback => {
|
2021-07-13 15:01:31 -07:00
|
|
|
this.setState({
|
|
|
|
isLoading: true,
|
2021-07-13 18:14:15 -07:00
|
|
|
accountIds: ImmutableOrderedSet(),
|
2021-07-13 15:01:31 -07:00
|
|
|
page: 0,
|
2021-07-13 18:14:15 -07:00
|
|
|
}, callback);
|
2021-07-13 15:01:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fetchNextPage = () => {
|
2021-07-13 18:14:15 -07:00
|
|
|
const { filters, page, query, pageSize } = this.state;
|
2021-07-13 15:59:55 -07:00
|
|
|
const nextPage = page + 1;
|
2021-07-13 15:01:31 -07:00
|
|
|
|
2021-07-13 18:14:15 -07:00
|
|
|
this.props.dispatch(fetchUsers(filters, nextPage, query, pageSize))
|
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();
|
|
|
|
}
|
|
|
|
|
2021-07-13 18:14:15 -07:00
|
|
|
refresh = () => {
|
|
|
|
this.clearState(() => {
|
|
|
|
this.fetchNextPage();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
refreshDebounced = debounce(() => {
|
|
|
|
this.refresh();
|
|
|
|
}, 1000)
|
|
|
|
|
2021-07-13 15:01:31 -07:00
|
|
|
componentDidUpdate(prevProps, prevState) {
|
2021-07-13 18:14:15 -07:00
|
|
|
const { filters, query } = this.state;
|
|
|
|
const filtersChanged = !is(filters, prevState.filters);
|
|
|
|
const queryChanged = query !== prevState.query;
|
2021-07-13 16:49:41 -07:00
|
|
|
|
2021-07-13 18:14:15 -07:00
|
|
|
if (filtersChanged) {
|
|
|
|
this.refresh();
|
|
|
|
} else if (queryChanged) {
|
|
|
|
this.refreshDebounced();
|
2021-07-13 15:01:31 -07:00
|
|
|
}
|
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
|
|
|
|
2021-07-13 18:14:15 -07:00
|
|
|
handleQueryChange = e => {
|
|
|
|
this.setState({ query: e.target.value });
|
|
|
|
}
|
|
|
|
|
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>
|
2021-07-13 18:14:15 -07:00
|
|
|
<input value={this.state.q} onChange={this.handleQueryChange} />
|
2021-07-13 13:16:31 -07:00
|
|
|
<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>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|