]> Untitled Git - lemmy.git/blob - ui/src/components/communities.tsx
Adding subscribe to communities.
[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, Post as PostI, GetPostResponse, PostResponse, Comment, CommentForm as CommentFormI, CommentResponse, CommentLikeForm, CommentSortType, CreatePostLikeResponse, ListCommunitiesResponse, CommunityResponse, FollowCommunityForm } from '../interfaces';
6 import { WebSocketService, UserService } from '../services';
7 import { msgOp, hotRank,mdToHtml } from '../utils';
8
9 declare const Sortable: any;
10
11 interface CommunitiesState {
12   communities: Array<Community>;
13 }
14
15 export class Communities extends Component<any, CommunitiesState> {
16   private subscription: Subscription;
17   private emptyState: CommunitiesState = {
18     communities: []
19   }
20
21   constructor(props, context) {
22     super(props, context);
23     this.state = this.emptyState;
24     this.subscription = WebSocketService.Instance.subject
25       .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
26       .subscribe(
27         (msg) => this.parseMessage(msg),
28         (err) => console.error(err),
29         () => console.log('complete')
30       );
31     WebSocketService.Instance.listCommunities();
32
33   }
34
35   componentDidMount() {
36     let table = document.querySelector('#community_table');
37     Sortable.initTable(table);
38   }
39
40   render() {
41     return (
42       <div class="container-fluid">
43         <h4>Communities</h4>
44         <div class="table-responsive">
45           <table id="community_table" class="table table-sm table-hover" data-sortable>
46             <thead>
47               <tr>
48                 <th>Name</th>
49                 <th>Title</th>
50                 <th>Category</th>
51                 <th class="text-right">Subscribers</th>
52                 <th class="text-right">Posts</th>
53                 <th class="text-right">Comments</th>
54                 <th></th>
55               </tr>
56             </thead>
57             <tbody>
58               {this.state.communities.map(community =>
59                 <tr>
60                   <td><Link to={`/community/${community.id}`}>{community.name}</Link></td>
61                   <td>{community.title}</td>
62                   <td>{community.category_name}</td>
63                   <td class="text-right">{community.number_of_subscribers}</td>
64                   <td class="text-right">{community.number_of_posts}</td>
65                   <td class="text-right">{community.number_of_comments}</td>
66                   <td class="text-right">
67                     {community.subscribed 
68                       ? <button class="btn btn-sm btn-secondary" onClick={linkEvent(community.id, this.handleUnsubscribe)}>Unsubscribe</button>
69                       : <button class="btn btn-sm btn-secondary" onClick={linkEvent(community.id, this.handleSubscribe)}>Subscribe</button>
70                     }
71                   </td>
72                 </tr>
73               )}
74             </tbody>
75           </table>
76         </div>
77       </div>
78     );
79   }
80
81   handleUnsubscribe(communityId: number) {
82     let form: FollowCommunityForm = {
83       community_id: communityId,
84       follow: false
85     };
86     WebSocketService.Instance.followCommunity(form);
87   }
88
89
90   handleSubscribe(communityId: number) {
91     let form: FollowCommunityForm = {
92       community_id: communityId,
93       follow: true
94     };
95     WebSocketService.Instance.followCommunity(form);
96   }
97
98   parseMessage(msg: any) {
99     console.log(msg);
100     let op: UserOperation = msgOp(msg);
101     if (msg.error) {
102       alert(msg.error);
103       return;
104     } else if (op == UserOperation.ListCommunities) {
105       let res: ListCommunitiesResponse = msg;
106       this.state.communities = res.communities;
107       this.state.communities.sort((a, b) => b.number_of_subscribers - a.number_of_subscribers);
108       this.setState(this.state);
109     } else if (op == UserOperation.FollowCommunity) {
110       let res: CommunityResponse = msg;
111       let found = this.state.communities.find(c => c.id == res.community.id);
112       found.subscribed = res.community.subscribed;
113       found.number_of_subscribers = res.community.number_of_subscribers;
114       this.setState(this.state);
115     }
116   }
117 }