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 } from '../interfaces';
14 import { WebSocketService } from '../services';
15 import { wsJsonToRes } from '../utils';
16 import { i18n } from '../i18next';
17 import { T } from 'inferno-i18next';
19 declare const Sortable: any;
21 interface CommunitiesState {
22 communities: Array<Community>;
27 export class Communities extends Component<any, CommunitiesState> {
28 private subscription: Subscription;
29 private emptyState: CommunitiesState = {
32 page: this.getPageFromProps(this.props),
35 constructor(props: any, context: any) {
36 super(props, context);
37 this.state = this.emptyState;
38 this.subscription = WebSocketService.Instance.subject
39 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
41 msg => this.parseMessage(msg),
42 err => console.error(err),
43 () => console.log('complete')
49 getPageFromProps(props: any): number {
50 return props.match.params.page ? Number(props.match.params.page) : 1;
53 componentWillUnmount() {
54 this.subscription.unsubscribe();
58 document.title = `${i18n.t('communities')} - ${
59 WebSocketService.Instance.site.name
63 // Necessary for back button for some reason
64 componentWillReceiveProps(nextProps: any) {
65 if (nextProps.history.action == 'POP') {
66 this.state = this.emptyState;
67 this.state.page = this.getPageFromProps(nextProps);
74 <div class="container">
75 {this.state.loading ? (
77 <svg class="icon icon-spinner spin">
78 <use xlinkHref="#icon-spinner"></use>
84 <T i18nKey="list_of_communities">#</T>
86 <div class="table-responsive">
87 <table id="community_table" class="table table-sm table-hover">
88 <thead class="pointer">
91 <T i18nKey="name">#</T>
93 <th class="d-none d-lg-table-cell">
94 <T i18nKey="title">#</T>
97 <T i18nKey="category">#</T>
99 <th class="text-right">
100 <T i18nKey="subscribers">#</T>
102 <th class="text-right d-none d-lg-table-cell">
103 <T i18nKey="posts">#</T>
105 <th class="text-right d-none d-lg-table-cell">
106 <T i18nKey="comments">#</T>
112 {this.state.communities.map(community => (
115 <Link to={`/c/${community.name}`}>
119 <td class="d-none d-lg-table-cell">{community.title}</td>
120 <td>{community.category_name}</td>
121 <td class="text-right">
122 {community.number_of_subscribers}
124 <td class="text-right d-none d-lg-table-cell">
125 {community.number_of_posts}
127 <td class="text-right d-none d-lg-table-cell">
128 {community.number_of_comments}
130 <td class="text-right">
131 {community.subscribed ? (
133 class="pointer btn-link"
136 this.handleUnsubscribe
139 <T i18nKey="unsubscribe">#</T>
143 class="pointer btn-link"
149 <T i18nKey="subscribe">#</T>
168 {this.state.page > 1 && (
170 class="btn btn-sm btn-secondary mr-1"
171 onClick={linkEvent(this, this.prevPage)}
173 <T i18nKey="prev">#</T>
177 class="btn btn-sm btn-secondary"
178 onClick={linkEvent(this, this.nextPage)}
180 <T i18nKey="next">#</T>
187 this.props.history.push(`/communities/page/${this.state.page}`);
190 nextPage(i: Communities) {
197 prevPage(i: Communities) {
204 handleUnsubscribe(communityId: number) {
205 let form: FollowCommunityForm = {
206 community_id: communityId,
209 WebSocketService.Instance.followCommunity(form);
212 handleSubscribe(communityId: number) {
213 let form: FollowCommunityForm = {
214 community_id: communityId,
217 WebSocketService.Instance.followCommunity(form);
221 let listCommunitiesForm: ListCommunitiesForm = {
222 sort: SortType[SortType.TopAll],
224 page: this.state.page,
227 WebSocketService.Instance.listCommunities(listCommunitiesForm);
230 parseMessage(msg: any) {
232 let res = wsJsonToRes(msg);
234 alert(i18n.t(res.error));
236 } else if (res.op == UserOperation.ListCommunities) {
237 let data = res.data as ListCommunitiesResponse;
238 this.state.communities = data.communities;
239 this.state.communities.sort(
240 (a, b) => b.number_of_subscribers - a.number_of_subscribers
242 this.state.loading = false;
243 window.scrollTo(0, 0);
244 this.setState(this.state);
245 let table = document.querySelector('#community_table');
246 Sortable.initTable(table);
247 } else if (res.op == UserOperation.FollowCommunity) {
248 let data = res.data as CommunityResponse;
249 let found = this.state.communities.find(c => c.id == data.community.id);
250 found.subscribed = data.community.subscribed;
251 found.number_of_subscribers = data.community.number_of_subscribers;
252 this.setState(this.state);