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';
18 import { T } from 'inferno-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')
52 getPageFromProps(props: any): number {
53 return props.match.params.page ? Number(props.match.params.page) : 1;
56 componentWillUnmount() {
57 this.subscription.unsubscribe();
61 document.title = `${i18n.t('communities')} - ${
62 WebSocketService.Instance.site.name
66 // Necessary for back button for some reason
67 componentWillReceiveProps(nextProps: any) {
68 if (nextProps.history.action == 'POP') {
69 this.state = this.emptyState;
70 this.state.page = this.getPageFromProps(nextProps);
77 <div class="container">
78 {this.state.loading ? (
79 <p class="text-center">
80 <svg class="icon icon-spinner spin">
81 <use xlinkHref="#icon-spinner"></use>
87 { i18n.t('list_of_communities') }
89 <div class="table-responsive">
90 <table id="community_table" class="table table-sm table-hover">
91 <thead class="pointer">
96 <th class="d-none d-lg-table-cell">
100 { i18n.t('category') }
102 <th class="text-right">
103 { i18n.t('subscribers') }
105 <th class="text-right d-none d-lg-table-cell">
108 <th class="text-right d-none d-lg-table-cell">
109 { i18n.t('comments') }
115 {this.state.communities.map(community => (
118 <Link to={`/c/${community.name}`}>
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 <T i18nKey="unsubscribe">#</T>
146 class="pointer btn-link"
152 { i18n.t('subscribe') }
171 {this.state.page > 1 && (
173 class="btn btn-sm btn-secondary mr-1"
174 onClick={linkEvent(this, this.prevPage)}
180 {this.state.communities.length == communityLimit && (
182 class="btn btn-sm btn-secondary"
183 onClick={linkEvent(this, this.nextPage)}
193 this.props.history.push(`/communities/page/${this.state.page}`);
196 nextPage(i: Communities) {
203 prevPage(i: Communities) {
210 handleUnsubscribe(communityId: number) {
211 let form: FollowCommunityForm = {
212 community_id: communityId,
215 WebSocketService.Instance.followCommunity(form);
218 handleSubscribe(communityId: number) {
219 let form: FollowCommunityForm = {
220 community_id: communityId,
223 WebSocketService.Instance.followCommunity(form);
227 let listCommunitiesForm: ListCommunitiesForm = {
228 sort: SortType[SortType.TopAll],
229 limit: communityLimit,
230 page: this.state.page,
233 WebSocketService.Instance.listCommunities(listCommunitiesForm);
236 parseMessage(msg: WebSocketJsonResponse) {
238 let res = wsJsonToRes(msg);
240 toast(i18n.t(msg.error), 'danger');
242 } else if (res.op == UserOperation.ListCommunities) {
243 let data = res.data as ListCommunitiesResponse;
244 this.state.communities = data.communities;
245 this.state.communities.sort(
246 (a, b) => b.number_of_subscribers - a.number_of_subscribers
248 this.state.loading = false;
249 window.scrollTo(0, 0);
250 this.setState(this.state);
251 let table = document.querySelector('#community_table');
252 Sortable.initTable(table);
253 } else if (res.op == UserOperation.FollowCommunity) {
254 let data = res.data as CommunityResponse;
255 let found = this.state.communities.find(c => c.id == data.community.id);
256 found.subscribed = data.community.subscribed;
257 found.number_of_subscribers = data.community.number_of_subscribers;
258 this.setState(this.state);