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 { i18n } from '../i18next';
19 declare const Sortable: any;
21 const communityLimit = 100;
23 interface CommunitiesState {
24 communities: Array<Community>;
29 export class Communities extends Component<any, CommunitiesState> {
30 private subscription: Subscription;
31 private emptyState: CommunitiesState = {
34 page: this.getPageFromProps(this.props),
37 constructor(props: any, context: any) {
38 super(props, context);
39 this.state = this.emptyState;
40 this.subscription = WebSocketService.Instance.subject
41 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
43 msg => this.parseMessage(msg),
44 err => console.error(err),
45 () => console.log('complete')
51 getPageFromProps(props: any): number {
52 return props.match.params.page ? Number(props.match.params.page) : 1;
55 componentWillUnmount() {
56 this.subscription.unsubscribe();
60 document.title = `${i18n.t('communities')} - ${
61 WebSocketService.Instance.site.name
65 // Necessary for back button for some reason
66 componentWillReceiveProps(nextProps: any) {
67 if (nextProps.history.action == 'POP') {
68 this.state = this.emptyState;
69 this.state.page = this.getPageFromProps(nextProps);
76 <div class="container">
77 {this.state.loading ? (
79 <svg class="icon icon-spinner spin">
80 <use xlinkHref="#icon-spinner"></use>
85 <h5>{i18n.t('list_of_communities')}</h5>
86 <div class="table-responsive">
87 <table id="community_table" class="table table-sm table-hover">
88 <thead class="pointer">
90 <th>{i18n.t('name')}</th>
91 <th class="d-none d-lg-table-cell">{i18n.t('title')}</th>
92 <th>{i18n.t('category')}</th>
93 <th class="text-right">{i18n.t('subscribers')}</th>
94 <th class="text-right d-none d-lg-table-cell">
97 <th class="text-right d-none d-lg-table-cell">
104 {this.state.communities.map(community => (
107 <Link to={`/c/${community.name}`}>
111 <td class="d-none d-lg-table-cell">{community.title}</td>
112 <td>{community.category_name}</td>
113 <td class="text-right">
114 {community.number_of_subscribers}
116 <td class="text-right d-none d-lg-table-cell">
117 {community.number_of_posts}
119 <td class="text-right d-none d-lg-table-cell">
120 {community.number_of_comments}
122 <td class="text-right">
123 {community.subscribed ? (
125 class="pointer btn-link"
128 this.handleUnsubscribe
131 {i18n.t('unsubscribe')}
135 class="pointer btn-link"
141 {i18n.t('subscribe')}
160 {this.state.page > 1 && (
162 class="btn btn-sm btn-secondary mr-1"
163 onClick={linkEvent(this, this.prevPage)}
169 {this.state.communities.length == communityLimit && (
171 class="btn btn-sm btn-secondary"
172 onClick={linkEvent(this, this.nextPage)}
182 this.props.history.push(`/communities/page/${this.state.page}`);
185 nextPage(i: Communities) {
192 prevPage(i: Communities) {
199 handleUnsubscribe(communityId: number) {
200 let form: FollowCommunityForm = {
201 community_id: communityId,
204 WebSocketService.Instance.followCommunity(form);
207 handleSubscribe(communityId: number) {
208 let form: FollowCommunityForm = {
209 community_id: communityId,
212 WebSocketService.Instance.followCommunity(form);
216 let listCommunitiesForm: ListCommunitiesForm = {
217 sort: SortType[SortType.TopAll],
218 limit: communityLimit,
219 page: this.state.page,
222 WebSocketService.Instance.listCommunities(listCommunitiesForm);
225 parseMessage(msg: WebSocketJsonResponse) {
227 let res = wsJsonToRes(msg);
229 toast(i18n.t(msg.error), 'danger');
231 } else if (res.op == UserOperation.ListCommunities) {
232 let data = res.data as ListCommunitiesResponse;
233 this.state.communities = data.communities;
234 this.state.communities.sort(
235 (a, b) => b.number_of_subscribers - a.number_of_subscribers
237 this.state.loading = false;
238 window.scrollTo(0, 0);
239 this.setState(this.state);
240 let table = document.querySelector('#community_table');
241 Sortable.initTable(table);
242 } else if (res.op == UserOperation.FollowCommunity) {
243 let data = res.data as CommunityResponse;
244 let found = this.state.communities.find(c => c.id == data.community.id);
245 found.subscribed = data.community.subscribed;
246 found.number_of_subscribers = data.community.number_of_subscribers;
247 this.setState(this.state);