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],
39 WebSocketService.Instance.listCommunities(listCommunitiesForm);
43 componentWillUnmount() {
44 this.subscription.unsubscribe();
48 document.title = "Forums - Lemmy";
49 let table = document.querySelector('#community_table');
50 Sortable.initTable(table);
55 <div class="container">
57 <h5 class=""><svg class="icon icon-spinner spin"><use xlinkHref="#icon-spinner"></use></svg></h5> :
60 <div class="table-responsive">
61 <table id="community_table" class="table table-sm table-hover">
62 <thead class="pointer">
67 <th class="text-right d-none d-md-table-cell">Subscribers</th>
68 <th class="text-right d-none d-md-table-cell">Posts</th>
69 <th class="text-right d-none d-md-table-cell">Comments</th>
74 {this.state.communities.map(community =>
76 <td><Link to={`/community/${community.id}`}>{community.name}</Link></td>
77 <td>{community.title}</td>
78 <td>{community.category_name}</td>
79 <td class="text-right d-none d-md-table-cell">{community.number_of_subscribers}</td>
80 <td class="text-right d-none d-md-table-cell">{community.number_of_posts}</td>
81 <td class="text-right d-none d-md-table-cell">{community.number_of_comments}</td>
82 <td class="text-right">
83 {community.subscribed ?
84 <span class="pointer btn-link" onClick={linkEvent(community.id, this.handleUnsubscribe)}>Unsubscribe</span> :
85 <span class="pointer btn-link" onClick={linkEvent(community.id, this.handleSubscribe)}>Subscribe</span>
99 handleUnsubscribe(communityId: number) {
100 let form: FollowCommunityForm = {
101 community_id: communityId,
104 WebSocketService.Instance.followCommunity(form);
107 handleSubscribe(communityId: number) {
108 let form: FollowCommunityForm = {
109 community_id: communityId,
112 WebSocketService.Instance.followCommunity(form);
115 parseMessage(msg: any) {
117 let op: UserOperation = msgOp(msg);
121 } else if (op == UserOperation.ListCommunities) {
122 let res: ListCommunitiesResponse = msg;
123 this.state.communities = res.communities;
124 this.state.communities.sort((a, b) => b.number_of_subscribers - a.number_of_subscribers);
125 this.state.loading = false;
126 this.setState(this.state);
127 } else if (op == UserOperation.FollowCommunity) {
128 let res: CommunityResponse = msg;
129 let found = this.state.communities.find(c => c.id == res.community.id);
130 found.subscribed = res.community.subscribed;
131 found.number_of_subscribers = res.community.number_of_subscribers;
132 this.setState(this.state);