import React from 'react'; import Button from 'misago/components/button'; // jshint ignore:line import Search from 'misago/components/quick-search'; // jshint ignore:line import UsersList from 'misago/components/users-list/root'; // jshint ignore:line import misago from 'misago/index'; import { hydrate, append } from 'misago/reducers/users'; // jshint ignore:line import ajax from 'misago/services/ajax'; import snackbar from 'misago/services/snackbar'; import store from 'misago/services/store'; import title from 'misago/services/page-title'; export default class extends React.Component { constructor(props) { super(props); this.setSpecialProps(); if (misago.has(this.PRELOADED_DATA_KEY)) { this.initWithPreloadedData(misago.pop(this.PRELOADED_DATA_KEY)); } else { this.initWithoutPreloadedData(); } } setSpecialProps() { this.PRELOADED_DATA_KEY = 'PROFILE_FOLLOWERS'; this.TITLE = gettext('Followers'); this.API_FILTER = 'followers'; } initWithPreloadedData(data) { this.state = { isLoaded: true, isBusy: false, search: '', count: data.count, more: data.more, page: data.page, pages: data.pages }; store.dispatch(hydrate(data.results)); } initWithoutPreloadedData() { this.state = { isLoaded: false, isBusy: false, search: '', count: 0, more: 0, page: 1, pages: 1 }; this.loadUsers(); } loadUsers(page=1, search=null) { const apiUrl = this.props.profile.api_url[this.API_FILTER]; ajax.get(apiUrl, { search: search, page: page || 1 }, 'user-' + this.API_FILTER).then((data) => { if (page === 1) { store.dispatch(hydrate(data.results)); } else { store.dispatch(append(data.results)); } this.setState({ isLoaded: true, isBusy: false, count: data.count, more: data.more, page: data.page, pages: data.pages }); }, (rejection) => { snackbar.apiError(rejection); }); } componentDidMount() { title.set({ title: this.TITLE, parent: this.props.profile.username }); } /* jshint ignore:start */ loadMore = () => { this.setState({ isBusy: true }); this.loadUsers(this.state.page + 1, this.state.search); }; search = (ev) => { this.setState({ isLoaded: false, isBusy: true, search: ev.target.value, count: 0, more: 0, page: 1, pages: 1 }); this.loadUsers(1, ev.target.value); }; /* jshint ignore:end */ getLabel() { if (!this.state.isLoaded) { return gettext('Loading...'); } else if (this.state.search) { let message = ngettext( "Found %(users)s user.", "Found %(users)s users.", this.state.count); return interpolate(message, { 'users': this.state.count }, true); } else if (this.props.profile.id === this.props.user.id) { let message = ngettext( "You have %(users)s follower.", "You have %(users)s followers.", this.state.count); return interpolate(message, { 'users': this.state.count }, true); } else { let message = ngettext( "%(username)s has %(users)s follower.", "%(username)s has %(users)s followers.", this.state.count); return interpolate(message, { 'username': this.props.profile.username, 'users': this.state.count }, true); } } getEmptyMessage() { if (this.state.search) { return gettext("Search returned no users matching specified criteria."); } else if (this.props.user.id === this.props.profile.id) { return gettext("You have no followers."); } else { return interpolate(gettext("%(username)s has no followers."), { 'username': this.props.profile.username }, true); } } getMoreButton() { if (this.state.more) { /* jshint ignore:start */ return
{this.getEmptyMessage()}
; /* jshint ignore:end */ } else { /* jshint ignore:start */ return