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>;
17 export class Communities extends Component<any, CommunitiesState> {
18 private subscription: Subscription;
19 private emptyState: CommunitiesState = {
22 page: this.getPageFromProps(this.props),
25 constructor(props: any, context: any) {
26 super(props, context);
27 this.state = this.emptyState;
28 this.subscription = WebSocketService.Instance.subject
29 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
31 (msg) => this.parseMessage(msg),
32 (err) => console.error(err),
33 () => console.log('complete')
40 getPageFromProps(props: any): number {
41 return (props.match.params.page) ? Number(props.match.params.page) : 1;
44 componentWillUnmount() {
45 this.subscription.unsubscribe();
49 document.title = "Communities - Lemmy";
50 let table = document.querySelector('#community_table');
51 Sortable.initTable(table);
54 // Necessary for back button for some reason
55 componentWillReceiveProps(nextProps: any) {
56 if (nextProps.history.action == 'POP') {
57 this.state = this.emptyState;
58 this.state.page = this.getPageFromProps(nextProps);
65 <div class="container">
67 <h5 class=""><svg class="icon icon-spinner spin"><use xlinkHref="#icon-spinner"></use></svg></h5> :
70 <div class="table-responsive">
71 <table id="community_table" class="table table-sm table-hover">
72 <thead class="pointer">
77 <th class="text-right d-none d-md-table-cell">Subscribers</th>
78 <th class="text-right d-none d-md-table-cell">Posts</th>
79 <th class="text-right d-none d-md-table-cell">Comments</th>
84 {this.state.communities.map(community =>
86 <td><Link to={`/c/${community.name}`}>{community.name}</Link></td>
87 <td>{community.title}</td>
88 <td>{community.category_name}</td>
89 <td class="text-right d-none d-md-table-cell">{community.number_of_subscribers}</td>
90 <td class="text-right d-none d-md-table-cell">{community.number_of_posts}</td>
91 <td class="text-right d-none d-md-table-cell">{community.number_of_comments}</td>
92 <td class="text-right">
93 {community.subscribed ?
94 <span class="pointer btn-link" onClick={linkEvent(community.id, this.handleUnsubscribe)}>Unsubscribe</span> :
95 <span class="pointer btn-link" onClick={linkEvent(community.id, this.handleSubscribe)}>Subscribe</span>
113 {this.state.page > 1 &&
114 <button class="btn btn-sm btn-secondary mr-1" onClick={linkEvent(this, this.prevPage)}>Prev</button>
116 <button class="btn btn-sm btn-secondary" onClick={linkEvent(this, this.nextPage)}>Next</button>
122 this.props.history.push(`/communities/page/${this.state.page}`);
125 nextPage(i: Communities) {
132 prevPage(i: Communities) {
139 handleUnsubscribe(communityId: number) {
140 let form: FollowCommunityForm = {
141 community_id: communityId,
144 WebSocketService.Instance.followCommunity(form);
147 handleSubscribe(communityId: number) {
148 let form: FollowCommunityForm = {
149 community_id: communityId,
152 WebSocketService.Instance.followCommunity(form);
156 let listCommunitiesForm: ListCommunitiesForm = {
157 sort: SortType[SortType.TopAll],
159 page: this.state.page,
162 WebSocketService.Instance.listCommunities(listCommunitiesForm);
166 parseMessage(msg: any) {
168 let op: UserOperation = msgOp(msg);
172 } else if (op == UserOperation.ListCommunities) {
173 let res: ListCommunitiesResponse = msg;
174 this.state.communities = res.communities;
175 this.state.communities.sort((a, b) => b.number_of_subscribers - a.number_of_subscribers);
176 this.state.loading = false;
177 this.setState(this.state);
178 } else if (op == UserOperation.FollowCommunity) {
179 let res: CommunityResponse = msg;
180 let found = this.state.communities.find(c => c.id == res.community.id);
181 found.subscribed = res.community.subscribed;
182 found.number_of_subscribers = res.community.number_of_subscribers;
183 this.setState(this.state);