]> Untitled Git - lemmy.git/blob - ui/src/components/communities.tsx
Commiting before I lose everything. I'll do this properly in a merge
[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     }
37
38     WebSocketService.Instance.listCommunities(listCommunitiesForm);
39
40   }
41
42   componentWillUnmount() {
43     this.subscription.unsubscribe();
44   }
45
46   componentDidMount() {
47     let table = document.querySelector('#community_table');
48     Sortable.initTable(table);
49   }
50
51   render() {
52     return (
53       <div class="container">
54         {this.state.loading ? 
55         <h4 class=""><svg class="icon icon-spinner spin"><use xlinkHref="#icon-spinner"></use></svg></h4> : 
56         <div>
57           <h4>Communities</h4>
58           <div class="table-responsive">
59             <table id="community_table" class="table table-sm table-hover">
60               <thead class="pointer">
61                 <tr>
62                   <th>Name</th>
63                   <th>Title</th>
64                   <th>Category</th>
65                   <th class="text-right">Subscribers</th>
66                   <th class="text-right">Posts</th>
67                   <th class="text-right">Comments</th>
68                   <th></th>
69                 </tr>
70               </thead>
71               <tbody>
72                 {this.state.communities.map(community =>
73                   <tr>
74                     <td><Link to={`/community/${community.id}`}>{community.name}</Link></td>
75                     <td>{community.title}</td>
76                     <td>{community.category_name}</td>
77                     <td class="text-right">{community.number_of_subscribers}</td>
78                     <td class="text-right">{community.number_of_posts}</td>
79                     <td class="text-right">{community.number_of_comments}</td>
80                     <td class="text-right">
81                       {community.subscribed ? 
82                       <button class="btn btn-sm btn-secondary" onClick={linkEvent(community.id, this.handleUnsubscribe)}>Unsubscribe</button> : 
83                       <button class="btn btn-sm btn-secondary" onClick={linkEvent(community.id, this.handleSubscribe)}>Subscribe</button>
84                       }
85                     </td>
86                   </tr>
87                 )}
88               </tbody>
89             </table>
90           </div>
91         </div>
92         }
93       </div>
94     );
95   }
96
97   handleUnsubscribe(communityId: number) {
98     let form: FollowCommunityForm = {
99       community_id: communityId,
100       follow: false
101     };
102     WebSocketService.Instance.followCommunity(form);
103   }
104
105   handleSubscribe(communityId: number) {
106     let form: FollowCommunityForm = {
107       community_id: communityId,
108       follow: true
109     };
110     WebSocketService.Instance.followCommunity(form);
111   }
112
113   parseMessage(msg: any) {
114     console.log(msg);
115     let op: UserOperation = msgOp(msg);
116     if (msg.error) {
117       alert(msg.error);
118       return;
119     } else if (op == UserOperation.ListCommunities) {
120       let res: ListCommunitiesResponse = msg;
121       this.state.communities = res.communities;
122       this.state.communities.sort((a, b) => b.number_of_subscribers - a.number_of_subscribers);
123       this.state.loading = false;
124       this.setState(this.state);
125     } else if (op == UserOperation.FollowCommunity) {
126       let res: CommunityResponse = msg;
127       let found = this.state.communities.find(c => c.id == res.community.id);
128       found.subscribed = res.community.subscribed;
129       found.number_of_subscribers = res.community.number_of_subscribers;
130       this.setState(this.state);
131     } 
132   }
133 }