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, toast } from '../utils';
16 import { i18n } from '../i18next';
17 import { T } from 'inferno-i18next';
19 declare const Sortable: any;
21 const communityLimit = 100;
23 interface CommunitiesState {
24 communities: Array<Community>;
29 export class Communities extends Component<any, CommunitiesState> {
30 private subscription: Subscription;
31 private emptyState: CommunitiesState = {
34 page: this.getPageFromProps(this.props),
37 constructor(props: any, context: any) {
38 super(props, context);
39 this.state = this.emptyState;
40 this.subscription = WebSocketService.Instance.subject
41 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
43 msg => this.parseMessage(msg),
44 err => console.error(err),
45 () => console.log('complete')
51 getPageFromProps(props: any): number {
52 return props.match.params.page ? Number(props.match.params.page) : 1;
55 componentWillUnmount() {
56 this.subscription.unsubscribe();
60 document.title = `${i18n.t('communities')} - ${
61 WebSocketService.Instance.site.name
65 // Necessary for back button for some reason
66 componentWillReceiveProps(nextProps: any) {
67 if (nextProps.history.action == 'POP') {
68 this.state = this.emptyState;
69 this.state.page = this.getPageFromProps(nextProps);
76 <div class="container">
77 {this.state.loading ? (
79 <svg class="icon icon-spinner spin">
80 <use xlinkHref="#icon-spinner"></use>
86 <T i18nKey="list_of_communities">#</T>
88 <div class="table-responsive">
89 <table id="community_table" class="table table-sm table-hover">
90 <thead class="pointer">
93 <T i18nKey="name">#</T>
95 <th class="d-none d-lg-table-cell">
96 <T i18nKey="title">#</T>
99 <T i18nKey="category">#</T>
101 <th class="text-right">
102 <T i18nKey="subscribers">#</T>
104 <th class="text-right d-none d-lg-table-cell">
105 <T i18nKey="posts">#</T>
107 <th class="text-right d-none d-lg-table-cell">
108 <T i18nKey="comments">#</T>
114 {this.state.communities.map(community => (
117 <Link to={`/c/${community.name}`}>
121 <td class="d-none d-lg-table-cell">{community.title}</td>
122 <td>{community.category_name}</td>
123 <td class="text-right">
124 {community.number_of_subscribers}
126 <td class="text-right d-none d-lg-table-cell">
127 {community.number_of_posts}
129 <td class="text-right d-none d-lg-table-cell">
130 {community.number_of_comments}
132 <td class="text-right">
133 {community.subscribed ? (
135 class="pointer btn-link"
138 this.handleUnsubscribe
141 <T i18nKey="unsubscribe">#</T>
145 class="pointer btn-link"
151 <T i18nKey="subscribe">#</T>
170 {this.state.page > 1 && (
172 class="btn btn-sm btn-secondary mr-1"
173 onClick={linkEvent(this, this.prevPage)}
175 <T i18nKey="prev">#</T>
178 {this.state.communities.length == communityLimit && (
180 class="btn btn-sm btn-secondary"
181 onClick={linkEvent(this, this.nextPage)}
183 <T i18nKey="next">#</T>
191 this.props.history.push(`/communities/page/${this.state.page}`);
194 nextPage(i: Communities) {
201 prevPage(i: Communities) {
208 handleUnsubscribe(communityId: number) {
209 let form: FollowCommunityForm = {
210 community_id: communityId,
213 WebSocketService.Instance.followCommunity(form);
216 handleSubscribe(communityId: number) {
217 let form: FollowCommunityForm = {
218 community_id: communityId,
221 WebSocketService.Instance.followCommunity(form);
225 let listCommunitiesForm: ListCommunitiesForm = {
226 sort: SortType[SortType.TopAll],
227 limit: communityLimit,
228 page: this.state.page,
231 WebSocketService.Instance.listCommunities(listCommunitiesForm);
234 parseMessage(msg: any) {
236 let op: UserOperation = msgOp(msg);
238 toast(i18n.t(msg.error), 'danger');
240 } else if (op == UserOperation.ListCommunities) {
241 let res: ListCommunitiesResponse = msg;
242 this.state.communities = res.communities;
243 this.state.communities.sort(
244 (a, b) => b.number_of_subscribers - a.number_of_subscribers
246 this.state.loading = false;
247 window.scrollTo(0, 0);
248 this.setState(this.state);
249 let table = document.querySelector('#community_table');
250 Sortable.initTable(table);
251 } else if (op == UserOperation.FollowCommunity) {
252 let res: CommunityResponse = msg;
253 let found = this.state.communities.find(c => c.id == res.community.id);
254 found.subscribed = res.community.subscribed;
255 found.number_of_subscribers = res.community.number_of_subscribers;
256 this.setState(this.state);