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 { msgOp } 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
48 msg => this.parseMessage(msg),
49 err => console.error(err),
50 () => console.log('complete')
56 getPageFromProps(props: any): number {
57 return props.match.params.page ? Number(props.match.params.page) : 1;
60 componentWillUnmount() {
61 this.subscription.unsubscribe();
65 document.title = `${i18n.t('communities')} - ${
66 WebSocketService.Instance.site.name
70 // Necessary for back button for some reason
71 componentWillReceiveProps(nextProps: any) {
72 if (nextProps.history.action == 'POP') {
73 this.state = this.emptyState;
74 this.state.page = this.getPageFromProps(nextProps);
81 <div class="container">
82 {this.state.loading ? (
84 <svg class="icon icon-spinner spin">
85 <use xlinkHref="#icon-spinner"></use>
91 <T i18nKey="list_of_communities">#</T>
93 <div class="table-responsive">
94 <table id="community_table" class="table table-sm table-hover">
95 <thead class="pointer">
98 <T i18nKey="name">#</T>
100 <th class="d-none d-lg-table-cell">
101 <T i18nKey="title">#</T>
104 <T i18nKey="category">#</T>
106 <th class="text-right">
107 <T i18nKey="subscribers">#</T>
109 <th class="text-right d-none d-lg-table-cell">
110 <T i18nKey="posts">#</T>
112 <th class="text-right d-none d-lg-table-cell">
113 <T i18nKey="comments">#</T>
119 {this.state.communities.map(community => (
122 <Link to={`/c/${community.name}`}>
126 <td class="d-none d-lg-table-cell">{community.title}</td>
127 <td>{community.category_name}</td>
128 <td class="text-right">
129 {community.number_of_subscribers}
131 <td class="text-right d-none d-lg-table-cell">
132 {community.number_of_posts}
134 <td class="text-right d-none d-lg-table-cell">
135 {community.number_of_comments}
137 <td class="text-right">
138 {community.subscribed ? (
140 class="pointer btn-link"
143 this.handleUnsubscribe
146 <T i18nKey="unsubscribe">#</T>
150 class="pointer btn-link"
156 <T i18nKey="subscribe">#</T>
175 {this.state.page > 1 && (
177 class="btn btn-sm btn-secondary mr-1"
178 onClick={linkEvent(this, this.prevPage)}
180 <T i18nKey="prev">#</T>
184 class="btn btn-sm btn-secondary"
185 onClick={linkEvent(this, this.nextPage)}
187 <T i18nKey="next">#</T>
194 this.props.history.push(`/communities/page/${this.state.page}`);
197 nextPage(i: Communities) {
204 prevPage(i: Communities) {
211 handleUnsubscribe(communityId: number) {
212 let form: FollowCommunityForm = {
213 community_id: communityId,
216 WebSocketService.Instance.followCommunity(form);
219 handleSubscribe(communityId: number) {
220 let form: FollowCommunityForm = {
221 community_id: communityId,
224 WebSocketService.Instance.followCommunity(form);
228 let listCommunitiesForm: ListCommunitiesForm = {
229 sort: SortType[SortType.TopAll],
231 page: this.state.page,
234 WebSocketService.Instance.listCommunities(listCommunitiesForm);
237 parseMessage(msg: any) {
239 let op: UserOperation = msgOp(msg);
241 alert(i18n.t(msg.error));
243 } else if (op == UserOperation.ListCommunities) {
244 let res: ListCommunitiesResponse = msg;
245 this.state.communities = res.communities;
246 this.state.communities.sort(
247 (a, b) => b.number_of_subscribers - a.number_of_subscribers
249 this.state.loading = false;
250 window.scrollTo(0, 0);
251 this.setState(this.state);
252 let table = document.querySelector('#community_table');
253 Sortable.initTable(table);
254 } else if (op == UserOperation.FollowCommunity) {
255 let res: CommunityResponse = msg;
256 let found = this.state.communities.find(c => c.id == res.community.id);
257 found.subscribed = res.community.subscribed;
258 found.number_of_subscribers = res.community.number_of_subscribers;
259 this.setState(this.state);