bigbuffet-rw/app/soapbox/features/admin/user_index.js

116 lines
3 KiB
JavaScript
Raw Normal View History

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';
import { Set as ImmutableSet, OrderedSet as ImmutableOrderedSet, is } from 'immutable';
export default @connect()
class UserIndex extends ImmutablePureComponent {
static propTypes = {
dispatch: PropTypes.func.isRequired,
};
state = {
isLoading: true,
filters: ImmutableSet(['local', 'active']),
accountIds: ImmutableOrderedSet(),
total: Infinity,
2021-07-13 16:49:41 -07:00
pageSize: 50,
page: 0,
2021-07-13 18:14:15 -07:00
query: '',
}
2021-07-13 18:14:15 -07:00
clearState = callback => {
this.setState({
isLoading: true,
2021-07-13 18:14:15 -07:00
accountIds: ImmutableOrderedSet(),
page: 0,
2021-07-13 18:14:15 -07:00
}, callback);
}
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 18:14:15 -07:00
this.props.dispatch(fetchUsers(filters, nextPage, query, pageSize))
.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(() => {});
}
componentDidMount() {
this.fetchNextPage();
}
2021-07-13 18:14:15 -07:00
refresh = () => {
this.clearState(() => {
this.fetchNextPage();
});
}
refreshDebounced = debounce(() => {
this.refresh();
}, 1000)
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();
}
}
handleLoadMore = debounce(() => {
this.fetchNextPage();
}, 2000, { leading: true });
2021-07-13 18:14:15 -07:00
handleQueryChange = e => {
this.setState({ query: e.target.value });
}
render() {
const { accountIds, isLoading } = this.state;
const hasMore = accountIds.count() < this.state.total;
const showLoading = isLoading && accountIds.isEmpty();
return (
<Column>
2021-07-13 18:14:15 -07:00
<input value={this.state.q} onChange={this.handleQueryChange} />
<ScrollableList
scrollKey='user-index'
hasMore={hasMore}
isLoading={isLoading}
showLoading={showLoading}
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>
);
}
}