1 import { Component, linkEvent } from 'inferno';
2 import { Subscription } from 'rxjs';
3 import { retryWhen, delay, take } from 'rxjs/operators';
7 ListCommunitiesResponse,
12 WebSocketJsonResponse,
14 } from '../interfaces';
15 import { WebSocketService } from '../services';
16 import { wsJsonToRes, toast, getPageFromProps } from '../utils';
17 import { CommunityLink } from './community-link';
18 import { i18n } from '../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: 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')
50 WebSocketService.Instance.getSite();
53 componentWillUnmount() {
54 this.subscription.unsubscribe();
57 static getDerivedStateFromProps(props) {
59 page: getPageFromProps(props),
63 componentDidUpdate(_, lastState) {
64 if (lastState.page !== this.state.page) {
65 this.setState({ loading: true });
72 <div class="container">
73 {this.state.loading ? (
75 <svg class="icon icon-spinner spin">
76 <use xlinkHref="#icon-spinner"></use>
81 <h5>{i18n.t('list_of_communities')}</h5>
82 <div class="table-responsive">
83 <table id="community_table" class="table table-sm table-hover">
84 <thead class="pointer">
86 <th>{i18n.t('name')}</th>
87 <th class="d-none d-lg-table-cell">{i18n.t('title')}</th>
88 <th>{i18n.t('category')}</th>
89 <th class="text-right">{i18n.t('subscribers')}</th>
90 <th class="text-right d-none d-lg-table-cell">
93 <th class="text-right d-none d-lg-table-cell">
100 {this.state.communities.map(community => (
103 <CommunityLink community={community} />
105 <td class="d-none d-lg-table-cell">{community.title}</td>
106 <td>{community.category_name}</td>
107 <td class="text-right">
108 {community.number_of_subscribers}
110 <td class="text-right d-none d-lg-table-cell">
111 {community.number_of_posts}
113 <td class="text-right d-none d-lg-table-cell">
114 {community.number_of_comments}
116 <td class="text-right">
117 {community.subscribed ? (
119 class="pointer btn-link"
122 this.handleUnsubscribe
125 {i18n.t('unsubscribe')}
129 class="pointer btn-link"
135 {i18n.t('subscribe')}
154 {this.state.page > 1 && (
156 class="btn btn-sm btn-secondary mr-1"
157 onClick={linkEvent(this, this.prevPage)}
163 {this.state.communities.length > 0 && (
165 class="btn btn-sm btn-secondary"
166 onClick={linkEvent(this, this.nextPage)}
175 updateUrl(paramUpdates: { page: number }) {
176 const page = paramUpdates.page || this.state.page;
177 this.props.history.push(`/communities/page/${page}`);
180 nextPage(i: Communities) {
181 i.updateUrl({ page: i.state.page + 1 });
184 prevPage(i: Communities) {
185 i.updateUrl({ page: i.state.page - 1 });
188 handleUnsubscribe(communityId: number) {
189 let form: FollowCommunityForm = {
190 community_id: communityId,
193 WebSocketService.Instance.followCommunity(form);
196 handleSubscribe(communityId: number) {
197 let form: FollowCommunityForm = {
198 community_id: communityId,
201 WebSocketService.Instance.followCommunity(form);
205 let listCommunitiesForm: ListCommunitiesForm = {
206 sort: SortType[SortType.TopAll],
207 limit: communityLimit,
208 page: this.state.page,
211 WebSocketService.Instance.listCommunities(listCommunitiesForm);
214 parseMessage(msg: WebSocketJsonResponse) {
216 let res = wsJsonToRes(msg);
218 toast(i18n.t(msg.error), 'danger');
220 } else if (res.op == UserOperation.ListCommunities) {
221 let data = res.data as ListCommunitiesResponse;
222 this.state.communities = data.communities;
223 this.state.communities.sort(
224 (a, b) => b.number_of_subscribers - a.number_of_subscribers
226 this.state.loading = false;
227 window.scrollTo(0, 0);
228 this.setState(this.state);
229 let table = document.querySelector('#community_table');
230 Sortable.initTable(table);
231 } else if (res.op == UserOperation.FollowCommunity) {
232 let data = res.data as CommunityResponse;
233 let found = this.state.communities.find(c => c.id == data.community.id);
234 found.subscribed = data.community.subscribed;
235 found.number_of_subscribers = data.community.number_of_subscribers;
236 this.setState(this.state);
237 } else if (res.op == UserOperation.GetSite) {
238 let data = res.data as GetSiteResponse;
239 document.title = `${i18n.t('communities')} - ${data.site.name}`;