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, getPageFromProps } 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 interface CommunitiesProps {
34 export class Communities extends Component<any, CommunitiesState> {
35 private subscription: Subscription;
36 private emptyState: CommunitiesState = {
39 page: getPageFromProps(this.props),
42 constructor(props: any, context: any) {
43 super(props, context);
44 this.state = this.emptyState;
45 this.subscription = WebSocketService.Instance.subject
46 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
48 msg => this.parseMessage(msg),
49 err => console.error(err),
50 () => console.log('complete')
54 WebSocketService.Instance.getSite();
57 componentWillUnmount() {
58 this.subscription.unsubscribe();
61 static getDerivedStateFromProps(props: any): CommunitiesProps {
63 page: getPageFromProps(props),
67 componentDidUpdate(_: any, lastState: CommunitiesState) {
68 if (lastState.page !== this.state.page) {
69 this.setState({ loading: true });
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 <CommunityLink community={community} />
109 <td class="d-none d-lg-table-cell">{community.title}</td>
110 <td>{community.category_name}</td>
111 <td class="text-right">
112 {community.number_of_subscribers}
114 <td class="text-right d-none d-lg-table-cell">
115 {community.number_of_posts}
117 <td class="text-right d-none d-lg-table-cell">
118 {community.number_of_comments}
120 <td class="text-right">
121 {community.subscribed ? (
123 class="pointer btn-link"
126 this.handleUnsubscribe
129 {i18n.t('unsubscribe')}
133 class="pointer btn-link"
139 {i18n.t('subscribe')}
158 {this.state.page > 1 && (
160 class="btn btn-secondary mr-1"
161 onClick={linkEvent(this, this.prevPage)}
167 {this.state.communities.length > 0 && (
169 class="btn btn-secondary"
170 onClick={linkEvent(this, this.nextPage)}
179 updateUrl(paramUpdates: CommunitiesProps) {
180 const page = paramUpdates.page || this.state.page;
181 this.props.history.push(`/communities/page/${page}`);
184 nextPage(i: Communities) {
185 i.updateUrl({ page: i.state.page + 1 });
188 prevPage(i: Communities) {
189 i.updateUrl({ page: i.state.page - 1 });
192 handleUnsubscribe(communityId: number) {
193 let form: FollowCommunityForm = {
194 community_id: communityId,
197 WebSocketService.Instance.followCommunity(form);
200 handleSubscribe(communityId: number) {
201 let form: FollowCommunityForm = {
202 community_id: communityId,
205 WebSocketService.Instance.followCommunity(form);
209 let listCommunitiesForm: ListCommunitiesForm = {
210 sort: SortType[SortType.TopAll],
211 limit: communityLimit,
212 page: this.state.page,
215 WebSocketService.Instance.listCommunities(listCommunitiesForm);
218 parseMessage(msg: WebSocketJsonResponse) {
220 let res = wsJsonToRes(msg);
222 toast(i18n.t(msg.error), 'danger');
224 } else if (res.op == UserOperation.ListCommunities) {
225 let data = res.data as ListCommunitiesResponse;
226 this.state.communities = data.communities;
227 this.state.communities.sort(
228 (a, b) => b.number_of_subscribers - a.number_of_subscribers
230 this.state.loading = false;
231 window.scrollTo(0, 0);
232 this.setState(this.state);
233 let table = document.querySelector('#community_table');
234 Sortable.initTable(table);
235 } else if (res.op == UserOperation.FollowCommunity) {
236 let data = res.data as CommunityResponse;
237 let found = this.state.communities.find(c => c.id == data.community.id);
238 found.subscribed = data.community.subscribed;
239 found.number_of_subscribers = data.community.number_of_subscribers;
240 this.setState(this.state);
241 } else if (res.op == UserOperation.GetSite) {
242 let data = res.data as GetSiteResponse;
243 document.title = `${i18n.t('communities')} - ${data.site.name}`;