]> Untitled Git - lemmy.git/blob - ui/src/components/communities.tsx
Adding user details / overview page.
[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 } 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 }
14
15 export class Communities extends Component<any, CommunitiesState> {
16   private subscription: Subscription;
17   private emptyState: CommunitiesState = {
18     communities: []
19   }
20
21   constructor(props: any, context: any) {
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   componentWillUnmount() {
36     this.subscription.unsubscribe();
37   }
38
39   componentDidMount() {
40     let table = document.querySelector('#community_table');
41     Sortable.initTable(table);
42   }
43
44   render() {
45     return (
46       <div class="container-fluid">
47         <h4>Communities</h4>
48         <div class="table-responsive">
49           <table id="community_table" class="table table-sm table-hover">
50             <thead class="pointer">
51               <tr>
52                 <th>Name</th>
53                 <th>Title</th>
54                 <th>Category</th>
55                 <th class="text-right">Subscribers</th>
56                 <th class="text-right">Posts</th>
57                 <th class="text-right">Comments</th>
58                 <th></th>
59               </tr>
60             </thead>
61             <tbody>
62               {this.state.communities.map(community =>
63                 <tr>
64                   <td><Link to={`/community/${community.id}`}>{community.name}</Link></td>
65                   <td>{community.title}</td>
66                   <td>{community.category_name}</td>
67                   <td class="text-right">{community.number_of_subscribers}</td>
68                   <td class="text-right">{community.number_of_posts}</td>
69                   <td class="text-right">{community.number_of_comments}</td>
70                   <td class="text-right">
71                     {community.subscribed 
72                       ? <button class="btn btn-sm btn-secondary" onClick={linkEvent(community.id, this.handleUnsubscribe)}>Unsubscribe</button>
73                       : <button class="btn btn-sm btn-secondary" onClick={linkEvent(community.id, this.handleSubscribe)}>Subscribe</button>
74                     }
75                   </td>
76                 </tr>
77               )}
78             </tbody>
79           </table>
80         </div>
81       </div>
82     );
83   }
84
85   handleUnsubscribe(communityId: number) {
86     let form: FollowCommunityForm = {
87       community_id: communityId,
88       follow: false
89     };
90     WebSocketService.Instance.followCommunity(form);
91   }
92
93
94   handleSubscribe(communityId: number) {
95     let form: FollowCommunityForm = {
96       community_id: communityId,
97       follow: true
98     };
99     WebSocketService.Instance.followCommunity(form);
100   }
101
102   parseMessage(msg: any) {
103     console.log(msg);
104     let op: UserOperation = msgOp(msg);
105     if (msg.error) {
106       alert(msg.error);
107       return;
108     } else if (op == UserOperation.ListCommunities) {
109       let res: ListCommunitiesResponse = msg;
110       this.state.communities = res.communities;
111       this.state.communities.sort((a, b) => b.number_of_subscribers - a.number_of_subscribers);
112       this.setState(this.state);
113     } else if (op == UserOperation.FollowCommunity) {
114       let res: CommunityResponse = msg;
115       let found = this.state.communities.find(c => c.id == res.community.id);
116       found.subscribed = res.community.subscribed;
117       found.number_of_subscribers = res.community.number_of_subscribers;
118       this.setState(this.state);
119     }
120   }
121 }