]> Untitled Git - lemmy.git/blob - ui/src/components/communities.tsx
Merge branch 'replies' into dev
[lemmy.git] / ui / src / components / communities.tsx
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';
8
9 declare const Sortable: any;
10
11 interface CommunitiesState {
12   communities: Array<Community>;
13   loading: boolean;
14 }
15
16 export class Communities extends Component<any, CommunitiesState> {
17   private subscription: Subscription;
18   private emptyState: CommunitiesState = {
19     communities: [],
20     loading: true
21   }
22
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))))
28     .subscribe(
29       (msg) => this.parseMessage(msg),
30         (err) => console.error(err),
31         () => console.log('complete')
32     );
33
34     let listCommunitiesForm: ListCommunitiesForm = {
35       sort: SortType[SortType.TopAll],
36       limit: 9999,
37     }
38
39     WebSocketService.Instance.listCommunities(listCommunitiesForm);
40
41   }
42
43   componentWillUnmount() {
44     this.subscription.unsubscribe();
45   }
46
47   componentDidMount() {
48     let table = document.querySelector('#community_table');
49     Sortable.initTable(table);
50   }
51
52   render() {
53     return (
54       <div class="container">
55         {this.state.loading ? 
56         <h5 class=""><svg class="icon icon-spinner spin"><use xlinkHref="#icon-spinner"></use></svg></h5> : 
57         <div>
58           <h5>Communities</h5>
59           <div class="table-responsive">
60             <table id="community_table" class="table table-sm table-hover">
61               <thead class="pointer">
62                 <tr>
63                   <th>Name</th>
64                   <th>Title</th>
65                   <th>Category</th>
66                   <th class="text-right d-none d-md-table-cell">Subscribers</th>
67                   <th class="text-right d-none d-md-table-cell">Posts</th>
68                   <th class="text-right d-none d-md-table-cell">Comments</th>
69                   <th></th>
70                 </tr>
71               </thead>
72               <tbody>
73                 {this.state.communities.map(community =>
74                   <tr>
75                     <td><Link to={`/community/${community.id}`}>{community.name}</Link></td>
76                     <td>{community.title}</td>
77                     <td>{community.category_name}</td>
78                     <td class="text-right d-none d-md-table-cell">{community.number_of_subscribers}</td>
79                     <td class="text-right d-none d-md-table-cell">{community.number_of_posts}</td>
80                     <td class="text-right d-none d-md-table-cell">{community.number_of_comments}</td>
81                     <td class="text-right">
82                       {community.subscribed ? 
83                       <span class="pointer btn-link" onClick={linkEvent(community.id, this.handleUnsubscribe)}>Unsubscribe</span> : 
84                       <span class="pointer btn-link" onClick={linkEvent(community.id, this.handleSubscribe)}>Subscribe</span>
85                       }
86                     </td>
87                   </tr>
88                 )}
89               </tbody>
90             </table>
91           </div>
92         </div>
93         }
94       </div>
95     );
96   }
97
98   handleUnsubscribe(communityId: number) {
99     let form: FollowCommunityForm = {
100       community_id: communityId,
101       follow: false
102     };
103     WebSocketService.Instance.followCommunity(form);
104   }
105
106   handleSubscribe(communityId: number) {
107     let form: FollowCommunityForm = {
108       community_id: communityId,
109       follow: true
110     };
111     WebSocketService.Instance.followCommunity(form);
112   }
113
114   parseMessage(msg: any) {
115     console.log(msg);
116     let op: UserOperation = msgOp(msg);
117     if (msg.error) {
118       alert(msg.error);
119       return;
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);
132     } 
133   }
134 }