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