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";
52 // Necessary for back button for some reason
53 componentWillReceiveProps(nextProps: any) {
54 if (nextProps.history.action == 'POP') {
55 this.state = this.emptyState;
56 this.state.page = this.getPageFromProps(nextProps);
63 <div class="container">
65 <h5 class=""><svg class="icon icon-spinner spin"><use xlinkHref="#icon-spinner"></use></svg></h5> :
67 <h5>List of communities</h5>
68 <div class="table-responsive">
69 <table id="community_table" class="table table-sm table-hover">
70 <thead class="pointer">
73 <th class="d-none d-lg-table-cell">Title</th>
75 <th class="text-right">Subscribers</th>
76 <th class="text-right d-none d-lg-table-cell">Posts</th>
77 <th class="text-right d-none d-lg-table-cell">Comments</th>
82 {this.state.communities.map(community =>
84 <td><Link to={`/c/${community.name}`}>{community.name}</Link></td>
85 <td class="d-none d-lg-table-cell">{community.title}</td>
86 <td>{community.category_name}</td>
87 <td class="text-right">{community.number_of_subscribers}</td>
88 <td class="text-right d-none d-lg-table-cell">{community.number_of_posts}</td>
89 <td class="text-right d-none d-lg-table-cell">{community.number_of_comments}</td>
90 <td class="text-right">
91 {community.subscribed ?
92 <span class="pointer btn-link" onClick={linkEvent(community.id, this.handleUnsubscribe)}>Unsubscribe</span> :
93 <span class="pointer btn-link" onClick={linkEvent(community.id, this.handleSubscribe)}>Subscribe</span>
111 {this.state.page > 1 &&
112 <button class="btn btn-sm btn-secondary mr-1" onClick={linkEvent(this, this.prevPage)}>Prev</button>
114 <button class="btn btn-sm btn-secondary" onClick={linkEvent(this, this.nextPage)}>Next</button>
120 this.props.history.push(`/communities/page/${this.state.page}`);
123 nextPage(i: Communities) {
130 prevPage(i: Communities) {
137 handleUnsubscribe(communityId: number) {
138 let form: FollowCommunityForm = {
139 community_id: communityId,
142 WebSocketService.Instance.followCommunity(form);
145 handleSubscribe(communityId: number) {
146 let form: FollowCommunityForm = {
147 community_id: communityId,
150 WebSocketService.Instance.followCommunity(form);
154 let listCommunitiesForm: ListCommunitiesForm = {
155 sort: SortType[SortType.TopAll],
157 page: this.state.page,
160 WebSocketService.Instance.listCommunities(listCommunitiesForm);
164 parseMessage(msg: any) {
166 let op: UserOperation = msgOp(msg);
170 } else if (op == UserOperation.ListCommunities) {
171 let res: ListCommunitiesResponse = msg;
172 this.state.communities = res.communities;
173 this.state.communities.sort((a, b) => b.number_of_subscribers - a.number_of_subscribers);
174 this.state.loading = false;
175 window.scrollTo(0,0);
176 this.setState(this.state);
177 let table = document.querySelector('#community_table');
178 Sortable.initTable(table);
179 } else if (op == UserOperation.FollowCommunity) {
180 let res: CommunityResponse = msg;
181 let found = this.state.communities.find(c => c.id == res.community.id);
182 found.subscribed = res.community.subscribed;
183 found.number_of_subscribers = res.community.number_of_subscribers;
184 this.setState(this.state);