1 import { Component, linkEvent } from 'inferno';
2 import { Helmet } from 'inferno-helmet';
3 import { Subscription } from 'rxjs';
4 import { retryWhen, delay, take } from 'rxjs/operators';
8 ListCommunitiesResponse,
13 WebSocketJsonResponse,
16 } from '../interfaces';
17 import { WebSocketService } from '../services';
18 import { wsJsonToRes, toast, getPageFromProps } from '../utils';
19 import { CommunityLink } from './community-link';
20 import { i18n } from '../i18next';
22 declare const Sortable: any;
24 const communityLimit = 100;
26 interface CommunitiesState {
27 communities: Array<Community>;
33 interface CommunitiesProps {
37 export class Communities extends Component<any, CommunitiesState> {
38 private subscription: Subscription;
39 private emptyState: CommunitiesState = {
42 page: getPageFromProps(this.props),
46 constructor(props: any, context: any) {
47 super(props, context);
48 this.state = this.emptyState;
49 this.subscription = WebSocketService.Instance.subject
50 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
52 msg => this.parseMessage(msg),
53 err => console.error(err),
54 () => console.log('complete')
58 WebSocketService.Instance.getSite();
61 componentWillUnmount() {
62 this.subscription.unsubscribe();
65 static getDerivedStateFromProps(props: any): CommunitiesProps {
67 page: getPageFromProps(props),
71 componentDidUpdate(_: any, lastState: CommunitiesState) {
72 if (lastState.page !== this.state.page) {
73 this.setState({ loading: true });
78 get documentTitle(): string {
79 if (this.state.site) {
80 return `${i18n.t('communities')} - ${this.state.site.name}`;
88 <div class="container">
89 <Helmet title={this.documentTitle} />
90 {this.state.loading ? (
92 <svg class="icon icon-spinner spin">
93 <use xlinkHref="#icon-spinner"></use>
98 <h5>{i18n.t('list_of_communities')}</h5>
99 <div class="table-responsive">
100 <table id="community_table" class="table table-sm table-hover">
101 <thead class="pointer">
103 <th>{i18n.t('name')}</th>
104 <th>{i18n.t('category')}</th>
105 <th class="text-right">{i18n.t('subscribers')}</th>
106 <th class="text-right d-none d-lg-table-cell">
109 <th class="text-right d-none d-lg-table-cell">
116 {this.state.communities.map(community => (
119 <CommunityLink community={community} />
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 {i18n.t('unsubscribe')}
144 class="pointer btn-link"
150 {i18n.t('subscribe')}
169 {this.state.page > 1 && (
171 class="btn btn-secondary mr-1"
172 onClick={linkEvent(this, this.prevPage)}
178 {this.state.communities.length > 0 && (
180 class="btn btn-secondary"
181 onClick={linkEvent(this, this.nextPage)}
190 updateUrl(paramUpdates: CommunitiesProps) {
191 const page = paramUpdates.page || this.state.page;
192 this.props.history.push(`/communities/page/${page}`);
195 nextPage(i: Communities) {
196 i.updateUrl({ page: i.state.page + 1 });
199 prevPage(i: Communities) {
200 i.updateUrl({ page: i.state.page - 1 });
203 handleUnsubscribe(communityId: number) {
204 let form: FollowCommunityForm = {
205 community_id: communityId,
208 WebSocketService.Instance.followCommunity(form);
211 handleSubscribe(communityId: number) {
212 let form: FollowCommunityForm = {
213 community_id: communityId,
216 WebSocketService.Instance.followCommunity(form);
220 let listCommunitiesForm: ListCommunitiesForm = {
221 sort: SortType[SortType.TopAll],
222 limit: communityLimit,
223 page: this.state.page,
226 WebSocketService.Instance.listCommunities(listCommunitiesForm);
229 parseMessage(msg: WebSocketJsonResponse) {
231 let res = wsJsonToRes(msg);
233 toast(i18n.t(msg.error), 'danger');
235 } else if (res.op == UserOperation.ListCommunities) {
236 let data = res.data as ListCommunitiesResponse;
237 this.state.communities = data.communities;
238 this.state.communities.sort(
239 (a, b) => b.number_of_subscribers - a.number_of_subscribers
241 this.state.loading = false;
242 window.scrollTo(0, 0);
243 this.setState(this.state);
244 let table = document.querySelector('#community_table');
245 Sortable.initTable(table);
246 } else if (res.op == UserOperation.FollowCommunity) {
247 let data = res.data as CommunityResponse;
248 let found = this.state.communities.find(c => c.id == data.community.id);
249 found.subscribed = data.community.subscribed;
250 found.number_of_subscribers = data.community.number_of_subscribers;
251 this.setState(this.state);
252 } else if (res.op == UserOperation.GetSite) {
253 let data = res.data as GetSiteResponse;
254 this.state.site = data.site;
255 this.setState(this.state);