1 import { Component, linkEvent } from 'inferno';
2 import { Helmet } from 'inferno-helmet';
3 import { Subscription } from 'rxjs';
4 import { retryWhen, delay, take } from 'rxjs/operators';
8 ListCommunitiesResponse,
13 WebSocketJsonResponse,
16 } from '../interfaces';
17 import { WebSocketService } from '../services';
18 import { wsJsonToRes, toast, getPageFromProps } from '../utils';
19 import { CommunityLink } from './community-link';
20 import { i18n } from '../i18next';
22 declare const Sortable: any;
24 const communityLimit = 100;
26 interface CommunitiesState {
27 communities: Array<Community>;
33 interface CommunitiesProps {
37 export class Communities extends Component<any, CommunitiesState> {
38 private subscription: Subscription;
39 private emptyState: CommunitiesState = {
42 page: getPageFromProps(this.props),
46 constructor(props: any, context: any) {
47 super(props, context);
48 this.state = this.emptyState;
49 this.subscription = WebSocketService.Instance.subject
50 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
52 msg => this.parseMessage(msg),
53 err => console.error(err),
54 () => console.log('complete')
58 WebSocketService.Instance.getSite();
61 componentWillUnmount() {
62 this.subscription.unsubscribe();
65 static getDerivedStateFromProps(props: any): CommunitiesProps {
67 page: getPageFromProps(props),
71 componentDidUpdate(_: any, lastState: CommunitiesState) {
72 if (lastState.page !== this.state.page) {
73 this.setState({ loading: true });
78 get documentTitle(): string {
79 if (this.state.site) {
80 return `${i18n.t('communities')} - ${this.state.site.name}`;
88 <div class="container">
89 <Helmet title={this.documentTitle} />
90 {this.state.loading ? (
92 <svg class="icon icon-spinner spin">
93 <use xlinkHref="#icon-spinner"></use>
98 <h5>{i18n.t('list_of_communities')}</h5>
99 <div class="table-responsive">
100 <table id="community_table" class="table table-sm table-hover">
101 <thead class="pointer">
103 <th>{i18n.t('name')}</th>
104 <th class="d-none d-lg-table-cell">{i18n.t('title')}</th>
105 <th>{i18n.t('category')}</th>
106 <th class="text-right">{i18n.t('subscribers')}</th>
107 <th class="text-right d-none d-lg-table-cell">
110 <th class="text-right d-none d-lg-table-cell">
117 {this.state.communities.map(community => (
120 <CommunityLink community={community} />
122 <td class="d-none d-lg-table-cell">{community.title}</td>
123 <td>{community.category_name}</td>
124 <td class="text-right">
125 {community.number_of_subscribers}
127 <td class="text-right d-none d-lg-table-cell">
128 {community.number_of_posts}
130 <td class="text-right d-none d-lg-table-cell">
131 {community.number_of_comments}
133 <td class="text-right">
134 {community.subscribed ? (
136 class="pointer btn-link"
139 this.handleUnsubscribe
142 {i18n.t('unsubscribe')}
146 class="pointer btn-link"
152 {i18n.t('subscribe')}
171 {this.state.page > 1 && (
173 class="btn btn-secondary mr-1"
174 onClick={linkEvent(this, this.prevPage)}
180 {this.state.communities.length > 0 && (
182 class="btn btn-secondary"
183 onClick={linkEvent(this, this.nextPage)}
192 updateUrl(paramUpdates: CommunitiesProps) {
193 const page = paramUpdates.page || this.state.page;
194 this.props.history.push(`/communities/page/${page}`);
197 nextPage(i: Communities) {
198 i.updateUrl({ page: i.state.page + 1 });
201 prevPage(i: Communities) {
202 i.updateUrl({ page: i.state.page - 1 });
205 handleUnsubscribe(communityId: number) {
206 let form: FollowCommunityForm = {
207 community_id: communityId,
210 WebSocketService.Instance.followCommunity(form);
213 handleSubscribe(communityId: number) {
214 let form: FollowCommunityForm = {
215 community_id: communityId,
218 WebSocketService.Instance.followCommunity(form);
222 let listCommunitiesForm: ListCommunitiesForm = {
223 sort: SortType[SortType.TopAll],
224 limit: communityLimit,
225 page: this.state.page,
228 WebSocketService.Instance.listCommunities(listCommunitiesForm);
231 parseMessage(msg: WebSocketJsonResponse) {
233 let res = wsJsonToRes(msg);
235 toast(i18n.t(msg.error), 'danger');
237 } else if (res.op == UserOperation.ListCommunities) {
238 let data = res.data as ListCommunitiesResponse;
239 this.state.communities = data.communities;
240 this.state.communities.sort(
241 (a, b) => b.number_of_subscribers - a.number_of_subscribers
243 this.state.loading = false;
244 window.scrollTo(0, 0);
245 this.setState(this.state);
246 let table = document.querySelector('#community_table');
247 Sortable.initTable(table);
248 } else if (res.op == UserOperation.FollowCommunity) {
249 let data = res.data as CommunityResponse;
250 let found = this.state.communities.find(c => c.id == data.community.id);
251 found.subscribed = data.community.subscribed;
252 found.number_of_subscribers = data.community.number_of_subscribers;
253 this.setState(this.state);
254 } else if (res.op == UserOperation.GetSite) {
255 let data = res.data as GetSiteResponse;
256 this.state.site = data.site;
257 this.setState(this.state);