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 ? (
80 <svg class="icon icon-spinner spin">
81 <use xlinkHref="#icon-spinner"></use>
87 <T i18nKey="list_of_communities">#</T>
89 <div class="table-responsive">
90 <table id="community_table" class="table table-sm table-hover">
91 <thead class="pointer">
94 <T i18nKey="name">#</T>
96 <th class="d-none d-lg-table-cell">
97 <T i18nKey="title">#</T>
100 <T i18nKey="category">#</T>
102 <th class="text-right">
103 <T i18nKey="subscribers">#</T>
105 <th class="text-right d-none d-lg-table-cell">
106 <T i18nKey="posts">#</T>
108 <th class="text-right d-none d-lg-table-cell">
109 <T i18nKey="comments">#</T>
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 <T i18nKey="subscribe">#</T>
171 {this.state.page > 1 && (
173 class="btn btn-sm btn-secondary mr-1"
174 onClick={linkEvent(this, this.prevPage)}
176 <T i18nKey="prev">#</T>
179 {this.state.communities.length == communityLimit && (
181 class="btn btn-sm btn-secondary"
182 onClick={linkEvent(this, this.nextPage)}
184 <T i18nKey="next">#</T>
192 this.props.history.push(`/communities/page/${this.state.page}`);
195 nextPage(i: Communities) {
202 prevPage(i: Communities) {
209 handleUnsubscribe(communityId: number) {
210 let form: FollowCommunityForm = {
211 community_id: communityId,
214 WebSocketService.Instance.followCommunity(form);
217 handleSubscribe(communityId: number) {
218 let form: FollowCommunityForm = {
219 community_id: communityId,
222 WebSocketService.Instance.followCommunity(form);
226 let listCommunitiesForm: ListCommunitiesForm = {
227 sort: SortType[SortType.TopAll],
228 limit: communityLimit,
229 page: this.state.page,
232 WebSocketService.Instance.listCommunities(listCommunitiesForm);
235 parseMessage(msg: WebSocketJsonResponse) {
237 let res = wsJsonToRes(msg);
239 toast(i18n.t(msg.error), 'danger');
241 } else if (res.op == UserOperation.ListCommunities) {
242 let data = res.data as ListCommunitiesResponse;
243 this.state.communities = data.communities;
244 this.state.communities.sort(
245 (a, b) => b.number_of_subscribers - a.number_of_subscribers
247 this.state.loading = false;
248 window.scrollTo(0, 0);
249 this.setState(this.state);
250 let table = document.querySelector('#community_table');
251 Sortable.initTable(table);
252 } else if (res.op == UserOperation.FollowCommunity) {
253 let data = res.data as CommunityResponse;
254 let found = this.state.communities.find(c => c.id == data.community.id);
255 found.subscribed = data.community.subscribed;
256 found.number_of_subscribers = data.community.number_of_subscribers;
257 this.setState(this.state);