1 import { Component, linkEvent } from 'inferno';
2 import { HtmlTags } from './html-tags';
3 import { Subscription } from 'rxjs';
7 ListCommunitiesResponse,
14 } from 'lemmy-js-client';
15 import { WebSocketService } from '../services';
28 import { CommunityLink } from './community-link';
29 import { Spinner } from './icon';
30 import { i18n } from '../i18next';
31 import { InitialFetchRequest } from 'shared/interfaces';
33 const communityLimit = 100;
35 interface CommunitiesState {
36 communities: CommunityView[];
43 interface CommunitiesProps {
47 export class Communities extends Component<any, CommunitiesState> {
48 private subscription: Subscription;
49 private isoData = setIsoData(this.context);
50 private emptyState: CommunitiesState = {
53 page: getPageFromProps(this.props),
54 site_view: this.isoData.site_res.site_view,
58 constructor(props: any, context: any) {
59 super(props, context);
60 this.state = this.emptyState;
62 this.parseMessage = this.parseMessage.bind(this);
63 this.subscription = wsSubscribe(this.parseMessage);
65 // Only fetch the data if coming from another route
66 if (this.isoData.path == this.context.router.route.match.url) {
67 this.state.communities = this.isoData.routeData[0].communities;
68 this.state.communities.sort(
69 (a, b) => b.counts.subscribers - a.counts.subscribers
71 this.state.loading = false;
77 componentWillUnmount() {
79 this.subscription.unsubscribe();
83 static getDerivedStateFromProps(props: any): CommunitiesProps {
85 page: getPageFromProps(props),
89 componentDidUpdate(_: any, lastState: CommunitiesState) {
90 if (lastState.page !== this.state.page) {
91 this.setState({ loading: true });
96 get documentTitle(): string {
97 return `${i18n.t('communities')} - ${this.state.site_view.site.name}`;
102 <div class="container">
104 title={this.documentTitle}
105 path={this.context.router.route.match.url}
107 {this.state.loading ? (
114 <div class="col-md-6">
115 <h4>{i18n.t('list_of_communities')}</h4>
117 <div class="col-md-6">
118 <div class="float-md-right">{this.searchForm()}</div>
122 <div class="table-responsive">
123 <table id="community_table" class="table table-sm table-hover">
124 <thead class="pointer">
126 <th>{i18n.t('name')}</th>
127 <th>{i18n.t('category')}</th>
128 <th class="text-right">{i18n.t('subscribers')}</th>
129 <th class="text-right">
130 {i18n.t('users')} / {i18n.t('month')}
132 <th class="text-right d-none d-lg-table-cell">
135 <th class="text-right d-none d-lg-table-cell">
142 {this.state.communities.map(cv => (
145 <CommunityLink community={cv.community} />
147 <td>{cv.category.name}</td>
148 <td class="text-right">{cv.counts.subscribers}</td>
149 <td class="text-right">{cv.counts.users_active_month}</td>
150 <td class="text-right d-none d-lg-table-cell">
153 <td class="text-right d-none d-lg-table-cell">
156 <td class="text-right">
159 class="pointer btn-link"
163 this.handleUnsubscribe
166 {i18n.t('unsubscribe')}
170 class="pointer btn-link"
177 {i18n.t('subscribe')}
197 onSubmit={linkEvent(this, this.handleSearchSubmit)}
201 class="form-control mr-2 mb-2"
202 value={this.state.searchText}
203 placeholder={`${i18n.t('search')}...`}
204 onInput={linkEvent(this, this.handleSearchChange)}
208 <button type="submit" class="btn btn-secondary mr-2 mb-2">
209 <span>{i18n.t('search')}</span>
218 {this.state.page > 1 && (
220 class="btn btn-secondary mr-1"
221 onClick={linkEvent(this, this.prevPage)}
227 {this.state.communities.length > 0 && (
229 class="btn btn-secondary"
230 onClick={linkEvent(this, this.nextPage)}
239 updateUrl(paramUpdates: CommunitiesProps) {
240 const page = paramUpdates.page || this.state.page;
241 this.props.history.push(`/communities/page/${page}`);
244 nextPage(i: Communities) {
245 i.updateUrl({ page: i.state.page + 1 });
248 prevPage(i: Communities) {
249 i.updateUrl({ page: i.state.page - 1 });
252 handleUnsubscribe(communityId: number) {
253 let form: FollowCommunity = {
254 community_id: communityId,
258 WebSocketService.Instance.send(wsClient.followCommunity(form));
261 handleSubscribe(communityId: number) {
262 let form: FollowCommunity = {
263 community_id: communityId,
267 WebSocketService.Instance.send(wsClient.followCommunity(form));
270 handleSearchChange(i: Communities, event: any) {
271 i.setState({ searchText: event.target.value });
274 handleSearchSubmit(i: Communities) {
275 const searchParamEncoded = encodeURIComponent(i.state.searchText);
276 i.context.router.history.push(
277 `/search/q/${searchParamEncoded}/type/Communities/sort/TopAll/page/1`
282 let listCommunitiesForm: ListCommunities = {
283 type_: ListingType.All,
284 sort: SortType.TopAll,
285 limit: communityLimit,
286 page: this.state.page,
287 auth: authField(false),
290 WebSocketService.Instance.send(
291 wsClient.listCommunities(listCommunitiesForm)
295 static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
296 let pathSplit = req.path.split('/');
297 let page = pathSplit[3] ? Number(pathSplit[3]) : 1;
298 let listCommunitiesForm: ListCommunities = {
299 type_: ListingType.All,
300 sort: SortType.TopAll,
301 limit: communityLimit,
304 setOptionalAuth(listCommunitiesForm, req.auth);
306 return [req.client.listCommunities(listCommunitiesForm)];
309 parseMessage(msg: any) {
310 let op = wsUserOp(msg);
312 toast(i18n.t(msg.error), 'danger');
314 } else if (op == UserOperation.ListCommunities) {
315 let data = wsJsonToRes<ListCommunitiesResponse>(msg).data;
316 this.state.communities = data.communities;
317 this.state.communities.sort(
318 (a, b) => b.counts.subscribers - a.counts.subscribers
320 this.state.loading = false;
321 window.scrollTo(0, 0);
322 this.setState(this.state);
323 } else if (op == UserOperation.FollowCommunity) {
324 let data = wsJsonToRes<CommunityResponse>(msg).data;
325 let found = this.state.communities.find(
326 c => c.community.id == data.community_view.community.id
328 found.subscribed = data.community_view.subscribed;
329 found.counts.subscribers = data.community_view.counts.subscribers;
330 this.setState(this.state);