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, ListCommunitiesForm, SortType } from '../interfaces';
6 import { WebSocketService } from '../services';
7 import { msgOp } from '../utils';
9 declare const Sortable: any;
11 interface CommunitiesState {
12 communities: Array<Community>;
16 export class Communities extends Component<any, CommunitiesState> {
17 private subscription: Subscription;
18 private emptyState: CommunitiesState = {
23 constructor(props: any, context: any) {
24 super(props, context);
25 this.state = this.emptyState;
26 this.subscription = WebSocketService.Instance.subject
27 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
29 (msg) => this.parseMessage(msg),
30 (err) => console.error(err),
31 () => console.log('complete')
34 let listCommunitiesForm: ListCommunitiesForm = {
35 sort: SortType[SortType.TopAll]
38 WebSocketService.Instance.listCommunities(listCommunitiesForm);
42 componentWillUnmount() {
43 this.subscription.unsubscribe();
47 let table = document.querySelector('#community_table');
48 Sortable.initTable(table);
53 <div class="container">
55 <h4 class=""><svg class="icon icon-spinner spin"><use xlinkHref="#icon-spinner"></use></svg></h4> :
58 <div class="table-responsive">
59 <table id="community_table" class="table table-sm table-hover">
60 <thead class="pointer">
65 <th class="text-right">Subscribers</th>
66 <th class="text-right">Posts</th>
67 <th class="text-right">Comments</th>
72 {this.state.communities.map(community =>
74 <td><Link to={`/community/${community.id}`}>{community.name}</Link></td>
75 <td>{community.title}</td>
76 <td>{community.category_name}</td>
77 <td class="text-right">{community.number_of_subscribers}</td>
78 <td class="text-right">{community.number_of_posts}</td>
79 <td class="text-right">{community.number_of_comments}</td>
80 <td class="text-right">
81 {community.subscribed ?
82 <button class="btn btn-sm btn-secondary" onClick={linkEvent(community.id, this.handleUnsubscribe)}>Unsubscribe</button> :
83 <button class="btn btn-sm btn-secondary" onClick={linkEvent(community.id, this.handleSubscribe)}>Subscribe</button>
97 handleUnsubscribe(communityId: number) {
98 let form: FollowCommunityForm = {
99 community_id: communityId,
102 WebSocketService.Instance.followCommunity(form);
106 handleSubscribe(communityId: number) {
107 let form: FollowCommunityForm = {
108 community_id: communityId,
111 WebSocketService.Instance.followCommunity(form);
114 parseMessage(msg: any) {
116 let op: UserOperation = msgOp(msg);
120 } else if (op == UserOperation.ListCommunities) {
121 let res: ListCommunitiesResponse = msg;
122 this.state.communities = res.communities;
123 this.state.communities.sort((a, b) => b.number_of_subscribers - a.number_of_subscribers);
124 this.state.loading = false;
125 this.setState(this.state);
126 } else if (op == UserOperation.FollowCommunity) {
127 let res: CommunityResponse = msg;
128 let found = this.state.communities.find(c => c.id == res.community.id);
129 found.subscribed = res.community.subscribed;
130 found.number_of_subscribers = res.community.number_of_subscribers;
131 this.setState(this.state);