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';
5 import { UserOperation, Community, ListCommunitiesResponse, CommunityResponse, FollowCommunityForm } from '../interfaces';
6 import { WebSocketService } from '../services';
7 import { msgOp } from '../utils';
9 declare const Sortable: any;
11 interface CommunitiesState {
12 communities: Array<Community>;
15 export class Communities extends Component<any, CommunitiesState> {
16 private subscription: Subscription;
17 private emptyState: CommunitiesState = {
21 constructor(props: any, context: any) {
22 super(props, context);
23 this.state = this.emptyState;
24 this.subscription = WebSocketService.Instance.subject
25 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
27 (msg) => this.parseMessage(msg),
28 (err) => console.error(err),
29 () => console.log('complete')
31 WebSocketService.Instance.listCommunities();
35 componentWillUnmount() {
36 this.subscription.unsubscribe();
40 let table = document.querySelector('#community_table');
41 Sortable.initTable(table);
46 <div class="container-fluid">
48 <div class="table-responsive">
49 <table id="community_table" class="table table-sm table-hover">
50 <thead class="pointer">
55 <th class="text-right">Subscribers</th>
56 <th class="text-right">Posts</th>
57 <th class="text-right">Comments</th>
62 {this.state.communities.map(community =>
64 <td><Link to={`/community/${community.id}`}>{community.name}</Link></td>
65 <td>{community.title}</td>
66 <td>{community.category_name}</td>
67 <td class="text-right">{community.number_of_subscribers}</td>
68 <td class="text-right">{community.number_of_posts}</td>
69 <td class="text-right">{community.number_of_comments}</td>
70 <td class="text-right">
72 ? <button class="btn btn-sm btn-secondary" onClick={linkEvent(community.id, this.handleUnsubscribe)}>Unsubscribe</button>
73 : <button class="btn btn-sm btn-secondary" onClick={linkEvent(community.id, this.handleSubscribe)}>Subscribe</button>
85 handleUnsubscribe(communityId: number) {
86 let form: FollowCommunityForm = {
87 community_id: communityId,
90 WebSocketService.Instance.followCommunity(form);
94 handleSubscribe(communityId: number) {
95 let form: FollowCommunityForm = {
96 community_id: communityId,
99 WebSocketService.Instance.followCommunity(form);
102 parseMessage(msg: any) {
104 let op: UserOperation = msgOp(msg);
108 } else if (op == UserOperation.ListCommunities) {
109 let res: ListCommunitiesResponse = msg;
110 this.state.communities = res.communities;
111 this.state.communities.sort((a, b) => b.number_of_subscribers - a.number_of_subscribers);
112 this.setState(this.state);
113 } else if (op == UserOperation.FollowCommunity) {
114 let res: CommunityResponse = msg;
115 let found = this.state.communities.find(c => c.id == res.community.id);
116 found.subscribed = res.community.subscribed;
117 found.number_of_subscribers = res.community.number_of_subscribers;
118 this.setState(this.state);