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 } from '../utils';
17 import { i18n } from '../i18next';
18 import { T } from 'inferno-i18next';
20 declare const Sortable: any;
22 interface CommunitiesState {
23 communities: Array<Community>;
28 export class Communities extends Component<any, CommunitiesState> {
29 private subscription: Subscription;
30 private emptyState: CommunitiesState = {
33 page: this.getPageFromProps(this.props),
36 constructor(props: any, context: any) {
37 super(props, context);
38 this.state = this.emptyState;
39 this.subscription = WebSocketService.Instance.subject
40 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
42 msg => this.parseMessage(msg),
43 err => console.error(err),
44 () => console.log('complete')
50 getPageFromProps(props: any): number {
51 return props.match.params.page ? Number(props.match.params.page) : 1;
54 componentWillUnmount() {
55 this.subscription.unsubscribe();
59 document.title = `${i18n.t('communities')} - ${
60 WebSocketService.Instance.site.name
64 // Necessary for back button for some reason
65 componentWillReceiveProps(nextProps: any) {
66 if (nextProps.history.action == 'POP') {
67 this.state = this.emptyState;
68 this.state.page = this.getPageFromProps(nextProps);
75 <div class="container">
76 {this.state.loading ? (
78 <svg class="icon icon-spinner spin">
79 <use xlinkHref="#icon-spinner"></use>
85 <T i18nKey="list_of_communities">#</T>
87 <div class="table-responsive">
88 <table id="community_table" class="table table-sm table-hover">
89 <thead class="pointer">
92 <T i18nKey="name">#</T>
94 <th class="d-none d-lg-table-cell">
95 <T i18nKey="title">#</T>
98 <T i18nKey="category">#</T>
100 <th class="text-right">
101 <T i18nKey="subscribers">#</T>
103 <th class="text-right d-none d-lg-table-cell">
104 <T i18nKey="posts">#</T>
106 <th class="text-right d-none d-lg-table-cell">
107 <T i18nKey="comments">#</T>
113 {this.state.communities.map(community => (
116 <Link to={`/c/${community.name}`}>
120 <td class="d-none d-lg-table-cell">{community.title}</td>
121 <td>{community.category_name}</td>
122 <td class="text-right">
123 {community.number_of_subscribers}
125 <td class="text-right d-none d-lg-table-cell">
126 {community.number_of_posts}
128 <td class="text-right d-none d-lg-table-cell">
129 {community.number_of_comments}
131 <td class="text-right">
132 {community.subscribed ? (
134 class="pointer btn-link"
137 this.handleUnsubscribe
140 <T i18nKey="unsubscribe">#</T>
144 class="pointer btn-link"
150 <T i18nKey="subscribe">#</T>
169 {this.state.page > 1 && (
171 class="btn btn-sm btn-secondary mr-1"
172 onClick={linkEvent(this, this.prevPage)}
174 <T i18nKey="prev">#</T>
178 class="btn btn-sm btn-secondary"
179 onClick={linkEvent(this, this.nextPage)}
181 <T i18nKey="next">#</T>
188 this.props.history.push(`/communities/page/${this.state.page}`);
191 nextPage(i: Communities) {
198 prevPage(i: Communities) {
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],
225 page: this.state.page,
228 WebSocketService.Instance.listCommunities(listCommunitiesForm);
231 parseMessage(msg: WebSocketJsonResponse) {
233 let res = wsJsonToRes(msg);
235 alert(i18n.t(res.error));
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);