1 import { Component, linkEvent } from 'inferno';
2 import { Link } from 'inferno-router';
3 import { Subscription } from 'rxjs';
4 import { retryWhen, delay, take } from 'rxjs/operators';
8 ListCommunitiesResponse,
13 WebSocketJsonResponse,
14 } from '../interfaces';
15 import { WebSocketService } from '../services';
16 import { wsJsonToRes, toast } from '../utils';
17 import { CommunityLink } from './community-link';
18 import { i18n } from '../i18next';
20 declare const Sortable: any;
22 const communityLimit = 100;
24 interface CommunitiesState {
25 communities: Array<Community>;
30 export class Communities extends Component<any, CommunitiesState> {
31 private subscription: Subscription;
32 private emptyState: CommunitiesState = {
35 page: this.getPageFromProps(this.props),
38 constructor(props: any, context: any) {
39 super(props, context);
40 this.state = this.emptyState;
41 this.subscription = WebSocketService.Instance.subject
42 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
44 msg => this.parseMessage(msg),
45 err => console.error(err),
46 () => console.log('complete')
52 getPageFromProps(props: any): number {
53 return props.match.params.page ? Number(props.match.params.page) : 1;
56 componentWillUnmount() {
57 this.subscription.unsubscribe();
61 document.title = `${i18n.t('communities')} - ${
62 WebSocketService.Instance.site.name
66 // Necessary for back button for some reason
67 componentWillReceiveProps(nextProps: any) {
68 if (nextProps.history.action == 'POP') {
69 this.state = this.emptyState;
70 this.state.page = this.getPageFromProps(nextProps);
77 <div class="container">
78 {this.state.loading ? (
80 <svg class="icon icon-spinner spin">
81 <use xlinkHref="#icon-spinner"></use>
86 <h5>{i18n.t('list_of_communities')}</h5>
87 <div class="table-responsive">
88 <table id="community_table" class="table table-sm table-hover">
89 <thead class="pointer">
91 <th>{i18n.t('name')}</th>
92 <th class="d-none d-lg-table-cell">{i18n.t('title')}</th>
93 <th>{i18n.t('category')}</th>
94 <th class="text-right">{i18n.t('subscribers')}</th>
95 <th class="text-right d-none d-lg-table-cell">
98 <th class="text-right d-none d-lg-table-cell">
105 {this.state.communities.map(community => (
108 <CommunityLink community={community} />
110 <td class="d-none d-lg-table-cell">{community.title}</td>
111 <td>{community.category_name}</td>
112 <td class="text-right">
113 {community.number_of_subscribers}
115 <td class="text-right d-none d-lg-table-cell">
116 {community.number_of_posts}
118 <td class="text-right d-none d-lg-table-cell">
119 {community.number_of_comments}
121 <td class="text-right">
122 {community.subscribed ? (
124 class="pointer btn-link"
127 this.handleUnsubscribe
130 {i18n.t('unsubscribe')}
134 class="pointer btn-link"
140 {i18n.t('subscribe')}
159 {this.state.page > 1 && (
161 class="btn btn-sm btn-secondary mr-1"
162 onClick={linkEvent(this, this.prevPage)}
168 {this.state.communities.length == communityLimit && (
170 class="btn btn-sm btn-secondary"
171 onClick={linkEvent(this, this.nextPage)}
181 this.props.history.push(`/communities/page/${this.state.page}`);
184 nextPage(i: Communities) {
191 prevPage(i: Communities) {
198 handleUnsubscribe(communityId: number) {
199 let form: FollowCommunityForm = {
200 community_id: communityId,
203 WebSocketService.Instance.followCommunity(form);
206 handleSubscribe(communityId: number) {
207 let form: FollowCommunityForm = {
208 community_id: communityId,
211 WebSocketService.Instance.followCommunity(form);
215 let listCommunitiesForm: ListCommunitiesForm = {
216 sort: SortType[SortType.TopAll],
217 limit: communityLimit,
218 page: this.state.page,
221 WebSocketService.Instance.listCommunities(listCommunitiesForm);
224 parseMessage(msg: WebSocketJsonResponse) {
226 let res = wsJsonToRes(msg);
228 toast(i18n.t(msg.error), 'danger');
230 } else if (res.op == UserOperation.ListCommunities) {
231 let data = res.data as ListCommunitiesResponse;
232 this.state.communities = data.communities;
233 this.state.communities.sort(
234 (a, b) => b.number_of_subscribers - a.number_of_subscribers
236 this.state.loading = false;
237 window.scrollTo(0, 0);
238 this.setState(this.state);
239 let table = document.querySelector('#community_table');
240 Sortable.initTable(table);
241 } else if (res.op == UserOperation.FollowCommunity) {
242 let data = res.data as CommunityResponse;
243 let found = this.state.communities.find(c => c.id == data.community.id);
244 found.subscribed = data.community.subscribed;
245 found.number_of_subscribers = data.community.number_of_subscribers;
246 this.setState(this.state);