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, Post as PostI, GetPostResponse, PostResponse, Comment, CommentForm as CommentFormI, CommentResponse, CommentLikeForm, CommentSortType, CreatePostLikeResponse, ListCommunitiesResponse } from '../interfaces';
6 import { WebSocketService, UserService } from '../services';
7 import { msgOp, hotRank,mdToHtml } 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, context) {
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 let table = document.querySelector('#community_table');
36 Sortable.initTable(table);
41 <div class="container-fluid">
43 <div class="table-responsive">
44 <table id="community_table" class="table table-sm table-hover" data-sortable>
50 <th class="text-right">Subscribers</th>
51 <th class="text-right">Posts</th>
52 <th class="text-right">Comments</th>
56 {this.state.communities.map(community =>
58 <td><Link to={`/community/${community.id}`}>{community.name}</Link></td>
59 <td>{community.title}</td>
60 <td>{community.category_name}</td>
61 <td class="text-right">{community.number_of_subscribers}</td>
62 <td class="text-right">{community.number_of_posts}</td>
63 <td class="text-right">{community.number_of_comments}</td>
75 parseMessage(msg: any) {
77 let op: UserOperation = msgOp(msg);
81 } else if (op == UserOperation.ListCommunities) {
82 let res: ListCommunitiesResponse = msg;
83 this.state.communities = res.communities;
84 this.state.communities.sort((a, b) => b.number_of_subscribers - a.number_of_subscribers);
85 this.setState(this.state);