1 import { Component, linkEvent } from 'inferno';
2 import { Subscription } from 'rxjs';
3 import { retryWhen, delay, take } from 'rxjs/operators';
7 ListCommunitiesResponse,
12 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')
50 WebSocketService.Instance.getSite();
53 getPageFromProps(props: any): number {
54 return props.match.params.page ? Number(props.match.params.page) : 1;
57 componentWillUnmount() {
58 this.subscription.unsubscribe();
61 // Necessary for back button for some reason
62 componentWillReceiveProps(nextProps: any) {
63 if (nextProps.history.action == 'POP') {
64 this.state = this.emptyState;
65 this.state.page = this.getPageFromProps(nextProps);
72 <div class="container">
73 {this.state.loading ? (
75 <svg class="icon icon-spinner spin">
76 <use xlinkHref="#icon-spinner"></use>
81 <h5>{i18n.t('list_of_communities')}</h5>
82 <div class="table-responsive">
83 <table id="community_table" class="table table-sm table-hover">
84 <thead class="pointer">
86 <th>{i18n.t('name')}</th>
87 <th class="d-none d-lg-table-cell">{i18n.t('title')}</th>
88 <th>{i18n.t('category')}</th>
89 <th class="text-right">{i18n.t('subscribers')}</th>
90 <th class="text-right d-none d-lg-table-cell">
93 <th class="text-right d-none d-lg-table-cell">
100 {this.state.communities.map(community => (
103 <CommunityLink community={community} />
105 <td class="d-none d-lg-table-cell">{community.title}</td>
106 <td>{community.category_name}</td>
107 <td class="text-right">
108 {community.number_of_subscribers}
110 <td class="text-right d-none d-lg-table-cell">
111 {community.number_of_posts}
113 <td class="text-right d-none d-lg-table-cell">
114 {community.number_of_comments}
116 <td class="text-right">
117 {community.subscribed ? (
119 class="pointer btn-link"
122 this.handleUnsubscribe
125 {i18n.t('unsubscribe')}
129 class="pointer btn-link"
135 {i18n.t('subscribe')}
154 {this.state.page > 1 && (
156 class="btn btn-sm btn-secondary mr-1"
157 onClick={linkEvent(this, this.prevPage)}
163 {this.state.communities.length > 0 && (
165 class="btn btn-sm btn-secondary"
166 onClick={linkEvent(this, this.nextPage)}
176 this.props.history.push(`/communities/page/${this.state.page}`);
179 nextPage(i: Communities) {
186 prevPage(i: Communities) {
193 handleUnsubscribe(communityId: number) {
194 let form: FollowCommunityForm = {
195 community_id: communityId,
198 WebSocketService.Instance.followCommunity(form);
201 handleSubscribe(communityId: number) {
202 let form: FollowCommunityForm = {
203 community_id: communityId,
206 WebSocketService.Instance.followCommunity(form);
210 let listCommunitiesForm: ListCommunitiesForm = {
211 sort: SortType[SortType.TopAll],
212 limit: communityLimit,
213 page: this.state.page,
216 WebSocketService.Instance.listCommunities(listCommunitiesForm);
219 parseMessage(msg: WebSocketJsonResponse) {
221 let res = wsJsonToRes(msg);
223 toast(i18n.t(msg.error), 'danger');
225 } else if (res.op == UserOperation.ListCommunities) {
226 let data = res.data as ListCommunitiesResponse;
227 this.state.communities = data.communities;
228 this.state.communities.sort(
229 (a, b) => b.number_of_subscribers - a.number_of_subscribers
231 this.state.loading = false;
232 window.scrollTo(0, 0);
233 this.setState(this.state);
234 let table = document.querySelector('#community_table');
235 Sortable.initTable(table);
236 } else if (res.op == UserOperation.FollowCommunity) {
237 let data = res.data as CommunityResponse;
238 let found = this.state.communities.find(c => c.id == data.community.id);
239 found.subscribed = data.community.subscribed;
240 found.number_of_subscribers = data.community.number_of_subscribers;
241 this.setState(this.state);
242 } else if (res.op == UserOperation.GetSite) {
243 let data = res.data as GetSiteResponse;
244 document.title = `${i18n.t('communities')} - ${data.site.name}`;